diff --git a/Samples/Serial2NetClientTest/Program.cs b/Samples/Serial2NetClientTest/Program.cs index 39bc2d9..94eae80 100644 --- a/Samples/Serial2NetClientTest/Program.cs +++ b/Samples/Serial2NetClientTest/Program.cs @@ -19,7 +19,7 @@ private static void Main(string[] args) var host = new A2(); // 配置并打开串口COM1 - var serial = host.CreateSerial(1, 9600); + var serial = host.CreateSerial(Coms.COM1, 9600); serial.DataReceived += OnReceiveSerial; serial.Open(); diff --git a/SmartA2/A2.cs b/SmartA2/A2.cs index 6ef2041..1fd73f0 100644 --- a/SmartA2/A2.cs +++ b/SmartA2/A2.cs @@ -6,6 +6,22 @@ namespace SmartA2; +/// 串口 +public enum Coms +{ + /// COM1 + COM1, + + /// COM2 + COM2, + + /// COM3 + COM3, + + /// COM4 + COM4, +} + /// A2硬件工厂 public class A2 { @@ -39,11 +55,11 @@ public class A2 /// /// /// - public SerialPort CreateSerial(Int32 com, Int32 baudrate = 9600) + public SerialPort CreateSerial(Coms com, Int32 baudrate = 9600) { - if (com <= 0 || com > 4) throw new ArgumentOutOfRangeException(nameof(com), $"无效串口COM{com},支持COM1/COM2/COM3/COM4"); + if (com < 0 || com > Coms.COM4) throw new ArgumentOutOfRangeException(nameof(com), $"无效串口{com},支持COM1/COM2/COM3/COM4"); - return new SerialPort(ComNames[com - 1], baudrate); + return new SerialPort(ComNames[(Int32)com], baudrate); } /// 创建Modbus @@ -51,11 +67,11 @@ public SerialPort CreateSerial(Int32 com, Int32 baudrate = 9600) /// /// /// - public Modbus CreateModbus(Int32 com, Int32 baudrate = 9600) + public Modbus CreateModbus(Coms com, Int32 baudrate = 9600) { - if (com <= 0 || com > 4) throw new ArgumentOutOfRangeException(nameof(com), $"无效串口COM{com},支持COM1/COM2/COM3/COM4"); + if (com < 0 || com > Coms.COM4) throw new ArgumentOutOfRangeException(nameof(com), $"无效串口{com},支持COM1/COM2/COM3/COM4"); - return new ModbusRtu { PortName = ComNames[com - 1], Baudrate = baudrate }; + return new ModbusRtu { PortName = ComNames[(Int32)com], Baudrate = baudrate }; } #endregion