NewLife/SmartA4

A4使用IoT板卡架构
智能大石头 authored at 2025-05-18 19:11:16
9f52f95
Tree
1 Parent(s) 5c88e7b
Summary: 4 changed files with 67 additions and 20 deletions.
Modified +4 -3
Modified +49 -13
Modified +9 -4
Modified +5 -0
Modified +4 -3
diff --git a/Samples/Serial2NetClientTest/Program.cs b/Samples/Serial2NetClientTest/Program.cs
index 5c464eb..5f942a1 100644
--- a/Samples/Serial2NetClientTest/Program.cs
+++ b/Samples/Serial2NetClientTest/Program.cs
@@ -1,6 +1,7 @@
 using System.IO.Ports;
 using NewLife;
 using NewLife.Data;
+using NewLife.IoT.Controllers;
 using NewLife.Log;
 using NewLife.Net;
 using SmartA4;
@@ -9,7 +10,7 @@ using SmartA4;
 
 internal class Program
 {
-    static SerialPort _serial;
+    static ISerialPort _serial;
     static ISocketRemote _client;
 
     private static void Main(string[] args)
@@ -20,7 +21,7 @@ internal class Program
 
         // 配置并打开串口COM1
         var serial = host.CreateSerial(Coms.COM1, 9600);
-        serial.DataReceived += OnReceiveSerial;
+        serial.Received += Serial_Received;
         serial.Open();
 
         // 服务器地址,可保存在配置文件中,支持tcp/udp地址
@@ -38,7 +39,7 @@ internal class Program
         Console.ReadLine();
     }
 
-    static void OnReceiveSerial(Object sender, SerialDataReceivedEventArgs e)
+    static void Serial_Received(Object sender, ReceivedEventArgs e)
     {
         // 等一会儿,等待数据接收完毕
         Thread.Sleep(10);
Modified +49 -13
diff --git a/SmartA4/A4.cs b/SmartA4/A4.cs
index d40ca68..d0b5753 100644
--- a/SmartA4/A4.cs
+++ b/SmartA4/A4.cs
@@ -1,7 +1,6 @@
 using System.ComponentModel;
-using System.IO.Ports;
 using NewLife;
-using NewLife.IoT.Protocols;
+using NewLife.IoT.Controllers;
 using NewLife.Serial.Protocols;
 
 namespace SmartA4;
@@ -23,59 +22,96 @@ public enum Coms
 }
 
 /// <summary>A2硬件工厂</summary>
-public class A4
+public class A4 : Board
 {
     #region 属性
     /// <summary>SYS LED 指示灯</summary>
     [DisplayName("指示灯")]
-    public OutputPort Led { get; } = new OutputPort { FileName = "/dev/led" };
+    public IOutputPort Led { get; } = new FileOutputPort("/dev/led");
 
     /// <summary>SYS LED 指示灯</summary>
     [DisplayName("指示灯")]
-    public OutputPort Led2 { get; } = new OutputPort { FileName = "/dev/led2" };
+    public IOutputPort Led2 { get; } = new FileOutputPort("/dev/led2");
 
     /// <summary>SYS LED 指示灯</summary>
     [DisplayName("指示灯")]
-    public OutputPort Led3 { get; } = new OutputPort { FileName = "/dev/led3" };
+    public IOutputPort Led3 { get; } = new FileOutputPort("/dev/led3");
 
     /// <summary>蜂鸣器</summary>
     [DisplayName("蜂鸣器")]
-    public OutputPort Buzzer { get; } = new OutputPort { FileName = "/dev/buzzer" };
+    public IOutputPort Buzzer { get; } = new FileOutputPort("/dev/buzzer");
 
     /// <summary>FUN按键</summary>
     [DisplayName("FUN按键")]
-    public InputPort Key { get; } = new InputPort { FileName = "/dev/key" };
+    public IInputPort Key { get; } = new FileInputPort("/dev/key");
 
     /// <summary>USB电源</summary>
     [DisplayName("USB电源")]
-    public OutputPort UsbPower { get; } = new OutputPort { FileName = "/dev/usbpwr" };
+    public IOutputPort UsbPower { get; } = new FileOutputPort("/dev/usbpwr");
 
     /// <summary>看门狗</summary>
     public Watchdog Watchdog { get; } = new Watchdog { FileName = "/dev/watchdog" };
 
     /// <summary>串口名</summary>
-    public String[] ComNames = new[] { "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3", "/dev/ttyS4" };
+    public String[] ComNames = ["/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3", "/dev/ttyS4"];
+    #endregion
+
+    #region 端口
+    /// <summary>创建输出口</summary>
+    /// <param name="name"></param>
+    /// <returns></returns>
+    public override IOutputPort CreateOutput(String name)
+    {
+        if (name.EndsWithIgnoreCase("led")) return Led;
+        if (name.EndsWithIgnoreCase("buzzer")) return Buzzer;
+        if (name.EndsWithIgnoreCase("usbpwr")) return UsbPower;
+
+        return base.CreateOutput(name);
+    }
+
+    /// <summary>创建输入口</summary>
+    /// <param name="name"></param>
+    /// <returns></returns>
+    public override IInputPort CreateInput(String name)
+    {
+        if (name.EndsWithIgnoreCase("key")) return Key;
+
+        return base.CreateInput(name);
+    }
     #endregion
 
     #region 串口
     /// <summary>创建串口</summary>
+    /// <param name="portName"></param>
+    /// <param name="baudrate"></param>
+    /// <returns></returns>
+    public override ISerialPort CreateSerial(String portName, Int32 baudrate = 9600) => new DefaultSerialPort { PortName = portName, Baudrate = baudrate };
+
+    /// <summary>创建串口</summary>
     /// <param name="com">串口COM1/COM2/COM3/COM4,全部支持RS485,其中COM3/COM4复用RS232</param>
     /// <param name="baudrate"></param>
     /// <returns></returns>
     /// <exception cref="ArgumentOutOfRangeException"></exception>
-    public SerialPort CreateSerial(Coms com, Int32 baudrate = 9600)
+    public ISerialPort CreateSerial(Coms com, Int32 baudrate = 9600)
     {
         if (com < 0 || com > Coms.COM4) throw new ArgumentOutOfRangeException(nameof(com), $"无效串口{com},支持COM1/COM2/COM3/COM4");
 
-        return new SerialPort(ComNames[(Int32)com], baudrate);
+        //return new SerialPort(ComNames[(Int32)com], baudrate);
+        return new DefaultSerialPort { PortName = ComNames[(Int32)com], Baudrate = baudrate };
     }
 
     /// <summary>创建Modbus</summary>
+    /// <param name="portName"></param>
+    /// <param name="baudrate"></param>
+    /// <returns></returns>
+    public override IModbus CreateModbus(String portName, Int32 baudrate = 9600) => new ModbusRtu { PortName = portName, Baudrate = baudrate };
+
+    /// <summary>创建Modbus</summary>
     /// <param name="com"></param>
     /// <param name="baudrate"></param>
     /// <returns></returns>
     /// <exception cref="ArgumentOutOfRangeException"></exception>
-    public Modbus CreateModbus(Coms com, Int32 baudrate = 9600)
+    public IModbus CreateModbus(Coms com, Int32 baudrate = 9600)
     {
         if (com < 0 || com > Coms.COM4) throw new ArgumentOutOfRangeException(nameof(com), $"无效串口{com},支持COM1/COM2/COM3/COM4");
 
Modified +9 -4
diff --git a/SmartA4/Drivers/A4Driver.cs b/SmartA4/Drivers/A4Driver.cs
index 35b05a3..098f425 100644
--- a/SmartA4/Drivers/A4Driver.cs
+++ b/SmartA4/Drivers/A4Driver.cs
@@ -1,6 +1,7 @@
 using System.ComponentModel;
 using System.Reflection;
 using NewLife;
+using NewLife.IoT.Controllers;
 using NewLife.IoT.Drivers;
 using NewLife.IoT.ThingModels;
 using NewLife.IoT.ThingSpecification;
@@ -53,9 +54,9 @@ public class A4Driver : DriverBase<Node, A4Parameter>
             if (point != null)
             {
                 var val = _a4.GetValue(pi);
-                if (val is InputPort inputPort)
+                if (val is IInputPort inputPort)
                     dic[point.Name] = inputPort.Read();
-                else if (val is OutputPort outputPort)
+                else if (val is IOutputPort outputPort)
                     dic[point.Name] = outputPort.Read();
             }
         }
@@ -148,7 +149,9 @@ public class A4Driver : DriverBase<Node, A4Parameter>
 
         // 只读
         foreach (var item in points)
+        {
             item.AccessMode = "r";
+        }
 
         //services.Add(ServiceSpec.Create(Speak));
         services.Add(ServiceSpec.Create(Reboot));
@@ -156,20 +159,22 @@ public class A4Driver : DriverBase<Node, A4Parameter>
 
         // A2特有
         foreach (var pi in _a4.GetType().GetProperties())
-            if (pi.PropertyType == typeof(InputPort))
+        {
+            if (pi.PropertyType == typeof(IInputPort))
             {
                 var pt = PropertySpec.Create(pi);
                 pt.DataType.Type = "bool";
                 pt.AccessMode = "r";
                 points.Add(pt);
             }
-            else if (pi.PropertyType == typeof(OutputPort))
+            else if (pi.PropertyType == typeof(IOutputPort))
             {
                 var pt = PropertySpec.Create(pi);
                 pt.DataType.Type = "bool";
                 pt.AccessMode = "rw";
                 points.Add(pt);
             }
+        }
         services.Add(ServiceSpec.Create(SetHostName));
 
         spec.Properties = points.Where(e => e != null).ToArray();
Modified +5 -0
diff --git a/SmartA4/SmartA4.csproj b/SmartA4/SmartA4.csproj
index 0e416ad..6225d51 100644
--- a/SmartA4/SmartA4.csproj
+++ b/SmartA4/SmartA4.csproj
@@ -37,6 +37,11 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <Compile Remove="InputPort.cs" />
+    <Compile Remove="OutputPort.cs" />
+  </ItemGroup>
+
+  <ItemGroup>
     <PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>