From 7d82d647d18b232e0928f689277a54e958a2120a Mon Sep 17 00:00:00 2001 From: parallelbgls Date: Fri, 22 Dec 2017 11:49:25 +0800 Subject: [PATCH] 2017-12-22 Update 1 MatchController --- .../AddressTranslatorModbus.cs | 60 ++++++------ ...rotocal.cs => ModbusAsciiInTcpProtocol.cs} | 12 +-- ...r.cs => ModbusAsciiInTcpProtocolLinker.cs} | 14 +-- ...sciiProtocal.cs => ModbusAsciiProtocol.cs} | 8 +- ...Linker.cs => ModbusAsciiProtocolLinker.cs} | 15 +-- .../{ModbusProtocal.cs => ModbusProtocol.cs} | 40 ++++---- ....cs => ModbusProtocolLinkerBytesExtend.cs} | 10 +- ...pProtocal.cs => ModbusRtuInTcpProtocol.cs} | 12 +-- ...ker.cs => ModbusRtuInTcpProtocolLinker.cs} | 14 +-- ...busRtuProtocal.cs => ModbusRtuProtocol.cs} | 8 +- ...alLinker.cs => ModbusRtuProtocolLinker.cs} | 15 +-- ...busTcpProtocal.cs => ModbusTcpProtocol.cs} | 12 +-- ...alLinker.cs => ModbusTcpProtocolLinker.cs} | 14 +-- Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs | 36 ++++---- .../{OpcDaProtocal.cs => OpcDaProtocol.cs} | 8 +- ...otocalLinker.cs => OpcDaProtocolLinker.cs} | 6 +- Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs | 2 +- .../{OpcProtocal.cs => OpcProtocol.cs} | 16 ++-- ...ProtocalLinker.cs => OpcProtocolLinker.cs} | 2 +- .../{OpcUaProtocal.cs => OpcUaProtocol.cs} | 8 +- ...otocalLinker.cs => OpcUaProtocolLinker.cs} | 6 +- Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs | 2 +- Modbus.Net/Modbus.Net.OPC/OpcUtility.cs | 10 +- ...nsPpiProtocal.cs => SiemensPpiProtocol.cs} | 16 ++-- ...lLinker.cs => SiemensPpiProtocolLinker.cs} | 17 ++-- ...{SiemensProtocal.cs => SiemensProtocol.cs} | 42 ++++----- ...cs => SiemensProtocolLinkerBytesExtend.cs} | 4 +- ...nsTcpProtocal.cs => SiemensTcpProtocol.cs} | 26 +++--- ...lLinker.cs => SiemensTcpProtocolLinker.cs} | 13 +-- .../Modbus.Net.Siemens/SiemensUtility.cs | 14 +-- Modbus.Net/Modbus.Net/AddressCombiner.cs | 28 +++--- Modbus.Net/Modbus.Net/AddressHelper.cs | 10 +- Modbus.Net/Modbus.Net/BaseController.cs | 67 -------------- Modbus.Net/Modbus.Net/BaseMachine.cs | 4 +- .../{BaseProtocal.cs => BaseProtocol.cs} | 66 +++++++------- Modbus.Net/Modbus.Net/BaseUtility.cs | 14 +-- Modbus.Net/Modbus.Net/ComProtocalLinker.cs | 8 +- Modbus.Net/Modbus.Net/FifoController.cs | 77 ++++++++++++++++ .../Modbus.Net/{IProtocal.cs => IProtocol.cs} | 20 ++-- ...alFormatting.cs => IProtocolFormatting.cs} | 4 +- ...{IProtocalLinker.cs => IProtocolLinker.cs} | 2 +- ...xtend.cs => IProtocolLinkerBytesExtend.cs} | 4 +- Modbus.Net/Modbus.Net/MatchController.cs | 91 +++++++++++++++++++ Modbus.Net/Modbus.Net/PipeUnit.cs | 60 ++++++------ Modbus.Net/Modbus.Net/ProtocalLinker.cs | 8 +- Modbus.Net/Modbus.Net/ProtocalUnit.cs | 10 +- Modbus.Net/Modbus.Net/README.md | 62 ++++++------- Modbus.Net/Modbus.Net/TcpProtocalLinker.cs | 8 +- 48 files changed, 555 insertions(+), 450 deletions(-) rename Modbus.Net/Modbus.Net.Modbus/{ModbusAsciiInTcpProtocal.cs => ModbusAsciiInTcpProtocol.cs} (78%) rename Modbus.Net/Modbus.Net.Modbus/{ModbusAsciiInTcpProtocalLinker.cs => ModbusAsciiInTcpProtocolLinker.cs} (74%) rename Modbus.Net/Modbus.Net.Modbus/{ModbusAsciiProtocal.cs => ModbusAsciiProtocol.cs} (79%) rename Modbus.Net/Modbus.Net.Modbus/{ModbusAsciiProtocalLinker.cs => ModbusAsciiProtocolLinker.cs} (68%) rename Modbus.Net/Modbus.Net.Modbus/{ModbusProtocal.cs => ModbusProtocol.cs} (95%) rename Modbus.Net/Modbus.Net.Modbus/{ModbusProtocalLinkerBytesExtend.cs => ModbusProtocolLinkerBytesExtend.cs} (92%) rename Modbus.Net/Modbus.Net.Modbus/{ModbusRtuInTcpProtocal.cs => ModbusRtuInTcpProtocol.cs} (78%) rename Modbus.Net/Modbus.Net.Modbus/{ModbusRtuInTcpProtocalLinker.cs => ModbusRtuInTcpProtocolLinker.cs} (71%) rename Modbus.Net/Modbus.Net.Modbus/{ModbusRtuProtocal.cs => ModbusRtuProtocol.cs} (79%) rename Modbus.Net/Modbus.Net.Modbus/{ModbusRtuProtocalLinker.cs => ModbusRtuProtocolLinker.cs} (64%) rename Modbus.Net/Modbus.Net.Modbus/{ModbusTcpProtocal.cs => ModbusTcpProtocol.cs} (78%) rename Modbus.Net/Modbus.Net.Modbus/{ModbusTcpProtocalLinker.cs => ModbusTcpProtocolLinker.cs} (71%) rename Modbus.Net/Modbus.Net.OPC/{OpcDaProtocal.cs => OpcDaProtocol.cs} (77%) rename Modbus.Net/Modbus.Net.OPC/{OpcDaProtocalLinker.cs => OpcDaProtocolLinker.cs} (77%) rename Modbus.Net/Modbus.Net.OPC/{OpcProtocal.cs => OpcProtocol.cs} (91%) rename Modbus.Net/Modbus.Net.OPC/{OpcProtocalLinker.cs => OpcProtocolLinker.cs} (96%) rename Modbus.Net/Modbus.Net.OPC/{OpcUaProtocal.cs => OpcUaProtocol.cs} (76%) rename Modbus.Net/Modbus.Net.OPC/{OpcUaProtocalLinker.cs => OpcUaProtocolLinker.cs} (77%) rename Modbus.Net/Modbus.Net.Siemens/{SiemensPpiProtocal.cs => SiemensPpiProtocol.cs} (87%) rename Modbus.Net/Modbus.Net.Siemens/{SiemensPpiProtocalLinker.cs => SiemensPpiProtocolLinker.cs} (85%) rename Modbus.Net/Modbus.Net.Siemens/{SiemensProtocal.cs => SiemensProtocol.cs} (95%) rename Modbus.Net/Modbus.Net.Siemens/{SiemensProtocalLinkerBytesExtend.cs => SiemensProtocolLinkerBytesExtend.cs} (95%) rename Modbus.Net/Modbus.Net.Siemens/{SiemensTcpProtocal.cs => SiemensTcpProtocol.cs} (88%) rename Modbus.Net/Modbus.Net.Siemens/{SiemensTcpProtocalLinker.cs => SiemensTcpProtocolLinker.cs} (78%) rename Modbus.Net/Modbus.Net/{BaseProtocal.cs => BaseProtocol.cs} (77%) create mode 100644 Modbus.Net/Modbus.Net/FifoController.cs rename Modbus.Net/Modbus.Net/{IProtocal.cs => IProtocol.cs} (82%) rename Modbus.Net/Modbus.Net/{IProtocalFormatting.cs => IProtocolFormatting.cs} (93%) rename Modbus.Net/Modbus.Net/{IProtocalLinker.cs => IProtocolLinker.cs} (97%) rename Modbus.Net/Modbus.Net/{IProtocalLinkerBytesExtend.cs => IProtocolLinkerBytesExtend.cs} (85%) create mode 100644 Modbus.Net/Modbus.Net/MatchController.cs diff --git a/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs b/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs index 05ca20d..c92cb40 100644 --- a/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs +++ b/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs @@ -70,7 +70,7 @@ namespace Modbus.Net.Modbus "Q", new AreaOutputDef { - Code = (int) ModbusProtocalReadDataFunctionCode.ReadCoilStatus, + Code = (int) ModbusProtocolReadDataFunctionCode.ReadCoilStatus, AreaWidth = 0.125 } }, @@ -78,7 +78,7 @@ namespace Modbus.Net.Modbus "M", new AreaOutputDef { - Code = (int) ModbusProtocalReadDataFunctionCode.ReadCoilStatus, + Code = (int) ModbusProtocolReadDataFunctionCode.ReadCoilStatus, AreaWidth = 0.125 } }, @@ -86,7 +86,7 @@ namespace Modbus.Net.Modbus "N", new AreaOutputDef { - Code = (int) ModbusProtocalReadDataFunctionCode.ReadCoilStatus, + Code = (int) ModbusProtocolReadDataFunctionCode.ReadCoilStatus, AreaWidth = 0.125 } }, @@ -94,7 +94,7 @@ namespace Modbus.Net.Modbus "I", new AreaOutputDef { - Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputStatus, + Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputStatus, AreaWidth = 0.125 } }, @@ -102,29 +102,29 @@ namespace Modbus.Net.Modbus "S", new AreaOutputDef { - Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputStatus, + Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputStatus, AreaWidth = 0.125 } }, { "IW", - new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputRegister, AreaWidth = 2} + new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputRegister, AreaWidth = 2} }, { "SW", - new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputRegister, AreaWidth = 2} + new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputRegister, AreaWidth = 2} }, { "MW", - new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2} + new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2} }, { "NW", - new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2} + new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2} }, { "QW", - new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2} + new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2} } }; WriteFunctionCodeDictionary = new Dictionary<(string, bool), AreaOutputDef> @@ -133,7 +133,7 @@ namespace Modbus.Net.Modbus ("Q", false), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiCoil, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiCoil, AreaWidth = 0.125 } }, @@ -141,7 +141,7 @@ namespace Modbus.Net.Modbus ("M", false), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiCoil, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiCoil, AreaWidth = 0.125 } }, @@ -149,7 +149,7 @@ namespace Modbus.Net.Modbus ("N", false), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiCoil, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiCoil, AreaWidth = 0.125 } }, @@ -157,7 +157,7 @@ namespace Modbus.Net.Modbus ("MW", false), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiRegister, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiRegister, AreaWidth = 2 } }, @@ -165,7 +165,7 @@ namespace Modbus.Net.Modbus ("NW", false), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiRegister, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiRegister, AreaWidth = 2 } }, @@ -173,7 +173,7 @@ namespace Modbus.Net.Modbus ("QW", false), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiRegister, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiRegister, AreaWidth = 2 } }, @@ -181,7 +181,7 @@ namespace Modbus.Net.Modbus ("Q", true), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteSingleCoil, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil, AreaWidth = 0.125 } }, @@ -189,7 +189,7 @@ namespace Modbus.Net.Modbus ("M", true), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteSingleCoil, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil, AreaWidth = 0.125 } }, @@ -197,7 +197,7 @@ namespace Modbus.Net.Modbus ("N", true), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteSingleCoil, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil, AreaWidth = 0.125 } }, @@ -205,7 +205,7 @@ namespace Modbus.Net.Modbus ("MW", true), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteSingleRegister, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleRegister, AreaWidth = 2 } }, @@ -213,7 +213,7 @@ namespace Modbus.Net.Modbus ("NW", true), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteSingleRegister, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleRegister, AreaWidth = 2 } }, @@ -221,7 +221,7 @@ namespace Modbus.Net.Modbus ("QW", true), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteSingleRegister, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleRegister, AreaWidth = 2 } } @@ -306,7 +306,7 @@ namespace Modbus.Net.Modbus "0X", new AreaOutputDef { - Code = (int) ModbusProtocalReadDataFunctionCode.ReadCoilStatus, + Code = (int) ModbusProtocolReadDataFunctionCode.ReadCoilStatus, AreaWidth = 0.125 } }, @@ -314,17 +314,17 @@ namespace Modbus.Net.Modbus "1X", new AreaOutputDef { - Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputStatus, + Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputStatus, AreaWidth = 0.125 } }, { "3X", - new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputRegister, AreaWidth = 2} + new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputRegister, AreaWidth = 2} }, { "4X", - new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2} + new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2} } }; WriteFunctionCodeDictionary = new Dictionary<(string, bool), AreaOutputDef> @@ -333,7 +333,7 @@ namespace Modbus.Net.Modbus ("0X", false), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiCoil, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiCoil, AreaWidth = 0.125 } }, @@ -341,7 +341,7 @@ namespace Modbus.Net.Modbus ("4X", false), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiRegister, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiRegister, AreaWidth = 2 } }, @@ -349,7 +349,7 @@ namespace Modbus.Net.Modbus ("0X", true), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteSingleCoil, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil, AreaWidth = 0.125 } }, @@ -357,7 +357,7 @@ namespace Modbus.Net.Modbus ("4X", true), new AreaOutputDef { - Code = (int) ModbusProtocalWriteDataFunctionCode.WriteSingleRegister, + Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleRegister, AreaWidth = 2 } } diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocal.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocol.cs similarity index 78% rename from Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocal.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocol.cs index 943a681..4256257 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocal.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocol.cs @@ -5,7 +5,7 @@ namespace Modbus.Net.Modbus /// /// Modbus/Ascii码协议 /// - public class ModbusAsciiInTcpProtocal : ModbusProtocal + public class ModbusAsciiInTcpProtocol : ModbusProtocol { /// /// 构造函数 @@ -13,7 +13,7 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusAsciiInTcpProtocal(byte slaveAddress, byte masterAddress, Endian endian) + public ModbusAsciiInTcpProtocol(byte slaveAddress, byte masterAddress, Endian endian) : this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress, endian) { } @@ -25,10 +25,10 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusAsciiInTcpProtocal(string ip, byte slaveAddress, byte masterAddress, Endian endian) + public ModbusAsciiInTcpProtocol(string ip, byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian) { - ProtocalLinker = new ModbusAsciiInTcpProtocalLinker(ip, slaveAddress); + ProtocolLinker = new ModbusAsciiInTcpProtocolLinker(ip, slaveAddress); } /// @@ -39,10 +39,10 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusAsciiInTcpProtocal(string ip, int port, byte slaveAddress, byte masterAddress, Endian endian) + public ModbusAsciiInTcpProtocol(string ip, int port, byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian) { - ProtocalLinker = new ModbusTcpProtocalLinker(ip, port); + ProtocolLinker = new ModbusTcpProtocolLinker(ip, port); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocalLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocolLinker.cs similarity index 74% rename from Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocalLinker.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocolLinker.cs index 1467900..ba6074d 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocolLinker.cs @@ -6,13 +6,13 @@ namespace Modbus.Net.Modbus /// /// Modbus/Ascii码协议连接器Tcp透传 /// - public class ModbusAsciiInTcpProtocalLinker : TcpProtocalLinker + public class ModbusAsciiInTcpProtocolLinker : TcpProtocolLinker { /// /// 构造函数 /// /// IP地址 - public ModbusAsciiInTcpProtocalLinker(string ip) + public ModbusAsciiInTcpProtocolLinker(string ip) : base(ip, int.Parse(ConfigurationManager.AppSettings["ModbusPort"] ?? "502")) { } @@ -22,10 +22,10 @@ namespace Modbus.Net.Modbus /// /// ip地址 /// 端口号 - public ModbusAsciiInTcpProtocalLinker(string ip, int port) + public ModbusAsciiInTcpProtocolLinker(string ip, int port) : base(ip, port) { - ((BaseConnector)BaseConnector).AddController(new FIFOController(500)); + ((BaseConnector)BaseConnector).AddController(new FifoController(500)); } /// @@ -35,15 +35,15 @@ namespace Modbus.Net.Modbus /// 校验是否正确 public override bool? CheckRight(byte[] content) { - //ProtocalLinker不会返回null + //ProtocolLinker不会返回null if (!base.CheckRight(content).Value) return false; //CRC校验失败 var contentString = Encoding.ASCII.GetString(content); if (!Crc16.GetInstance().LrcEfficacy(contentString)) - throw new ModbusProtocalErrorException(501); + throw new ModbusProtocolErrorException(501); //Modbus协议错误 if (byte.Parse(contentString.Substring(3, 2)) > 127) - throw new ModbusProtocalErrorException(byte.Parse(contentString.Substring(5, 2))); + throw new ModbusProtocolErrorException(byte.Parse(contentString.Substring(5, 2))); return true; } } diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocal.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocol.cs similarity index 79% rename from Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocal.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocol.cs index 22c5b58..18d5fff 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocal.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocol.cs @@ -5,7 +5,7 @@ namespace Modbus.Net.Modbus /// /// Modbus/Ascii码协议 /// - public class ModbusAsciiProtocal : ModbusProtocal + public class ModbusAsciiProtocol : ModbusProtocol { /// /// 构造函数 @@ -13,7 +13,7 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusAsciiProtocal(byte slaveAddress, byte masterAddress, Endian endian) + public ModbusAsciiProtocol(byte slaveAddress, byte masterAddress, Endian endian) : this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress, endian) { } @@ -25,10 +25,10 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusAsciiProtocal(string com, byte slaveAddress, byte masterAddress, Endian endian) + public ModbusAsciiProtocol(string com, byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian) { - ProtocalLinker = new ModbusAsciiProtocalLinker(com, slaveAddress); + ProtocolLinker = new ModbusAsciiProtocolLinker(com, slaveAddress); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocalLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocolLinker.cs similarity index 68% rename from Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocalLinker.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocolLinker.cs index 28a5028..46a0354 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocolLinker.cs @@ -1,4 +1,5 @@ -using System.IO.Ports; +using System.Collections.Generic; +using System.IO.Ports; using System.Text; namespace Modbus.Net.Modbus @@ -6,17 +7,17 @@ namespace Modbus.Net.Modbus /// /// Modbus/Ascii码协议连接器 /// - public class ModbusAsciiProtocalLinker : ComProtocalLinker + public class ModbusAsciiProtocolLinker : ComProtocolLinker { /// /// 构造函数 /// /// 串口地址 /// 从站号 - public ModbusAsciiProtocalLinker(string com, int slaveAddress) + public ModbusAsciiProtocolLinker(string com, int slaveAddress) : base(com, 9600, Parity.None, StopBits.One, 8, slaveAddress) { - ((BaseConnector)BaseConnector).AddController(new FIFOController(500)); + ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection[] { new List { 0, 1 }}, 500)); } /// @@ -26,15 +27,15 @@ namespace Modbus.Net.Modbus /// 校验是否正确 public override bool? CheckRight(byte[] content) { - //ProtocalLinker不会返回null + //ProtocolLinker不会返回null if (!base.CheckRight(content).Value) return false; //CRC校验失败 var contentString = Encoding.ASCII.GetString(content); if (!Crc16.GetInstance().LrcEfficacy(contentString)) - throw new ModbusProtocalErrorException(501); + throw new ModbusProtocolErrorException(501); //Modbus协议错误 if (byte.Parse(contentString.Substring(3, 2)) > 127) - throw new ModbusProtocalErrorException(byte.Parse(contentString.Substring(5, 2))); + throw new ModbusProtocolErrorException(byte.Parse(contentString.Substring(5, 2))); return true; } } diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocal.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs similarity index 95% rename from Modbus.Net/Modbus.Net.Modbus/ModbusProtocal.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs index a247903..fe10626 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocal.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs @@ -7,7 +7,7 @@ namespace Modbus.Net.Modbus /// /// 变量功能码 /// - internal enum ModbusProtocalVariableFunctionCode : byte + internal enum ModbusProtocolVariableFunctionCode : byte { /// /// 读变量 @@ -23,7 +23,7 @@ namespace Modbus.Net.Modbus /// /// 跟时间有关的功能码 /// - public enum ModbusProtocalTimeFunctionCode : byte + public enum ModbusProtocolTimeFunctionCode : byte { /// /// 读时间 @@ -39,7 +39,7 @@ namespace Modbus.Net.Modbus /// /// 跟读数据有关的功能码 /// - public enum ModbusProtocalReadDataFunctionCode : byte + public enum ModbusProtocolReadDataFunctionCode : byte { /// /// 读线圈 @@ -65,7 +65,7 @@ namespace Modbus.Net.Modbus /// /// 跟写数据有关的功能码 /// - internal enum ModbusProtocalWriteDataFunctionCode : byte + internal enum ModbusProtocolWriteDataFunctionCode : byte { /// /// 写单个线圈 @@ -91,7 +91,7 @@ namespace Modbus.Net.Modbus /// /// Modbus协议 /// - public abstract class ModbusProtocal : BaseProtocal + public abstract class ModbusProtocol : BaseProtocol { /// /// 构造函数 @@ -99,7 +99,7 @@ namespace Modbus.Net.Modbus /// 从站地址 /// 主站地址 /// 端格式 - protected ModbusProtocal(byte slaveAddress, byte masterAddress, Endian endian) + protected ModbusProtocol(byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian) { } @@ -110,7 +110,7 @@ namespace Modbus.Net.Modbus /// 是否连接成功 public override async Task ConnectAsync() { - return await ProtocalLinker.ConnectAsync(); + return await ProtocolLinker.ConnectAsync(); } } @@ -205,7 +205,7 @@ namespace Modbus.Net.Modbus /// /// 读数据协议 /// - public class ReadDataModbusProtocal : ProtocalUnit + public class ReadDataModbusProtocol : ProtocolUnit { /// /// 格式化 @@ -343,7 +343,7 @@ namespace Modbus.Net.Modbus /// /// 写多个寄存器协议 /// - public class WriteDataModbusProtocal : ProtocalUnit + public class WriteDataModbusProtocol : ProtocolUnit { /// /// 格式化 @@ -398,7 +398,7 @@ namespace Modbus.Net.Modbus StartAddress = (ushort) translateAddress.Address; int a = 0, b = 0; var writeByteValue = - FunctionCode == (byte) ModbusProtocalWriteDataFunctionCode.WriteSingleCoil + FunctionCode == (byte) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil ? ((bool) writeValue ? new byte[] {0xFF, 0x00} : new byte[] {0x00, 0x00}) @@ -473,7 +473,7 @@ namespace Modbus.Net.Modbus /// /// 写多个寄存器协议 /// - public class WriteSingleDataModbusProtocal : ProtocalUnit + public class WriteSingleDataModbusProtocol : ProtocolUnit { /// /// 格式化 @@ -501,7 +501,7 @@ namespace Modbus.Net.Modbus var functionCode = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag); var startAddress = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag); var writeValue = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag); - var returnValue = functionCode == (byte)ModbusProtocalWriteDataFunctionCode.WriteSingleCoil + var returnValue = functionCode == (byte)ModbusProtocolWriteDataFunctionCode.WriteSingleCoil ? (object)(writeValue == 0xFF00) : writeValue; return new WriteSingleDataModbusOutputStruct(slaveAddress, functionCode, startAddress, returnValue); @@ -524,7 +524,7 @@ namespace Modbus.Net.Modbus public GetSystemTimeModbusInputStruct(byte slaveAddress) { SlaveAddress = slaveAddress; - FunctionCode = (byte) ModbusProtocalTimeFunctionCode.GetSystemTime; + FunctionCode = (byte) ModbusProtocolTimeFunctionCode.GetSystemTime; StartAddress = 30000; GetCount = 5; } @@ -602,7 +602,7 @@ namespace Modbus.Net.Modbus /// /// 读系统时间协议 /// - public class GetSystemTimeModbusProtocal : ProtocalUnit + public class GetSystemTimeModbusProtocol : ProtocolUnit { /// /// 格式化 @@ -656,7 +656,7 @@ namespace Modbus.Net.Modbus public SetSystemTimeModbusInputStruct(byte slaveAddress, DateTime time) { SlaveAddress = slaveAddress; - FunctionCode = (byte) ModbusProtocalTimeFunctionCode.SetSystemTime; + FunctionCode = (byte) ModbusProtocolTimeFunctionCode.SetSystemTime; StartAddress = 30000; WriteCount = 5; WriteByteCount = 10; @@ -775,7 +775,7 @@ namespace Modbus.Net.Modbus /// /// 写系统时间协议 /// - public class SetSystemTimeModbusProtocal : ProtocalUnit + public class SetSystemTimeModbusProtocol : ProtocolUnit { /// /// 格式化 @@ -812,9 +812,9 @@ namespace Modbus.Net.Modbus /// /// Modbus协议错误表 /// - public class ModbusProtocalErrorException : ProtocalErrorException + public class ModbusProtocolErrorException : ProtocolErrorException { - private static readonly Dictionary ProtocalErrorDictionary = new Dictionary + private static readonly Dictionary ProtocolErrorDictionary = new Dictionary { {1, "ILLEGAL_FUNCTION"}, {2, "ILLEGAL_DATA_ACCESS"}, @@ -830,8 +830,8 @@ namespace Modbus.Net.Modbus /// Modbus错误 /// /// Modbus错误号 - public ModbusProtocalErrorException(int messageNumber) - : base(ProtocalErrorDictionary[messageNumber]) + public ModbusProtocolErrorException(int messageNumber) + : base(ProtocolErrorDictionary[messageNumber]) { ErrorMessageNumber = messageNumber; } diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocalLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs similarity index 92% rename from Modbus.Net/Modbus.Net.Modbus/ModbusProtocalLinkerBytesExtend.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs index 7796252..4063964 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocalLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs @@ -5,12 +5,12 @@ using System.Text; namespace Modbus.Net.Modbus { - public class ModbusRtuInTcpProtocalLinkerBytesExtend : ModbusRtuProtocalLinkerBytesExtend + public class ModbusRtuInTcpProtocolLinkerBytesExtend : ModbusRtuProtocolLinkerBytesExtend { } - public class ModbusAsciiInTcpProtocalLinkerBytesExtend : ModbusAsciiProtocalLinkerBytesExtend + public class ModbusAsciiInTcpProtocolLinkerBytesExtend : ModbusAsciiProtocolLinkerBytesExtend { } @@ -18,7 +18,7 @@ namespace Modbus.Net.Modbus /// /// Tcp协议字节伸缩 /// - public class ModbusTcpProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend + public class ModbusTcpProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend { /// /// 协议扩展,协议内容发送前调用 @@ -54,7 +54,7 @@ namespace Modbus.Net.Modbus /// /// Rtu协议字节伸缩 /// - public class ModbusRtuProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend + public class ModbusRtuProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend { /// /// 协议扩展,协议内容发送前调用 @@ -89,7 +89,7 @@ namespace Modbus.Net.Modbus /// /// Ascii协议字节伸缩 /// - public class ModbusAsciiProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend + public class ModbusAsciiProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend { /// /// 协议扩展,协议内容发送前调用 diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocal.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocol.cs similarity index 78% rename from Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocal.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocol.cs index 07cb907..a209c04 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocal.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocol.cs @@ -6,7 +6,7 @@ namespace Modbus.Net.Modbus /// /// Modbus/Rtu协议tcp透传 /// - public class ModbusRtuInTcpProtocal : ModbusProtocal + public class ModbusRtuInTcpProtocol : ModbusProtocol { /// /// 构造函数 @@ -14,7 +14,7 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusRtuInTcpProtocal(byte slaveAddress, byte masterAddress, Endian endian) + public ModbusRtuInTcpProtocol(byte slaveAddress, byte masterAddress, Endian endian) : this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress, endian) { } @@ -26,10 +26,10 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusRtuInTcpProtocal(string ip, byte slaveAddress, byte masterAddress, Endian endian) + public ModbusRtuInTcpProtocol(string ip, byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian) { - ProtocalLinker = new ModbusTcpProtocalLinker(ip); + ProtocolLinker = new ModbusTcpProtocolLinker(ip); } /// @@ -39,10 +39,10 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusRtuInTcpProtocal(string ip, int port, byte slaveAddress, byte masterAddress, Endian endian) + public ModbusRtuInTcpProtocol(string ip, int port, byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian) { - ProtocalLinker = new ModbusRtuInTcpProtocalLinker(ip, port); + ProtocolLinker = new ModbusRtuInTcpProtocolLinker(ip, port); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocalLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocolLinker.cs similarity index 71% rename from Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocalLinker.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocolLinker.cs index 896178c..14713de 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocolLinker.cs @@ -5,13 +5,13 @@ namespace Modbus.Net.Modbus /// /// Modbus/Rtu协议连接器 /// - public class ModbusRtuInTcpProtocalLinker : TcpProtocalLinker + public class ModbusRtuInTcpProtocolLinker : TcpProtocolLinker { /// /// 构造函数 /// /// IP地址 - public ModbusRtuInTcpProtocalLinker(string ip) + public ModbusRtuInTcpProtocolLinker(string ip) : base(ip, int.Parse(ConfigurationManager.AppSettings["ModbusPort"] ?? "502")) { } @@ -21,10 +21,10 @@ namespace Modbus.Net.Modbus /// /// IP地址 /// 端口号 - public ModbusRtuInTcpProtocalLinker(string ip, int port) + public ModbusRtuInTcpProtocolLinker(string ip, int port) : base(ip, port) { - ((BaseConnector)BaseConnector).AddController(new FIFOController(500)); + ((BaseConnector)BaseConnector).AddController(new FifoController(500)); } /// @@ -34,14 +34,14 @@ namespace Modbus.Net.Modbus /// 数据是否正确 public override bool? CheckRight(byte[] content) { - //ProtocalLinker的CheckRight不会返回null + //ProtocolLinker的CheckRight不会返回null if (!base.CheckRight(content).Value) return false; //CRC校验失败 if (!Crc16.GetInstance().CrcEfficacy(content)) - throw new ModbusProtocalErrorException(501); + throw new ModbusProtocolErrorException(501); //Modbus协议错误 if (content[1] > 127) - throw new ModbusProtocalErrorException(content[2]); + throw new ModbusProtocolErrorException(content[2]); return true; } } diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocal.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocol.cs similarity index 79% rename from Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocal.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocol.cs index 46a7b17..6227e43 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocal.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocol.cs @@ -5,7 +5,7 @@ namespace Modbus.Net.Modbus /// /// Modbus/Rtu协议 /// - public class ModbusRtuProtocal : ModbusProtocal + public class ModbusRtuProtocol : ModbusProtocol { /// /// 构造函数 @@ -13,7 +13,7 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusRtuProtocal(byte slaveAddress, byte masterAddress, Endian endian) + public ModbusRtuProtocol(byte slaveAddress, byte masterAddress, Endian endian) : this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress, endian) { } @@ -25,10 +25,10 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusRtuProtocal(string com, byte slaveAddress, byte masterAddress, Endian endian) + public ModbusRtuProtocol(string com, byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian) { - ProtocalLinker = new ModbusRtuProtocalLinker(com, slaveAddress); + ProtocolLinker = new ModbusRtuProtocolLinker(com, slaveAddress); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocalLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocolLinker.cs similarity index 64% rename from Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocalLinker.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocolLinker.cs index 06f71fb..aa42ce7 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocolLinker.cs @@ -1,21 +1,22 @@ -using System.IO.Ports; +using System.Collections.Generic; +using System.IO.Ports; namespace Modbus.Net.Modbus { /// /// Modbus/Rtu协议连接器 /// - public class ModbusRtuProtocalLinker : ComProtocalLinker + public class ModbusRtuProtocolLinker : ComProtocolLinker { /// /// 构造函数 /// /// 串口地址 /// 从站号 - public ModbusRtuProtocalLinker(string com, int slaveAddress) + public ModbusRtuProtocolLinker(string com, int slaveAddress) : base(com, 9600, Parity.None, StopBits.One, 8, slaveAddress) { - ((BaseConnector)BaseConnector).AddController(new FIFOController(500)); + ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection[]{new List{0}}, 500)); } /// @@ -25,14 +26,14 @@ namespace Modbus.Net.Modbus /// 数据是否正确 public override bool? CheckRight(byte[] content) { - //ProtocalLinker的CheckRight不会返回null + //ProtocolLinker的CheckRight不会返回null if (!base.CheckRight(content).Value) return false; //CRC校验失败 if (!Crc16.GetInstance().CrcEfficacy(content)) - throw new ModbusProtocalErrorException(501); + throw new ModbusProtocolErrorException(501); //Modbus协议错误 if (content[1] > 127) - throw new ModbusProtocalErrorException(content[2]); + throw new ModbusProtocolErrorException(content[2]); return true; } } diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocal.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocol.cs similarity index 78% rename from Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocal.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocol.cs index 2822c8f..0ae1ce1 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocal.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocol.cs @@ -5,7 +5,7 @@ namespace Modbus.Net.Modbus /// /// Modbus/Tcp协议 /// - public class ModbusTcpProtocal : ModbusProtocal + public class ModbusTcpProtocol : ModbusProtocol { /// /// 构造函数 @@ -13,7 +13,7 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusTcpProtocal(byte slaveAddress, byte masterAddress, Endian endian) + public ModbusTcpProtocol(byte slaveAddress, byte masterAddress, Endian endian) : this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress, endian) { } @@ -25,10 +25,10 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusTcpProtocal(string ip, byte slaveAddress, byte masterAddress, Endian endian) + public ModbusTcpProtocol(string ip, byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian) { - ProtocalLinker = new ModbusTcpProtocalLinker(ip); + ProtocolLinker = new ModbusTcpProtocolLinker(ip); } /// @@ -39,10 +39,10 @@ namespace Modbus.Net.Modbus /// 从站号 /// 主站号 /// 端格式 - public ModbusTcpProtocal(string ip, int port, byte slaveAddress, byte masterAddress, Endian endian) + public ModbusTcpProtocol(string ip, int port, byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian) { - ProtocalLinker = new ModbusTcpProtocalLinker(ip, port); + ProtocolLinker = new ModbusTcpProtocolLinker(ip, port); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocalLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocolLinker.cs similarity index 71% rename from Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocalLinker.cs rename to Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocolLinker.cs index 31939d6..250aecb 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocolLinker.cs @@ -5,13 +5,13 @@ namespace Modbus.Net.Modbus /// /// Modbus/Tcp协议连接器 /// - public class ModbusTcpProtocalLinker : TcpProtocalLinker + public class ModbusTcpProtocolLinker : TcpProtocolLinker { /// /// 构造函数 /// /// IP地址 - public ModbusTcpProtocalLinker(string ip) + public ModbusTcpProtocolLinker(string ip) : this(ip, int.Parse(ConfigurationManager.AppSettings["ModbusPort"] ?? "502")) { } @@ -21,9 +21,9 @@ namespace Modbus.Net.Modbus /// /// IP地址 /// 端口 - public ModbusTcpProtocalLinker(string ip, int port) : base(ip, port) + public ModbusTcpProtocolLinker(string ip, int port) : base(ip, port) { - ((BaseConnector)BaseConnector).AddController(new FIFOController(500)); + ((BaseConnector)BaseConnector).AddController(new FifoController(500)); } /// @@ -33,14 +33,14 @@ namespace Modbus.Net.Modbus /// 数据是否正确 public override bool? CheckRight(byte[] content) { - //ProtocalLinker的CheckRight不会返回null + //ProtocolLinker的CheckRight不会返回null if (!base.CheckRight(content).Value) return false; //长度校验失败 if (content[5] != content.Length - 6) - throw new ModbusProtocalErrorException(500); + throw new ModbusProtocolErrorException(500); //Modbus协议错误 if (content[7] > 127) - throw new ModbusProtocalErrorException(content[2] > 0 ? content[2] : content[8]); + throw new ModbusProtocolErrorException(content[2] > 0 ? content[2] : content[8]); return true; } } diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs index f00c194..3d0e578 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs @@ -145,18 +145,18 @@ namespace Modbus.Net.Modbus case ModbusType.Rtu: { Wrapper = ConnectionString == null - ? new ModbusRtuProtocal(SlaveAddress, MasterAddress, Endian) - : new ModbusRtuProtocal(ConnectionString, SlaveAddress, MasterAddress, Endian); + ? new ModbusRtuProtocol(SlaveAddress, MasterAddress, Endian) + : new ModbusRtuProtocol(ConnectionString, SlaveAddress, MasterAddress, Endian); break; } //Tcp协议 case ModbusType.Tcp: { Wrapper = ConnectionString == null - ? new ModbusTcpProtocal(SlaveAddress, MasterAddress, Endian) + ? new ModbusTcpProtocol(SlaveAddress, MasterAddress, Endian) : (ConnectionStringPort == null - ? new ModbusTcpProtocal(ConnectionString, SlaveAddress, MasterAddress, Endian) - : new ModbusTcpProtocal(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress, + ? new ModbusTcpProtocol(ConnectionString, SlaveAddress, MasterAddress, Endian) + : new ModbusTcpProtocol(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress, MasterAddress, Endian)); break; } @@ -164,18 +164,18 @@ namespace Modbus.Net.Modbus case ModbusType.Ascii: { Wrapper = ConnectionString == null - ? new ModbusAsciiProtocal(SlaveAddress, MasterAddress, Endian) - : new ModbusAsciiProtocal(ConnectionString, SlaveAddress, MasterAddress, Endian); + ? new ModbusAsciiProtocol(SlaveAddress, MasterAddress, Endian) + : new ModbusAsciiProtocol(ConnectionString, SlaveAddress, MasterAddress, Endian); break; } //Rtu协议 case ModbusType.RtuInTcp: { Wrapper = ConnectionString == null - ? new ModbusRtuInTcpProtocal(SlaveAddress, MasterAddress, Endian) + ? new ModbusRtuInTcpProtocol(SlaveAddress, MasterAddress, Endian) : (ConnectionStringPort == null - ? new ModbusRtuInTcpProtocal(ConnectionString, SlaveAddress, MasterAddress, Endian) - : new ModbusRtuInTcpProtocal(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress, + ? new ModbusRtuInTcpProtocol(ConnectionString, SlaveAddress, MasterAddress, Endian) + : new ModbusRtuInTcpProtocol(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress, MasterAddress, Endian)); break; } @@ -183,10 +183,10 @@ namespace Modbus.Net.Modbus case ModbusType.AsciiInTcp: { Wrapper = ConnectionString == null - ? new ModbusAsciiInTcpProtocal(SlaveAddress, MasterAddress, Endian) + ? new ModbusAsciiInTcpProtocol(SlaveAddress, MasterAddress, Endian) : (ConnectionStringPort == null - ? new ModbusAsciiInTcpProtocal(ConnectionString, SlaveAddress, MasterAddress, Endian) - : new ModbusAsciiInTcpProtocal(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress, + ? new ModbusAsciiInTcpProtocol(ConnectionString, SlaveAddress, MasterAddress, Endian) + : new ModbusAsciiInTcpProtocol(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress, MasterAddress, Endian)); break; } @@ -205,7 +205,7 @@ namespace Modbus.Net.Modbus var inputStruct = new GetSystemTimeModbusInputStruct(SlaveAddress); var outputStruct = await Wrapper.SendReceiveAsync( - Wrapper[typeof(GetSystemTimeModbusProtocal)], inputStruct); + Wrapper[typeof(GetSystemTimeModbusProtocol)], inputStruct); return outputStruct?.Time ?? DateTime.MinValue; } catch (Exception e) @@ -227,7 +227,7 @@ namespace Modbus.Net.Modbus var inputStruct = new SetSystemTimeModbusInputStruct(SlaveAddress, setTime); var outputStruct = await Wrapper.SendReceiveAsync( - Wrapper[typeof(SetSystemTimeModbusProtocal)], inputStruct); + Wrapper[typeof(SetSystemTimeModbusProtocol)], inputStruct); return outputStruct?.WriteCount > 0; } catch (Exception e) @@ -259,7 +259,7 @@ namespace Modbus.Net.Modbus var inputStruct = new ReadDataModbusInputStruct(SlaveAddress, startAddress, (ushort) getByteCount, AddressTranslator); var outputStruct = await - Wrapper.SendReceiveAsync(Wrapper[typeof(ReadDataModbusProtocal)], + Wrapper.SendReceiveAsync(Wrapper[typeof(ReadDataModbusProtocol)], inputStruct); return outputStruct?.DataValue; } @@ -283,7 +283,7 @@ namespace Modbus.Net.Modbus var inputStruct = new WriteDataModbusInputStruct(SlaveAddress, startAddress, setContents, AddressTranslator, Endian); var outputStruct = await - Wrapper.SendReceiveAsync(Wrapper[typeof(WriteDataModbusProtocal)], + Wrapper.SendReceiveAsync(Wrapper[typeof(WriteDataModbusProtocol)], inputStruct); return outputStruct?.WriteCount == setContents.Length; } @@ -307,7 +307,7 @@ namespace Modbus.Net.Modbus var inputStruct = new WriteSingleDataModbusInputStruct(SlaveAddress, startAddress, setContent, (ModbusTranslatorBase)AddressTranslator, Endian); var outputStruct = await - Wrapper.SendReceiveAsync(Wrapper[typeof(WriteSingleDataModbusProtocal)], + Wrapper.SendReceiveAsync(Wrapper[typeof(WriteSingleDataModbusProtocol)], inputStruct); return outputStruct?.WriteValue.ToString() == setContent.ToString(); } diff --git a/Modbus.Net/Modbus.Net.OPC/OpcDaProtocal.cs b/Modbus.Net/Modbus.Net.OPC/OpcDaProtocol.cs similarity index 77% rename from Modbus.Net/Modbus.Net.OPC/OpcDaProtocal.cs rename to Modbus.Net/Modbus.Net.OPC/OpcDaProtocol.cs index 798847a..5cfab4e 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcDaProtocal.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcDaProtocol.cs @@ -5,7 +5,7 @@ namespace Modbus.Net.OPC /// /// Opc Da协议 /// - public class OpcDaProtocal : OpcProtocal + public class OpcDaProtocol : OpcProtocol { private readonly string _host; @@ -16,7 +16,7 @@ namespace Modbus.Net.OPC /// /// Opc DA服务地址 /// 是否开启正则匹配 - public OpcDaProtocal(string host, bool isRegexOn) + public OpcDaProtocol(string host, bool isRegexOn) { _host = host; _isRegexOn = isRegexOn; @@ -28,8 +28,8 @@ namespace Modbus.Net.OPC /// 是否连接成功 public override async Task ConnectAsync() { - ProtocalLinker = new OpcDaProtocalLinker(_host, _isRegexOn); - if (!await ProtocalLinker.ConnectAsync()) + ProtocolLinker = new OpcDaProtocolLinker(_host, _isRegexOn); + if (!await ProtocolLinker.ConnectAsync()) return false; return true; } diff --git a/Modbus.Net/Modbus.Net.OPC/OpcDaProtocalLinker.cs b/Modbus.Net/Modbus.Net.OPC/OpcDaProtocolLinker.cs similarity index 77% rename from Modbus.Net/Modbus.Net.OPC/OpcDaProtocalLinker.cs rename to Modbus.Net/Modbus.Net.OPC/OpcDaProtocolLinker.cs index 265a1a8..4056f32 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcDaProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcDaProtocolLinker.cs @@ -5,13 +5,13 @@ namespace Modbus.Net.OPC /// /// Opc Da协议连接器 /// - public class OpcDaProtocalLinker : OpcProtocalLinker + public class OpcDaProtocolLinker : OpcProtocolLinker { /// /// 构造函数 /// /// 是否开启正则匹配 - public OpcDaProtocalLinker(bool isRegexOn) : this(ConfigurationManager.AppSettings["OpcDaHost"], isRegexOn) + public OpcDaProtocolLinker(bool isRegexOn) : this(ConfigurationManager.AppSettings["OpcDaHost"], isRegexOn) { } @@ -20,7 +20,7 @@ namespace Modbus.Net.OPC /// /// Opc DA服务地址 /// 是否开启正则匹配 - public OpcDaProtocalLinker(string host, bool isRegexOn) + public OpcDaProtocolLinker(string host, bool isRegexOn) { BaseConnector = OpcDaConnector.Instance(host, isRegexOn); } diff --git a/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs b/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs index 3acb95e..a778185 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs @@ -12,7 +12,7 @@ /// 是否开启正则匹配 public OpcDaUtility(string connectionString, bool isRegexOn = false) : base(connectionString) { - Wrapper = new OpcDaProtocal(ConnectionString, isRegexOn); + Wrapper = new OpcDaProtocol(ConnectionString, isRegexOn); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs b/Modbus.Net/Modbus.Net.OPC/OpcProtocol.cs similarity index 91% rename from Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs rename to Modbus.Net/Modbus.Net.OPC/OpcProtocol.cs index 3d9f543..a9e376f 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcProtocol.cs @@ -3,14 +3,14 @@ /// /// Opc协议 /// - public abstract class OpcProtocal : BaseProtocal, - PipeUnit, - ProtocalUnit>> + public abstract class OpcProtocol : BaseProtocol, + PipeUnit, + ProtocolUnit>> { /// /// 构造函数 /// - protected OpcProtocal() : base(0, 0, Endian.BigEndianLsb) + protected OpcProtocol() : base(0, 0, Endian.BigEndianLsb) { } } @@ -67,8 +67,8 @@ /// /// 读数据协议 /// - [SpecialProtocalUnit] - public class ReadRequestOpcProtocal : ProtocalUnit + [SpecialProtocolUnit] + public class ReadRequestOpcProtocol : ProtocolUnit { /// /// 从对象的参数数组格式化 @@ -159,8 +159,8 @@ /// /// 写数据协议 /// - [SpecialProtocalUnit] - public class WriteRequestOpcProtocal : ProtocalUnit + [SpecialProtocolUnit] + public class WriteRequestOpcProtocol : ProtocolUnit { /// /// 从对象的参数数组格式化 diff --git a/Modbus.Net/Modbus.Net.OPC/OpcProtocalLinker.cs b/Modbus.Net/Modbus.Net.OPC/OpcProtocolLinker.cs similarity index 96% rename from Modbus.Net/Modbus.Net.OPC/OpcProtocalLinker.cs rename to Modbus.Net/Modbus.Net.OPC/OpcProtocolLinker.cs index d76b2cf..de7e595 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcProtocolLinker.cs @@ -6,7 +6,7 @@ namespace Modbus.Net.OPC /// /// Opc协议连接器 /// - public abstract class OpcProtocalLinker : ProtocalLinker + public abstract class OpcProtocolLinker : ProtocolLinker { /// /// 发送并接收数据 diff --git a/Modbus.Net/Modbus.Net.OPC/OpcUaProtocal.cs b/Modbus.Net/Modbus.Net.OPC/OpcUaProtocol.cs similarity index 76% rename from Modbus.Net/Modbus.Net.OPC/OpcUaProtocal.cs rename to Modbus.Net/Modbus.Net.OPC/OpcUaProtocol.cs index c80d15f..b18faca 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcUaProtocal.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcUaProtocol.cs @@ -5,7 +5,7 @@ namespace Modbus.Net.OPC /// /// Opc UA协议 /// - public class OpcUaProtocal : OpcProtocal + public class OpcUaProtocol : OpcProtocol { private readonly string _host; @@ -16,7 +16,7 @@ namespace Modbus.Net.OPC /// /// Opc UA服务地址 /// 是否开启正则匹配 - public OpcUaProtocal(string host, bool isRegexOn) + public OpcUaProtocol(string host, bool isRegexOn) { _host = host; _isRegexOn = isRegexOn; @@ -28,8 +28,8 @@ namespace Modbus.Net.OPC /// 是否连接成功 public override async Task ConnectAsync() { - ProtocalLinker = new OpcUaProtocalLinker(_host, _isRegexOn); - if (!await ProtocalLinker.ConnectAsync()) return false; + ProtocolLinker = new OpcUaProtocolLinker(_host, _isRegexOn); + if (!await ProtocolLinker.ConnectAsync()) return false; return true; } } diff --git a/Modbus.Net/Modbus.Net.OPC/OpcUaProtocalLinker.cs b/Modbus.Net/Modbus.Net.OPC/OpcUaProtocolLinker.cs similarity index 77% rename from Modbus.Net/Modbus.Net.OPC/OpcUaProtocalLinker.cs rename to Modbus.Net/Modbus.Net.OPC/OpcUaProtocolLinker.cs index d4ed0af..c565089 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcUaProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcUaProtocolLinker.cs @@ -5,13 +5,13 @@ namespace Modbus.Net.OPC /// /// Opc UA协议连接器 /// - public class OpcUaProtocalLinker : OpcProtocalLinker + public class OpcUaProtocolLinker : OpcProtocolLinker { /// /// 构造函数 /// /// 是否开启正则匹配 - public OpcUaProtocalLinker(bool isRegexOn) : this(ConfigurationManager.AppSettings["OpcUaHost"], isRegexOn) + public OpcUaProtocolLinker(bool isRegexOn) : this(ConfigurationManager.AppSettings["OpcUaHost"], isRegexOn) { } @@ -20,7 +20,7 @@ namespace Modbus.Net.OPC /// /// Opc UA服务地址 /// 是否开启正则匹配 - public OpcUaProtocalLinker(string host, bool isRegexOn) + public OpcUaProtocolLinker(string host, bool isRegexOn) { BaseConnector = OpcUaConnector.Instance(host, isRegexOn); } diff --git a/Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs b/Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs index e4db506..c1f4aae 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs @@ -12,7 +12,7 @@ /// 是否开启正则匹配 public OpcUaUtility(string connectionString, bool isRegexOn = false) : base(connectionString) { - Wrapper = new OpcUaProtocal(ConnectionString, isRegexOn); + Wrapper = new OpcUaProtocol(ConnectionString, isRegexOn); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.OPC/OpcUtility.cs b/Modbus.Net/Modbus.Net.OPC/OpcUtility.cs index c469572..e098b32 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcUtility.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcUtility.cs @@ -7,9 +7,9 @@ namespace Modbus.Net.OPC /// /// Opc通用Api入口 /// - public abstract class OpcUtility : BaseUtility, - PipeUnit, - ProtocalUnit>> + public abstract class OpcUtility : BaseUtility, + PipeUnit, + ProtocolUnit>> { /// /// 获取分隔符 @@ -60,7 +60,7 @@ namespace Modbus.Net.OPC var readRequestOpcInputStruct = new ReadRequestOpcInputStruct(startAddress.Split('\r'), split); var readRequestOpcOutputStruct = await - Wrapper.SendReceiveAsync(Wrapper[typeof(ReadRequestOpcProtocal)], + Wrapper.SendReceiveAsync(Wrapper[typeof(ReadRequestOpcProtocol)], readRequestOpcInputStruct); return readRequestOpcOutputStruct?.GetValue; } @@ -86,7 +86,7 @@ namespace Modbus.Net.OPC new WriteRequestOpcInputStruct(startAddress.Split('\r'), split, setContents[0]); var writeRequestOpcOutputStruct = await - Wrapper.SendReceiveAsync(Wrapper[typeof(WriteRequestOpcProtocal)], + Wrapper.SendReceiveAsync(Wrapper[typeof(WriteRequestOpcProtocol)], writeRequestOpcInputStruct); return writeRequestOpcOutputStruct?.WriteResult == true; } diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocal.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs similarity index 87% rename from Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocal.cs rename to Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs index 8311e38..52d45b3 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocal.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs @@ -6,7 +6,7 @@ namespace Modbus.Net.Siemens /// /// 西门子Ppi协议 /// - public class SiemensPpiProtocal : SiemensProtocal + public class SiemensPpiProtocol : SiemensProtocol { private readonly string _com; @@ -15,7 +15,7 @@ namespace Modbus.Net.Siemens /// /// 从站号 /// 主站号 - public SiemensPpiProtocal(byte slaveAddress, byte masterAddress) + public SiemensPpiProtocol(byte slaveAddress, byte masterAddress) : this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress) { } @@ -26,7 +26,7 @@ namespace Modbus.Net.Siemens /// 串口地址 /// 从站号 /// 主站号 - public SiemensPpiProtocal(string com, byte slaveAddress, byte masterAddress) + public SiemensPpiProtocol(string com, byte slaveAddress, byte masterAddress) : base(slaveAddress, masterAddress) { _com = com; @@ -49,7 +49,7 @@ namespace Modbus.Net.Siemens /// 从设备获取的字节流 public override async Task SendReceiveAsync(params object[] content) { - if (ProtocalLinker == null || !ProtocalLinker.IsConnected) + if (ProtocolLinker == null || !ProtocolLinker.IsConnected) await ConnectAsync(); return await base.SendReceiveAsync(Endian, content); } @@ -60,7 +60,7 @@ namespace Modbus.Net.Siemens /// 协议核心 /// 协议的参数 /// 设备返回的信息 - private async Task ForceSendReceiveAsync(ProtocalUnit unit, IInputStruct content) + private async Task ForceSendReceiveAsync(ProtocolUnit unit, IInputStruct content) { return await base.SendReceiveAsync(unit, content); } @@ -71,13 +71,13 @@ namespace Modbus.Net.Siemens /// 是否连接成功 public override async Task ConnectAsync() { - ProtocalLinker = new SiemensPpiProtocalLinker(_com, SlaveAddress); + ProtocolLinker = new SiemensPpiProtocolLinker(_com, SlaveAddress); var inputStruct = new ComCreateReferenceSiemensInputStruct(SlaveAddress, MasterAddress); var outputStruct = (await (await - ForceSendReceiveAsync(this[typeof(ComCreateReferenceSiemensProtocal)], + ForceSendReceiveAsync(this[typeof(ComCreateReferenceSiemensProtocol)], inputStruct)). - SendReceiveAsync(this[typeof(ComConfirmMessageSiemensProtocal)], answer => + SendReceiveAsync(this[typeof(ComConfirmMessageSiemensProtocol)], answer => new ComConfirmMessageSiemensInputStruct(SlaveAddress, MasterAddress) )).Unwrap(); diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocalLinker.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocolLinker.cs similarity index 85% rename from Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocalLinker.cs rename to Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocolLinker.cs index 5e04bd0..ae5dfb3 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocolLinker.cs @@ -1,4 +1,5 @@ -using System.IO.Ports; +using System.Collections.Generic; +using System.IO.Ports; using System.Threading; using System.Threading.Tasks; @@ -7,17 +8,17 @@ namespace Modbus.Net.Siemens /// /// 西门子Ppi协议连接器 /// - public class SiemensPpiProtocalLinker : ComProtocalLinker + public class SiemensPpiProtocolLinker : ComProtocolLinker { /// /// 构造函数 /// /// 串口地址 /// 从站号 - public SiemensPpiProtocalLinker(string com, int slaveAddress) + public SiemensPpiProtocolLinker(string com, int slaveAddress) : base(com, 9600, Parity.Even, StopBits.One, 8, slaveAddress) { - ((BaseConnector)BaseConnector).AddController(new FIFOController(500)); + ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection[] { new List { 5 }, new List { 6 }, new List { 11, 12 } }, 500)); } /// @@ -33,7 +34,7 @@ namespace Modbus.Net.Siemens var inputStruct2 = new ComConfirmMessageSiemensInputStruct(content[4], content[5]); var receiveBytes2 = await SendReceiveWithoutExtAndDecAsync( - new ComConfirmMessageSiemensProtocal().Format(inputStruct2)); + new ComConfirmMessageSiemensProtocol().Format(inputStruct2)); } var receiveBytes = await SendReceiveWithoutExtAndDecAsync(extBytes); if (content.Length > 6 && receiveBytes.Length == 1 && receiveBytes[0] == 0xe5) @@ -41,7 +42,7 @@ namespace Modbus.Net.Siemens var inputStruct2 = new ComConfirmMessageSiemensInputStruct(content[4], content[5]); var receiveBytes2 = await SendReceiveWithoutExtAndDecAsync( - new ComConfirmMessageSiemensProtocal().Format(inputStruct2)); + new ComConfirmMessageSiemensProtocol().Format(inputStruct2)); return BytesDecact(receiveBytes2); } return BytesDecact(receiveBytes); @@ -63,14 +64,14 @@ namespace Modbus.Net.Siemens var inputStruct2 = new ComConfirmMessageSiemensInputStruct(content[1], content[2]); ans = await SendReceiveWithoutExtAndDecAsync( - new ComConfirmMessageSiemensProtocal().Format(inputStruct2)); + new ComConfirmMessageSiemensProtocol().Format(inputStruct2)); } else { var inputStruct2 = new ComConfirmMessageSiemensInputStruct(content[4], content[5]); ans = await SendReceiveWithoutExtAndDecAsync( - new ComConfirmMessageSiemensProtocal().Format(inputStruct2)); + new ComConfirmMessageSiemensProtocol().Format(inputStruct2)); } } return ans; diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocal.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocol.cs similarity index 95% rename from Modbus.Net/Modbus.Net.Siemens/SiemensProtocal.cs rename to Modbus.Net/Modbus.Net.Siemens/SiemensProtocol.cs index b2f9954..0b4c182 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocal.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocol.cs @@ -105,14 +105,14 @@ namespace Modbus.Net.Siemens /// /// 西门子协议 /// - public abstract class SiemensProtocal : BaseProtocal + public abstract class SiemensProtocol : BaseProtocol { /// /// 构造函数 /// /// 从站号 /// 主站号 - protected SiemensProtocal(byte slaveAddress, byte masterAddress) + protected SiemensProtocol(byte slaveAddress, byte masterAddress) : base(slaveAddress, masterAddress, Endian.BigEndianLsb) { } @@ -146,8 +146,8 @@ namespace Modbus.Net.Siemens public byte ConfirmMessage { get; set; } } - [SpecialProtocalUnit] - internal class ComCreateReferenceSiemensProtocal : ProtocalUnit + [SpecialProtocolUnit] + internal class ComCreateReferenceSiemensProtocol : ProtocolUnit { public override byte[] Format(IInputStruct message) { @@ -201,8 +201,8 @@ namespace Modbus.Net.Siemens public ushort TsapDst { get; } } - [SpecialProtocalUnit] - internal class CreateReferenceSiemensProtocal : ProtocalUnit + [SpecialProtocolUnit] + internal class CreateReferenceSiemensProtocol : ProtocolUnit { public override byte[] Format(IInputStruct message) { @@ -308,8 +308,8 @@ namespace Modbus.Net.Siemens /// /// 串口消息确认协议 /// - [SpecialProtocalUnit] - public class ComConfirmMessageSiemensProtocal : ProtocalUnit + [SpecialProtocolUnit] + public class ComConfirmMessageSiemensProtocol : ProtocolUnit { /// /// 格式化 @@ -374,7 +374,7 @@ namespace Modbus.Net.Siemens public ushort MaxPdu { get; } } - internal class EstablishAssociationSiemensProtocal : ProtocalUnit + internal class EstablishAssociationSiemensProtocol : ProtocolUnit { public override byte[] Format(IInputStruct message) { @@ -533,7 +533,7 @@ namespace Modbus.Net.Siemens /// /// 读数据协议 /// - public class ReadRequestSiemensProtocal : ProtocalUnit + public class ReadRequestSiemensProtocol : ProtocolUnit { /// /// 格式化 @@ -688,7 +688,7 @@ namespace Modbus.Net.Siemens /// /// 写数据协议 /// - public class WriteRequestSiemensProtocal : ProtocalUnit + public class WriteRequestSiemensProtocol : ProtocolUnit { /// /// 格式化 @@ -780,7 +780,7 @@ namespace Modbus.Net.Siemens public TodClockStatus TodClockStatus { get; private set; } } - public class ReadTimeSiemensProtocal : ProtocalUnit + public class ReadTimeSiemensProtocol : ProtocolUnit { public override byte[] Format(IInputStruct message) { @@ -823,7 +823,7 @@ namespace Modbus.Net.Siemens public byte ErrCod { get;private set; } } - public class WriteTimeSiemensProtocal : ProtocalUnit + public class WriteTimeSiemensProtocol : ProtocolUnit { public override byte[] Format(IInputStruct message) { @@ -842,9 +842,9 @@ namespace Modbus.Net.Siemens /// /// 西门子通讯报错信息 /// - public class SiemensProtocalErrorException : ProtocalErrorException + public class SiemensProtocolErrorException : ProtocolErrorException { - private static readonly Dictionary ProtocalErrorDictionary = new Dictionary + private static readonly Dictionary ProtocolErrorDictionary = new Dictionary { {0x00, "No Error"}, {0x81, "Error in the application Id of the request"}, @@ -860,7 +860,7 @@ namespace Modbus.Net.Siemens {0xEF, "Layer 2 specific error"} }; - private static readonly Dictionary ProtocalErrorDetailDictionary = new Dictionary + private static readonly Dictionary ProtocolErrorDetailDictionary = new Dictionary { {0x8304, "Resource not available,\r\n there are no more resources available for application associations to be established" }, {0x8104, "Context is not supported:\r\n -Error in PDU structure\r\n -Unknown service" }, @@ -873,12 +873,12 @@ namespace Modbus.Net.Siemens /// /// 错误分类 /// 错误码 - public SiemensProtocalErrorException(int errCls, int errCod) - : base((ProtocalErrorDictionary.ContainsKey(errCls) - ? ProtocalErrorDictionary[errCls] + public SiemensProtocolErrorException(int errCls, int errCod) + : base((ProtocolErrorDictionary.ContainsKey(errCls) + ? ProtocolErrorDictionary[errCls] : "Unknown error") + " \r\n " + - (ProtocalErrorDetailDictionary.ContainsKey(errCls * 256 + errCod) - ? ProtocalErrorDetailDictionary[errCls * 256 + errCod] + (ProtocolErrorDetailDictionary.ContainsKey(errCls * 256 + errCod) + ? ProtocolErrorDetailDictionary[errCls * 256 + errCod] : "Unknown error detail")) { ErrorClass = errCls; diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocalLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocolLinkerBytesExtend.cs similarity index 95% rename from Modbus.Net/Modbus.Net.Siemens/SiemensProtocalLinkerBytesExtend.cs rename to Modbus.Net/Modbus.Net.Siemens/SiemensProtocolLinkerBytesExtend.cs index d5ae066..79433e1 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocalLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocolLinkerBytesExtend.cs @@ -5,7 +5,7 @@ namespace Modbus.Net.Siemens /// /// 西门子Tcp协议扩展 /// - public class SiemensTcpProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend + public class SiemensTcpProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend { /// /// 协议扩展,协议内容发送前调用 @@ -35,7 +35,7 @@ namespace Modbus.Net.Siemens /// /// 西门子Ppi协议扩展 /// - public class SiemensPpiProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend + public class SiemensPpiProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend { /// /// 协议扩展,协议内容发送前调用 diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocal.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs similarity index 88% rename from Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocal.cs rename to Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs index 5eac699..3e8ce00 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocal.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs @@ -6,7 +6,7 @@ namespace Modbus.Net.Siemens /// /// 西门子Tcp协议 /// - public class SiemensTcpProtocal : SiemensProtocal + public class SiemensTcpProtocol : SiemensProtocol { private readonly string _ip; private readonly ushort _maxCalled; @@ -27,7 +27,7 @@ namespace Modbus.Net.Siemens /// /// /// - public SiemensTcpProtocal(byte tdpuSize, ushort tsapSrc, ushort tsapDst, ushort maxCalling, ushort maxCalled, + public SiemensTcpProtocol(byte tdpuSize, ushort tsapSrc, ushort tsapDst, ushort maxCalling, ushort maxCalled, ushort maxPdu) : this(tdpuSize, tsapSrc, tsapDst, maxCalling, maxCalled, maxPdu, ConfigurationManager.AppSettings["IP"]) { @@ -43,7 +43,7 @@ namespace Modbus.Net.Siemens /// /// /// IP地址 - public SiemensTcpProtocal(byte tdpuSize, ushort tsapSrc, ushort tsapDst, ushort maxCalling, ushort maxCalled, + public SiemensTcpProtocol(byte tdpuSize, ushort tsapSrc, ushort tsapDst, ushort maxCalling, ushort maxCalled, ushort maxPdu, string ip) : this( tdpuSize, tsapSrc, tsapDst, maxCalling, maxCalled, maxPdu, ip, @@ -62,7 +62,7 @@ namespace Modbus.Net.Siemens /// /// IP地址 /// 端口 - public SiemensTcpProtocal(byte tdpuSize, ushort tsapSrc, ushort tsapDst, ushort maxCalling, ushort maxCalled, + public SiemensTcpProtocol(byte tdpuSize, ushort tsapSrc, ushort tsapDst, ushort maxCalling, ushort maxCalled, ushort maxPdu, string ip, int port) : base(0, 0) { _taspSrc = tsapSrc; @@ -93,7 +93,7 @@ namespace Modbus.Net.Siemens /// 返回的数据 public override async Task SendReceiveAsync(params object[] content) { - if (ProtocalLinker == null || !ProtocalLinker.IsConnected) + if (ProtocolLinker == null || !ProtocolLinker.IsConnected) await ConnectAsync(); return await base.SendReceiveAsync(Endian, content); } @@ -104,7 +104,7 @@ namespace Modbus.Net.Siemens /// 协议的核心 /// 协议的参数 /// 返回的数据 - public override PipeUnit SendReceive(ProtocalUnit unit, IInputStruct content) + public override PipeUnit SendReceive(ProtocolUnit unit, IInputStruct content) { return AsyncHelper.RunSync(() => SendReceiveAsync(unit, content)); } @@ -115,9 +115,9 @@ namespace Modbus.Net.Siemens /// 发送的数据 /// 协议的参数 /// 返回的数据 - public override async Task SendReceiveAsync(ProtocalUnit unit, IInputStruct content) + public override async Task SendReceiveAsync(ProtocolUnit unit, IInputStruct content) { - if (ProtocalLinker != null && ProtocalLinker.IsConnected) return await base.SendReceiveAsync(unit, content); + if (ProtocolLinker != null && ProtocolLinker.IsConnected) return await base.SendReceiveAsync(unit, content); if (_connectTryCount > 10) return null; return await @@ -131,7 +131,7 @@ namespace Modbus.Net.Siemens /// 发送的数据 /// 协议的参数 /// 返回的数据 - private async Task ForceSendReceiveAsync(ProtocalUnit unit, IInputStruct content) + private async Task ForceSendReceiveAsync(ProtocolUnit unit, IInputStruct content) { return await base.SendReceiveAsync(unit, content); } @@ -143,15 +143,15 @@ namespace Modbus.Net.Siemens public override async Task ConnectAsync() { _connectTryCount++; - ProtocalLinker = new SiemensTcpProtocalLinker(_ip, _port); - if (!await ProtocalLinker.ConnectAsync()) return false; + ProtocolLinker = new SiemensTcpProtocolLinker(_ip, _port); + if (!await ProtocolLinker.ConnectAsync()) return false; _connectTryCount = 0; var inputStruct = new CreateReferenceSiemensInputStruct(_tdpuSize, _taspSrc, _tsapDst); var outputStruct = //先建立连接,然后建立设备的引用 (await (await - ForceSendReceiveAsync(this[typeof(CreateReferenceSiemensProtocal)], inputStruct)).SendReceiveAsync( - this[typeof(EstablishAssociationSiemensProtocal)], answer => + ForceSendReceiveAsync(this[typeof(CreateReferenceSiemensProtocol)], inputStruct)).SendReceiveAsync( + this[typeof(EstablishAssociationSiemensProtocol)], answer => new EstablishAssociationSiemensInputStruct(0x0101, _maxCalling, _maxCalled, _maxPdu))).Unwrap(); diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocalLinker.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocolLinker.cs similarity index 78% rename from Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocalLinker.cs rename to Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocolLinker.cs index 87b7c6b..1ad2e3f 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocolLinker.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Configuration; namespace Modbus.Net.Siemens @@ -6,13 +7,13 @@ namespace Modbus.Net.Siemens /// /// 西门子Tcp协议连接器 /// - public class SiemensTcpProtocalLinker : TcpProtocalLinker + public class SiemensTcpProtocolLinker : TcpProtocolLinker { /// /// 构造函数 /// /// IP地址 - public SiemensTcpProtocalLinker(string ip) + public SiemensTcpProtocolLinker(string ip) : this(ip, int.Parse(ConfigurationManager.AppSettings["SiemensPort"] ?? "102")) { } @@ -22,10 +23,10 @@ namespace Modbus.Net.Siemens /// /// IP地址 /// 端口 - public SiemensTcpProtocalLinker(string ip, int port) + public SiemensTcpProtocolLinker(string ip, int port) : base(ip, port) { - ((BaseConnector)BaseConnector).AddController(new FIFOController(500)); + ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection[] { new List { 11, 12 } }, 500)); } /// @@ -48,10 +49,10 @@ namespace Modbus.Net.Siemens case 0x02: case 0x03: if (content[17] == 0x00 && content[18] == 0x00) return true; - throw new SiemensProtocalErrorException(content[17], content[18]); + throw new SiemensProtocolErrorException(content[17], content[18]); case 0x07: if (content[27] == 0x00 && content[28] == 0x00) return true; - throw new SiemensProtocalErrorException(content[27], content[28]); + throw new SiemensProtocolErrorException(content[27], content[28]); } return true; default: diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs index 4daf5c3..b3762fa 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs @@ -187,8 +187,8 @@ namespace Modbus.Net.Siemens case SiemensType.Ppi: { Wrapper = ConnectionString == null - ? new SiemensPpiProtocal(SlaveAddress, MasterAddress) - : new SiemensPpiProtocal(ConnectionString, SlaveAddress, MasterAddress); + ? new SiemensPpiProtocol(SlaveAddress, MasterAddress) + : new SiemensPpiProtocol(ConnectionString, SlaveAddress, MasterAddress); break; } //MPI @@ -200,11 +200,11 @@ namespace Modbus.Net.Siemens case SiemensType.Tcp: { Wrapper = ConnectionString == null - ? new SiemensTcpProtocal(_tdpuSize, _taspSrc, _tsapDst, _maxCalling, _maxCalled, _maxPdu) + ? new SiemensTcpProtocol(_tdpuSize, _taspSrc, _tsapDst, _maxCalling, _maxCalled, _maxPdu) : (ConnectionStringPort == null - ? new SiemensTcpProtocal(_tdpuSize, _taspSrc, _tsapDst, _maxCalling, _maxCalled, _maxPdu, + ? new SiemensTcpProtocol(_tdpuSize, _taspSrc, _tsapDst, _maxCalling, _maxCalled, _maxPdu, ConnectionString) - : new SiemensTcpProtocal(_tdpuSize, _taspSrc, _tsapDst, _maxCalling, _maxCalled, _maxPdu, + : new SiemensTcpProtocol(_tdpuSize, _taspSrc, _tsapDst, _maxCalling, _maxCalled, _maxPdu, ConnectionStringIp, ConnectionStringPort.Value)); break; } @@ -236,7 +236,7 @@ namespace Modbus.Net.Siemens var readRequestSiemensOutputStruct = await Wrapper.SendReceiveAsync( - Wrapper[typeof(ReadRequestSiemensProtocal)], + Wrapper[typeof(ReadRequestSiemensProtocol)], readRequestSiemensInputStruct); return readRequestSiemensOutputStruct?.GetValue; } @@ -262,7 +262,7 @@ namespace Modbus.Net.Siemens var writeRequestSiemensOutputStruct = await Wrapper.SendReceiveAsync( - Wrapper[typeof(WriteRequestSiemensProtocal)], + Wrapper[typeof(WriteRequestSiemensProtocol)], writeRequestSiemensInputStruct); return writeRequestSiemensOutputStruct?.AccessResult == SiemensAccessResult.NoError; } diff --git a/Modbus.Net/Modbus.Net/AddressCombiner.cs b/Modbus.Net/Modbus.Net/AddressCombiner.cs index 5c66dde..1d4c243 100644 --- a/Modbus.Net/Modbus.Net/AddressCombiner.cs +++ b/Modbus.Net/Modbus.Net/AddressCombiner.cs @@ -76,7 +76,7 @@ namespace Modbus.Net //按从小到大的顺序对地址进行排序 var groupedAddresses = from address in addresses orderby - AddressHelper.GetProtocalCoordinate(address.Address, address.SubAddress, + AddressHelper.GetProtocolCoordinate(address.Address, address.SubAddress, AddressTranslator.GetAreaByteLength(address.Area)) group address by address.Area into grouped @@ -97,42 +97,42 @@ namespace Modbus.Net var orderedAddresses = groupedAddress.OrderBy( address => - AddressHelper.GetProtocalCoordinate(address.Address, address.SubAddress, + AddressHelper.GetProtocolCoordinate(address.Address, address.SubAddress, AddressTranslator.GetAreaByteLength(address.Area))); foreach (var address in orderedAddresses) { //第一次进入时直接压入地址 if (initNum < 0) { - initNum = AddressHelper.GetProtocalCoordinate(address.Address, address.SubAddress, + initNum = AddressHelper.GetProtocolCoordinate(address.Address, address.SubAddress, AddressTranslator.GetAreaByteLength(address.Area)); originalAddresses.Add(address); } else { //如果当前地址小于已经记录的地址域,表示这个地址的开始已经记录过了 - if (AddressHelper.GetProtocalCoordinate(address.Address, address.SubAddress, + if (AddressHelper.GetProtocolCoordinate(address.Address, address.SubAddress, AddressTranslator.GetAreaByteLength(address.Area)) < - AddressHelper.GetProtocalCoordinateNextPosition(preNum, + AddressHelper.GetProtocolCoordinateNextPosition(preNum, preType, AddressTranslator.GetAreaByteLength(address.Area))) { originalAddresses.Add(address); //如果当前地址的末尾被记录,表示地址被记录的地址域覆盖,这个地址没有记录的必要 - if (AddressHelper.GetProtocalCoordinateNextPosition( - AddressHelper.GetProtocalCoordinate(address.Address, address.SubAddress, + if (AddressHelper.GetProtocolCoordinateNextPosition( + AddressHelper.GetProtocolCoordinate(address.Address, address.SubAddress, AddressTranslator.GetAreaByteLength(address.Area)), address.DataType, AddressTranslator.GetAreaByteLength(address.Area)) <= - AddressHelper.GetProtocalCoordinateNextPosition(preNum, + AddressHelper.GetProtocolCoordinateNextPosition(preNum, preType, AddressTranslator.GetAreaByteLength(address.Area))) continue; } //如果当前地址大于记录的地址域的开头,则表示地址已经不连续了 - else if (AddressHelper.GetProtocalCoordinate(address.Address, address.SubAddress, + else if (AddressHelper.GetProtocolCoordinate(address.Address, address.SubAddress, AddressTranslator.GetAreaByteLength(address.Area)) > - AddressHelper.GetProtocalCoordinateNextPosition(preNum, + AddressHelper.GetProtocolCoordinateNextPosition(preNum, preType, AddressTranslator.GetAreaByteLength(address.Area))) { @@ -144,7 +144,7 @@ namespace Modbus.Net GetCount = (int) Math.Ceiling( - AddressHelper.MapProtocalGetCountToAbstractByteCount( + AddressHelper.MapProtocolGetCountToAbstractByteCount( preNum - (int) Math.Floor(initNum), AddressTranslator.GetAreaByteLength(address.Area), BigEndianValueHelper.Instance.ByteLength[preType.FullName])), @@ -162,7 +162,7 @@ namespace Modbus.Net } } //把当前地址变为上一个地址 - preNum = AddressHelper.GetProtocalCoordinate(address.Address, address.SubAddress, + preNum = AddressHelper.GetProtocolCoordinate(address.Address, address.SubAddress, AddressTranslator.GetAreaByteLength(address.Area)); preType = address.DataType; } @@ -174,7 +174,7 @@ namespace Modbus.Net GetCount = (int) Math.Ceiling( - AddressHelper.MapProtocalGetCountToAbstractByteCount( + AddressHelper.MapProtocolGetCountToAbstractByteCount( preNum - (int) Math.Floor(initNum), AddressTranslator.GetAreaByteLength(area), BigEndianValueHelper.Instance.ByteLength[preType.FullName])), DataType = typeof(byte), @@ -333,7 +333,7 @@ namespace Modbus.Net EndUnit = continusAddress, GapNumber = (int) - Math.Ceiling(AddressHelper.MapProtocalCoordinateToAbstractCoordinate( + Math.Ceiling(AddressHelper.MapProtocolCoordinateToAbstractCoordinate( continusAddress.Address, preCommunicationUnit.Address, AddressTranslator.GetAreaByteLength(continusAddress.Area)) - preCommunicationUnit.GetCount * diff --git a/Modbus.Net/Modbus.Net/AddressHelper.cs b/Modbus.Net/Modbus.Net/AddressHelper.cs index ce16ee6..0d3673f 100644 --- a/Modbus.Net/Modbus.Net/AddressHelper.cs +++ b/Modbus.Net/Modbus.Net/AddressHelper.cs @@ -14,7 +14,7 @@ namespace Modbus.Net /// 起始地址 /// 协议坐标单个地址的字节长度 /// - public static double MapAbstractCoordinateToProtocalCoordinate(double abstractAddress, int startAddress, + public static double MapAbstractCoordinateToProtocolCoordinate(double abstractAddress, int startAddress, double byteLength) { return abstractAddress / byteLength + startAddress; @@ -27,7 +27,7 @@ namespace Modbus.Net /// 起始地址 /// 协议坐标单个地址的字节长度 /// - public static double MapProtocalCoordinateToAbstractCoordinate(double protocalAddress, int startAddress, + public static double MapProtocolCoordinateToAbstractCoordinate(double protocalAddress, int startAddress, double byteLength) { return (protocalAddress - startAddress) * byteLength; @@ -40,7 +40,7 @@ namespace Modbus.Net /// 协议坐标区域与字节之间的防缩倍数 /// 协议坐标单个地址的字节长度 /// - public static double MapProtocalGetCountToAbstractByteCount(double protocalGetCount, double areaLength, + public static double MapProtocolGetCountToAbstractByteCount(double protocalGetCount, double areaLength, double byteLength) { return protocalGetCount * areaLength + byteLength; @@ -53,7 +53,7 @@ namespace Modbus.Net /// 子地址 /// 协议坐标单个地址的字节长度 /// - public static double GetProtocalCoordinate(int address, int subAddress, double byteLength) + public static double GetProtocolCoordinate(int address, int subAddress, double byteLength) { return address + subAddress * (0.125 / byteLength); } @@ -76,7 +76,7 @@ namespace Modbus.Net /// 间隔的数据类型 /// 协议坐标单个地址的字节长度 /// - public static double GetProtocalCoordinateNextPosition(double protocalAddress, Type nextPositionBetweenType, + public static double GetProtocolCoordinateNextPosition(double protocalAddress, Type nextPositionBetweenType, double byteLength) { return protocalAddress + diff --git a/Modbus.Net/Modbus.Net/BaseController.cs b/Modbus.Net/Modbus.Net/BaseController.cs index 2b75d76..c870ad8 100644 --- a/Modbus.Net/Modbus.Net/BaseController.cs +++ b/Modbus.Net/Modbus.Net/BaseController.cs @@ -104,71 +104,4 @@ namespace Modbus.Net public EventWaitHandle ReceiveMutex { get; set; } } - - public class FIFOController : BaseController - { - private MessageWaitingDef _currentSendingPos; - - public int AcquireTime { get; } - - public FIFOController(int acquireTime) - { - AcquireTime = acquireTime; - } - - protected override void SendingMessageControlInner() - { - try - { - while (!_taskCancel) - { - if (AcquireTime > 0) - { - Thread.Sleep(AcquireTime); - } - lock (WaitingMessages) - { - if (_currentSendingPos == null) - { - if (WaitingMessages.Count > 0) - { - _currentSendingPos = WaitingMessages.First(); - } - } - if (_currentSendingPos != null) - { - _currentSendingPos.SendMutex.Set(); - if (WaitingMessages.Count <= 1) - { - _currentSendingPos = null; - } - else - { - _currentSendingPos = WaitingMessages[WaitingMessages.IndexOf(_currentSendingPos) + 1]; - } - } - } - } - } - catch (ObjectDisposedException) - { - //ignore - } - catch(Exception e) - { - Log.Error(e, "Controller thorws exception"); - } - - } - - protected override string GetKeyFromMessage(byte[] message) - { - return null; - } - - protected override MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage) - { - return WaitingMessages.FirstOrDefault(); - } - } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/BaseMachine.cs b/Modbus.Net/Modbus.Net/BaseMachine.cs index f8d52cc..fe58d17 100644 --- a/Modbus.Net/Modbus.Net/BaseMachine.cs +++ b/Modbus.Net/Modbus.Net/BaseMachine.cs @@ -264,7 +264,7 @@ namespace Modbus.Net foreach (var address in communicateAddress.OriginalAddresses) { //字节坐标的位置 - var localPos = AddressHelper.MapProtocalCoordinateToAbstractCoordinate(address.Address, + var localPos = AddressHelper.MapProtocolCoordinateToAbstractCoordinate(address.Address, communicateAddress.Address, AddressTranslator.GetAreaByteLength(communicateAddress.Area)) + address.SubAddress * 0.125; @@ -469,7 +469,7 @@ namespace Modbus.Net { //字节坐标地址 var byteCount = - AddressHelper.MapProtocalGetCountToAbstractByteCount( + AddressHelper.MapProtocolGetCountToAbstractByteCount( addressUnit.Address - communicateAddress.Address + addressUnit.SubAddress * 0.125 / AddressTranslator.GetAreaByteLength(communicateAddress.Area), diff --git a/Modbus.Net/Modbus.Net/BaseProtocal.cs b/Modbus.Net/Modbus.Net/BaseProtocol.cs similarity index 77% rename from Modbus.Net/Modbus.Net/BaseProtocal.cs rename to Modbus.Net/Modbus.Net/BaseProtocol.cs index e890cec..369f59c 100644 --- a/Modbus.Net/Modbus.Net/BaseProtocal.cs +++ b/Modbus.Net/Modbus.Net/BaseProtocol.cs @@ -9,12 +9,12 @@ namespace Modbus.Net /// /// 基本协议 /// - public abstract class BaseProtocal : BaseProtocal + public abstract class BaseProtocol : BaseProtocol { /// /// 构造器 /// - protected BaseProtocal(byte slaveAddress, byte masterAddress, Endian endian) + protected BaseProtocol(byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian) { } @@ -30,7 +30,7 @@ namespace Modbus.Net { var pipeUnit = new PipeUnit( - ProtocalLinker); + ProtocolLinker); return await pipeUnit.SendReceiveAsync(Endian, paramOut => content); } return null; @@ -43,11 +43,11 @@ namespace Modbus.Net /// 输入信息的结构化描述 /// 输出信息的结构化描述 public override async Task - SendReceiveAsync(ProtocalUnit unit, IInputStruct content) + SendReceiveAsync(ProtocolUnit unit, IInputStruct content) { if (content != null) { - var pipeUnit = new PipeUnit(ProtocalLinker); + var pipeUnit = new PipeUnit(ProtocolLinker); return await pipeUnit.SendReceiveAsync(unit, paramOut => content); } return null; @@ -57,19 +57,19 @@ namespace Modbus.Net /// /// 基本协议 /// - public abstract class BaseProtocal : - IProtocal - where TProtocalUnit : class, IProtocalFormatting + public abstract class BaseProtocol : + IProtocol + where TProtocolUnit : class, IProtocolFormatting where TParamOut : class - where TPipeUnit : PipeUnit, TProtocalUnit> + where TPipeUnit : PipeUnit, TProtocolUnit> { /// /// 构造器 /// - protected BaseProtocal(byte slaveAddress, byte masterAddress, Endian endian) + protected BaseProtocol(byte slaveAddress, byte masterAddress, Endian endian) { Endian = endian; - Protocals = new Dictionary(); + Protocols = new Dictionary(); SlaveAddress = slaveAddress; MasterAddress = masterAddress; } @@ -92,45 +92,45 @@ namespace Modbus.Net /// /// 协议集合 /// - protected Dictionary Protocals { get; } + protected Dictionary Protocols { get; } /// /// 协议索引器,这是一个懒加载协议,当字典中不存在协议时自动加载协议,否则调用已经加载的协议 /// /// 协议的类的GetType /// 协议的实例 - public TProtocalUnit this[Type type] + public TProtocolUnit this[Type type] { get { var protocalName = type.FullName; - TProtocalUnit protocalUnitReturn = null; - lock (Protocals) + TProtocolUnit protocalUnitReturn = null; + lock (Protocols) { - if (Protocals.ContainsKey(protocalName)) + if (Protocols.ContainsKey(protocalName)) { - protocalUnitReturn = Protocals[protocalName]; + protocalUnitReturn = Protocols[protocalName]; } else { //自动寻找存在的协议并将其加载 var protocalUnit = Activator.CreateInstance(type.GetTypeInfo().Assembly - .GetType(protocalName)) as TProtocalUnit; + .GetType(protocalName)) as TProtocolUnit; if (protocalUnit == null) - throw new InvalidCastException($"No ProtocalUnit {nameof(TProtocalUnit)} implemented"); + throw new InvalidCastException($"No ProtocolUnit {nameof(TProtocolUnit)} implemented"); protocalUnit.Endian = Endian; Register(protocalUnit); } } - return protocalUnitReturn ?? Protocals[protocalName]; + return protocalUnitReturn ?? Protocols[protocalName]; } } /// /// 协议的连接器 /// - public IProtocalLinker ProtocalLinker { get; protected set; } + public IProtocolLinker ProtocolLinker { get; protected set; } /// /// 协议连接开始 @@ -144,8 +144,8 @@ namespace Modbus.Net /// public virtual bool Disconnect() { - if (ProtocalLinker != null) - return ProtocalLinker.Disconnect(); + if (ProtocolLinker != null) + return ProtocolLinker.Disconnect(); return false; } @@ -156,7 +156,7 @@ namespace Modbus.Net /// 输入信息的结构化描述 /// 输出信息的结构化描述 public virtual TPipeUnit SendReceive( - TProtocalUnit unit, IInputStruct content) + TProtocolUnit unit, IInputStruct content) { return AsyncHelper.RunSync(() => SendReceiveAsync(unit, content)); } @@ -168,13 +168,13 @@ namespace Modbus.Net /// 输入信息的结构化描述 /// 输出信息的结构化描述 public virtual async Task - SendReceiveAsync(TProtocalUnit unit, IInputStruct content) + SendReceiveAsync(TProtocolUnit unit, IInputStruct content) { if (content != null) { var pipeUnit = - new PipeUnit, TProtocalUnit>( - ProtocalLinker); + new PipeUnit, TProtocolUnit>( + ProtocolLinker); return await pipeUnit.SendReceiveAsync(unit, paramOut => content) as TPipeUnit; } return null; @@ -207,7 +207,7 @@ namespace Modbus.Net /// 输入信息的结构化描述 /// 输出信息的结构化描述 /// IOutputStruct的具体类型 - public virtual T SendReceive(TProtocalUnit unit, IInputStruct content) where T : class, IOutputStruct + public virtual T SendReceive(TProtocolUnit unit, IInputStruct content) where T : class, IOutputStruct { return AsyncHelper.RunSync(() => SendReceiveAsync(unit, content)); } @@ -219,7 +219,7 @@ namespace Modbus.Net /// 输入信息的结构化描述 /// 输出信息的结构化描述 /// IOutputStruct的具体类型 - public virtual async Task SendReceiveAsync(TProtocalUnit unit, IInputStruct content) + public virtual async Task SendReceiveAsync(TProtocolUnit unit, IInputStruct content) where T : class, IOutputStruct { return (await SendReceiveAsync(unit, content)).Unwrap(); @@ -228,11 +228,11 @@ namespace Modbus.Net /// /// 注册一个协议 /// - /// 需要注册的协议 - protected void Register(TProtocalUnit linkProtocal) + /// 需要注册的协议 + protected void Register(TProtocolUnit linkProtocol) { - if (linkProtocal == null) return; - Protocals.Add(linkProtocal.GetType().FullName, linkProtocal); + if (linkProtocol == null) return; + Protocols.Add(linkProtocol.GetType().FullName, linkProtocol); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/BaseUtility.cs b/Modbus.Net/Modbus.Net/BaseUtility.cs index bf3cd78..60616f7 100644 --- a/Modbus.Net/Modbus.Net/BaseUtility.cs +++ b/Modbus.Net/Modbus.Net/BaseUtility.cs @@ -30,7 +30,7 @@ namespace Modbus.Net /// /// 基础Api入口 /// - public abstract class BaseUtility : BaseUtility + public abstract class BaseUtility : BaseUtility { /// /// 构造器 @@ -43,14 +43,14 @@ namespace Modbus.Net /// /// 基础Api入口 /// - public abstract class BaseUtility : IUtilityProperty, IUtilityMethodData - where TProtocalUnit : class, IProtocalFormatting where TParamOut : class - where TPipeUnit : PipeUnit, TProtocalUnit> + public abstract class BaseUtility : IUtilityProperty, IUtilityMethodData + where TProtocolUnit : class, IProtocolFormatting where TParamOut : class + where TPipeUnit : PipeUnit, TProtocolUnit> { /// /// 协议收发主体 /// - protected IProtocal Wrapper; + protected IProtocol Wrapper; /// /// 构造器 @@ -237,13 +237,13 @@ namespace Modbus.Net /// /// 设备是否已经连接 /// - public bool IsConnected => Wrapper?.ProtocalLinker != null && Wrapper.ProtocalLinker.IsConnected; + public bool IsConnected => Wrapper?.ProtocolLinker != null && Wrapper.ProtocolLinker.IsConnected; /// /// 标识设备的连接关键字 /// public string ConnectionToken - => Wrapper?.ProtocalLinker == null ? ConnectionString : Wrapper.ProtocalLinker.ConnectionToken; + => Wrapper?.ProtocolLinker == null ? ConnectionString : Wrapper.ProtocolLinker.ConnectionToken; /// /// 地址翻译器 diff --git a/Modbus.Net/Modbus.Net/ComProtocalLinker.cs b/Modbus.Net/Modbus.Net/ComProtocalLinker.cs index 98c7c89..2c266c3 100644 --- a/Modbus.Net/Modbus.Net/ComProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net/ComProtocalLinker.cs @@ -6,7 +6,7 @@ namespace Modbus.Net /// /// 串口连接对象 /// - public abstract class ComProtocalLinker : ProtocalLinker + public abstract class ComProtocolLinker : ProtocolLinker { /// /// 构造器 @@ -16,7 +16,7 @@ namespace Modbus.Net /// 停止位 /// 数据位 /// 从站地址 - protected ComProtocalLinker(int baudRate, Parity parity, StopBits stopBits, int dataBits, int slaveAddress) + protected ComProtocolLinker(int baudRate, Parity parity, StopBits stopBits, int dataBits, int slaveAddress) : this(ConfigurationManager.AppSettings["COM"], baudRate, parity, stopBits, dataBits, slaveAddress) { } @@ -30,7 +30,7 @@ namespace Modbus.Net /// 停止位 /// 数据位 /// 从站地址 - protected ComProtocalLinker(string com, int baudRate, Parity parity, StopBits stopBits, int dataBits, + protected ComProtocolLinker(string com, int baudRate, Parity parity, StopBits stopBits, int dataBits, int slaveAddress) : this( com, baudRate, parity, stopBits, dataBits, @@ -48,7 +48,7 @@ namespace Modbus.Net /// 数据位 /// 超时时间 /// 从站地址 - protected ComProtocalLinker(string com, int baudRate, Parity parity, StopBits stopBits, int dataBits, + protected ComProtocolLinker(string com, int baudRate, Parity parity, StopBits stopBits, int dataBits, int connectionTimeout, int slaveAddress) { BaseConnector = new ComConnector(com + ":" + slaveAddress, baudRate, parity, stopBits, dataBits, diff --git a/Modbus.Net/Modbus.Net/FifoController.cs b/Modbus.Net/Modbus.Net/FifoController.cs new file mode 100644 index 0000000..8989c2a --- /dev/null +++ b/Modbus.Net/Modbus.Net/FifoController.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Serilog; + +namespace Modbus.Net +{ + public class FifoController : BaseController + { + private MessageWaitingDef _currentSendingPos; + + public int AcquireTime { get; } + + public FifoController(int acquireTime) + { + AcquireTime = acquireTime; + } + + protected override void SendingMessageControlInner() + { + try + { + while (!_taskCancel) + { + if (AcquireTime > 0) + { + Thread.Sleep(AcquireTime); + } + lock (WaitingMessages) + { + if (_currentSendingPos == null) + { + if (WaitingMessages.Count > 0) + { + _currentSendingPos = WaitingMessages.First(); + } + } + if (_currentSendingPos != null) + { + _currentSendingPos.SendMutex.Set(); + if (WaitingMessages.Count <= 1) + { + _currentSendingPos = null; + } + else + { + _currentSendingPos = WaitingMessages[WaitingMessages.IndexOf(_currentSendingPos) + 1]; + } + } + } + } + } + catch (ObjectDisposedException) + { + //ignore + } + catch (Exception e) + { + Log.Error(e, "Controller throws exception"); + } + + } + + protected override string GetKeyFromMessage(byte[] message) + { + return null; + } + + protected override MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage) + { + return WaitingMessages.FirstOrDefault(); + } + } +} diff --git a/Modbus.Net/Modbus.Net/IProtocal.cs b/Modbus.Net/Modbus.Net/IProtocol.cs similarity index 82% rename from Modbus.Net/Modbus.Net/IProtocal.cs rename to Modbus.Net/Modbus.Net/IProtocol.cs index 266c10e..376e0d3 100644 --- a/Modbus.Net/Modbus.Net/IProtocal.cs +++ b/Modbus.Net/Modbus.Net/IProtocol.cs @@ -8,23 +8,23 @@ namespace Modbus.Net /// /// 向Connector传入的类型 /// 从Connector返回的类型 - /// 协议单元的类型 - public interface IProtocal - where TProtocalUnit : class, IProtocalFormatting + /// 协议单元的类型 + public interface IProtocol + where TProtocolUnit : class, IProtocolFormatting where TParamOut : class - where TPipeUnit : PipeUnit, TProtocalUnit> + where TPipeUnit : PipeUnit, TProtocolUnit> { /// /// 协议的连接器 /// - IProtocalLinker ProtocalLinker { get; } + IProtocolLinker ProtocolLinker { get; } /// /// 协议索引器,这是一个懒加载协议,当字典中不存在协议时自动加载协议,否则调用已经加载的协议 /// /// 协议的类的GetType /// 协议的实例 - TProtocalUnit this[Type type] { get; } + TProtocolUnit this[Type type] { get; } /// /// 协议连接开始 @@ -58,7 +58,7 @@ namespace Modbus.Net /// 协议的实例 /// 输入信息的结构化描述 /// 输出信息的结构化描述 - TPipeUnit SendReceive(TProtocalUnit unit, IInputStruct content); + TPipeUnit SendReceive(TProtocolUnit unit, IInputStruct content); /// /// 发送协议,通过传入需要使用的协议内容和输入结构 @@ -66,7 +66,7 @@ namespace Modbus.Net /// 协议的实例 /// 输入信息的结构化描述 /// 输出信息的结构化描述 - Task SendReceiveAsync(TProtocalUnit unit, IInputStruct content); + Task SendReceiveAsync(TProtocolUnit unit, IInputStruct content); /// /// 发送协议,通过传入需要使用的协议内容和输入结构 @@ -76,7 +76,7 @@ namespace Modbus.Net /// 输出信息的结构化描述 /// IOutputStruct的具体类型 T SendReceive( - TProtocalUnit unit, IInputStruct content) where T : class, IOutputStruct; + TProtocolUnit unit, IInputStruct content) where T : class, IOutputStruct; /// /// 发送协议,通过传入需要使用的协议内容和输入结构 @@ -86,6 +86,6 @@ namespace Modbus.Net /// 输出信息的结构化描述 /// IOutputStruct的具体类型 Task SendReceiveAsync( - TProtocalUnit unit, IInputStruct content) where T : class, IOutputStruct; + TProtocolUnit unit, IInputStruct content) where T : class, IOutputStruct; } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/IProtocalFormatting.cs b/Modbus.Net/Modbus.Net/IProtocolFormatting.cs similarity index 93% rename from Modbus.Net/Modbus.Net/IProtocalFormatting.cs rename to Modbus.Net/Modbus.Net/IProtocolFormatting.cs index d156e00..38c85f7 100644 --- a/Modbus.Net/Modbus.Net/IProtocalFormatting.cs +++ b/Modbus.Net/Modbus.Net/IProtocolFormatting.cs @@ -3,7 +3,7 @@ /// /// 协议转换的接口 /// - public interface IProtocalFormatting : IProtocalFormatting + public interface IProtocolFormatting : IProtocolFormatting { } @@ -12,7 +12,7 @@ /// /// 向Connector传入的数据类型 /// 从Connector返回的数据类型 - public interface IProtocalFormatting + public interface IProtocolFormatting { /// /// 是否为小端格式 diff --git a/Modbus.Net/Modbus.Net/IProtocalLinker.cs b/Modbus.Net/Modbus.Net/IProtocolLinker.cs similarity index 97% rename from Modbus.Net/Modbus.Net/IProtocalLinker.cs rename to Modbus.Net/Modbus.Net/IProtocolLinker.cs index 000ff06..867dde9 100644 --- a/Modbus.Net/Modbus.Net/IProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net/IProtocolLinker.cs @@ -7,7 +7,7 @@ namespace Modbus.Net /// /// 向Connector传入的数据类型 /// 从Connector返回的数据类型 - public interface IProtocalLinker + public interface IProtocolLinker { /// /// 通讯字符串 diff --git a/Modbus.Net/Modbus.Net/IProtocalLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net/IProtocolLinkerBytesExtend.cs similarity index 85% rename from Modbus.Net/Modbus.Net/IProtocalLinkerBytesExtend.cs rename to Modbus.Net/Modbus.Net/IProtocolLinkerBytesExtend.cs index ef2af9d..ed89694 100644 --- a/Modbus.Net/Modbus.Net/IProtocalLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net/IProtocolLinkerBytesExtend.cs @@ -3,14 +3,14 @@ /// /// 协议字节伸缩 /// - public interface IProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend + public interface IProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend { } /// /// 协议字节伸缩 /// - public interface IProtocalLinkerBytesExtend + public interface IProtocolLinkerBytesExtend { /// /// 协议扩展,协议内容发送前调用 diff --git a/Modbus.Net/Modbus.Net/MatchController.cs b/Modbus.Net/Modbus.Net/MatchController.cs new file mode 100644 index 0000000..883abc7 --- /dev/null +++ b/Modbus.Net/Modbus.Net/MatchController.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Serilog; + +namespace Modbus.Net +{ + public class MatchController : BaseController + { + private MessageWaitingDef _currentSendingPos; + + public int AcquireTime { get; } + + protected ICollection[] KeyMatches { get; } + + public MatchController(ICollection[] keyMatches, int acquireTime) + { + KeyMatches = keyMatches; + AcquireTime = acquireTime; + } + + protected override void SendingMessageControlInner() + { + try + { + while (!_taskCancel) + { + if (AcquireTime > 0) + { + Thread.Sleep(AcquireTime); + } + lock (WaitingMessages) + { + if (_currentSendingPos == null) + { + if (WaitingMessages.Count > 0) + { + _currentSendingPos = WaitingMessages.First(); + } + } + if (_currentSendingPos != null) + { + _currentSendingPos.SendMutex.Set(); + if (WaitingMessages.Count <= 1) + { + _currentSendingPos = null; + } + else + { + _currentSendingPos = WaitingMessages[WaitingMessages.IndexOf(_currentSendingPos) + 1]; + } + } + } + } + } + catch (ObjectDisposedException) + { + //ignore + } + catch (Exception e) + { + Log.Error(e, "Controller throws exception"); + } + + } + + protected override string GetKeyFromMessage(byte[] message) + { + string ans = ""; + foreach (var matchPoses in KeyMatches) + { + int tmpCount = 0; + foreach (var matchPos in matchPoses) + { + tmpCount = tmpCount * 256 + message[matchPos]; + } + ans += tmpCount + " "; + } + return ans; + } + + protected override MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage) + { + var returnKey = GetKeyFromMessage(receiveMessage); + return WaitingMessages.FirstOrDefault(p=>p.Key == returnKey); + } + } +} diff --git a/Modbus.Net/Modbus.Net/PipeUnit.cs b/Modbus.Net/Modbus.Net/PipeUnit.cs index 21a36c7..d89c95c 100644 --- a/Modbus.Net/Modbus.Net/PipeUnit.cs +++ b/Modbus.Net/Modbus.Net/PipeUnit.cs @@ -8,13 +8,13 @@ namespace Modbus.Net /// /// 管道单元 /// - public class PipeUnit : PipeUnit, ProtocalUnit> + public class PipeUnit : PipeUnit, ProtocolUnit> { /// /// 构造函数 /// /// 连接器 - public PipeUnit(IProtocalLinker protocalLinker) : base(protocalLinker) + public PipeUnit(IProtocolLinker protocalLinker) : base(protocalLinker) { } @@ -26,7 +26,7 @@ namespace Modbus.Net /// 协议单元 /// 传递给输入结构的参数 /// 上次的管道是否成功执行 - protected PipeUnit(IProtocalLinker protocalLinker, ProtocalUnit protocalUnit, byte[] parameters, + protected PipeUnit(IProtocolLinker protocalLinker, ProtocolUnit protocalUnit, byte[] parameters, bool success) : base(protocalLinker, protocalUnit, parameters, success) { } @@ -42,12 +42,12 @@ namespace Modbus.Net if (Success) { var content = inputStructCreator.Invoke(ReturnParams); - if (ProtocalLinker != null) - return new PipeUnit(ProtocalLinker, null, - await ProtocalLinker.SendReceiveAsync(ProtocalUnit.TranslateContent(endian, content)), + if (ProtocolLinker != null) + return new PipeUnit(ProtocolLinker, null, + await ProtocolLinker.SendReceiveAsync(ProtocolUnit.TranslateContent(endian, content)), true); } - return new PipeUnit(ProtocalLinker, null, ReturnParams, false); + return new PipeUnit(ProtocolLinker, null, ReturnParams, false); } /// @@ -57,14 +57,14 @@ namespace Modbus.Net /// 构造输入结构的函数 /// 发送完成之后新的管道实例 public new async Task SendReceiveAsync( - ProtocalUnit unit, + ProtocolUnit unit, Func inputStructCreator) { var receiveContent = await SendReceiveAsyncParamOut(unit, inputStructCreator); if (receiveContent != null) - return new PipeUnit(ProtocalLinker, unit, + return new PipeUnit(ProtocolLinker, unit, receiveContent, true); - return new PipeUnit(ProtocalLinker, unit, ReturnParams, + return new PipeUnit(ProtocolLinker, unit, ReturnParams, false); } @@ -83,18 +83,18 @@ namespace Modbus.Net /// /// 输入参数 /// 输出参数 - /// 连接器 - /// 协议单元 - public class PipeUnit - where TProtocalUnit : class, IProtocalFormatting - where TProtocalLinker : class, IProtocalLinker + /// 连接器 + /// 协议单元 + public class PipeUnit + where TProtocolUnit : class, IProtocolFormatting + where TProtocolLinker : class, IProtocolLinker where TParamOut : class { /// /// 构造函数 /// /// 连接器 - public PipeUnit(TProtocalLinker protocalLinker) : this(protocalLinker, null, null, true) + public PipeUnit(TProtocolLinker protocalLinker) : this(protocalLinker, null, null, true) { } @@ -106,10 +106,10 @@ namespace Modbus.Net /// 协议单元 /// 输入参数 /// 上一次管道结果是否成功 - protected PipeUnit(TProtocalLinker protocalLinker, TProtocalUnit protocalUnit, TParamOut parameters, bool success) + protected PipeUnit(TProtocolLinker protocalLinker, TProtocolUnit protocalUnit, TParamOut parameters, bool success) { - ProtocalLinker = protocalLinker; - ProtocalUnit = protocalUnit; + ProtocolLinker = protocalLinker; + ProtocolUnit = protocalUnit; ReturnParams = parameters; Success = success; } @@ -117,12 +117,12 @@ namespace Modbus.Net /// /// 协议连接器 /// - protected TProtocalLinker ProtocalLinker { get; set; } + protected TProtocolLinker ProtocolLinker { get; set; } /// /// 协议单元 /// - protected TProtocalUnit ProtocalUnit { get; set; } + protected TProtocolUnit ProtocolUnit { get; set; } /// /// 输入结构传入的参数 @@ -140,7 +140,7 @@ namespace Modbus.Net /// 协议单元 /// 输入参数生成函数 /// 输出参数 - protected async Task SendReceiveAsyncParamOut(TProtocalUnit unit, + protected async Task SendReceiveAsyncParamOut(TProtocolUnit unit, Func inputStructCreator) { if (Success) @@ -151,10 +151,10 @@ namespace Modbus.Net { TParamOut receiveContent; //如果为特别处理协议的话,跳过协议扩展收缩 - if (unit.GetType().GetTypeInfo().GetCustomAttributes(typeof(SpecialProtocalUnitAttribute)).Any()) - receiveContent = await ProtocalLinker.SendReceiveWithoutExtAndDecAsync(formatContent); + if (unit.GetType().GetTypeInfo().GetCustomAttributes(typeof(SpecialProtocolUnitAttribute)).Any()) + receiveContent = await ProtocolLinker.SendReceiveWithoutExtAndDecAsync(formatContent); else - receiveContent = await ProtocalLinker.SendReceiveAsync(formatContent); + receiveContent = await ProtocolLinker.SendReceiveAsync(formatContent); return receiveContent; } } @@ -167,15 +167,15 @@ namespace Modbus.Net /// 协议单元 /// 输入参数生成函数 /// 管道实体 - public virtual async Task> SendReceiveAsync( - TProtocalUnit unit, + public virtual async Task> SendReceiveAsync( + TProtocolUnit unit, Func inputStructCreator) { var receiveContent = await SendReceiveAsyncParamOut(unit, inputStructCreator); if (receiveContent != null) - return new PipeUnit(ProtocalLinker, unit, + return new PipeUnit(ProtocolLinker, unit, receiveContent, true); - return new PipeUnit(ProtocalLinker, unit, ReturnParams, + return new PipeUnit(ProtocolLinker, unit, ReturnParams, false); } @@ -187,7 +187,7 @@ namespace Modbus.Net public T Unwrap() where T : class, IOutputStruct { var t = 0; - return ProtocalUnit.Unformat(ReturnParams, ref t); + return ProtocolUnit.Unformat(ReturnParams, ref t); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/ProtocalLinker.cs b/Modbus.Net/Modbus.Net/ProtocalLinker.cs index 1a892b7..1521db7 100644 --- a/Modbus.Net/Modbus.Net/ProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net/ProtocalLinker.cs @@ -7,7 +7,7 @@ namespace Modbus.Net /// /// 基本的协议连接器 /// - public abstract class ProtocalLinker : ProtocalLinker + public abstract class ProtocolLinker : ProtocolLinker { /// /// 发送并接收数据 @@ -47,7 +47,7 @@ namespace Modbus.Net var bytesExtend = Activator.CreateInstance(GetType().GetTypeInfo().Assembly.GetType(GetType().FullName + "BytesExtend")) as - IProtocalLinkerBytesExtend; + IProtocolLinkerBytesExtend; return bytesExtend?.BytesExtend(content); } @@ -62,7 +62,7 @@ namespace Modbus.Net var bytesExtend = Activator.CreateInstance(GetType().GetTypeInfo().Assembly.GetType(GetType().FullName + "BytesExtend")) as - IProtocalLinkerBytesExtend; + IProtocolLinkerBytesExtend; return bytesExtend?.BytesDecact(content); } } @@ -70,7 +70,7 @@ namespace Modbus.Net /// /// 基本的协议连接器 /// - public abstract class ProtocalLinker : IProtocalLinker + public abstract class ProtocolLinker : IProtocolLinker where TParamOut : class { /// diff --git a/Modbus.Net/Modbus.Net/ProtocalUnit.cs b/Modbus.Net/Modbus.Net/ProtocalUnit.cs index c3f308c..db44eea 100644 --- a/Modbus.Net/Modbus.Net/ProtocalUnit.cs +++ b/Modbus.Net/Modbus.Net/ProtocalUnit.cs @@ -5,14 +5,14 @@ namespace Modbus.Net /// /// 协议单元 /// - public abstract class ProtocalUnit : ProtocalUnit + public abstract class ProtocolUnit : ProtocolUnit { } /// /// 协议单元 /// - public abstract class ProtocalUnit : IProtocalFormatting + public abstract class ProtocolUnit : IProtocolFormatting { /// /// 是否为小端格式 @@ -72,7 +72,7 @@ namespace Modbus.Net /// 特殊协议单元,写入这个协议不会执行BytesExtend和BytesDecact /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] - public class SpecialProtocalUnitAttribute : Attribute + public class SpecialProtocolUnitAttribute : Attribute { } @@ -93,13 +93,13 @@ namespace Modbus.Net /// /// 协议错误 /// - public class ProtocalErrorException : Exception + public class ProtocolErrorException : Exception { /// /// 构造函数 /// /// - public ProtocalErrorException(string message) + public ProtocolErrorException(string message) : base(message) { } diff --git a/Modbus.Net/Modbus.Net/README.md b/Modbus.Net/Modbus.Net/README.md index 354e012..b1e2856 100644 --- a/Modbus.Net/Modbus.Net/README.md +++ b/Modbus.Net/Modbus.Net/README.md @@ -42,15 +42,15 @@ Modbus TCP connection and Siemens Ethenet Connection are supported at the same t Connector implements the basic connecting methods, like Socket, Com and SignalR. -### ProtocalLinker +### ProtocolLinker -ProtocalLinker implements the link, send, and send recevice actions. +ProtocolLinker implements the link, send, and send recevice actions. -### ProtocalLinkerBytesExtend +### ProtocolLinkerBytesExtend -Some Protocal has the same head or tail in the same connection way, but different in different connection way, so bytes extend can extend sending message when they are sended in different type of connection. +Some Protocol has the same head or tail in the same connection way, but different in different connection way, so bytes extend can extend sending message when they are sended in different type of connection. -### ProtocalUnit +### ProtocolUnit Format and deformat the protocal message between the structual class and bytes array. @@ -58,13 +58,13 @@ Format and deformat the protocal message between the structual class and bytes a Help change the value between number and bytes, or number array to bytes array. -### Protocal +### Protocol Manage all protocals and implement a lazy loading method. ### Utility -Manage several types of Protocal to a same calling interface. +Manage several types of Protocol to a same calling interface. ### Machine @@ -385,7 +385,7 @@ task.InvokeTimerAll(new TaskItemGetData(returnValues => public async Task SetDatasAsync(string connectionToken, MachineSetDataType setDataType, Dictionary values) ``` -## Implementing Your Own Protocal +## Implementing Your Own Protocol The main target of Modbus.Net is building a high extensable hardware communication protocal, so we allow everyone to extend the protocal. To extend Modbus.Net, first of all ValueHelper.cs in Modbus.Net is a really powerful tool that you can use to modify values in byte array.There are two ValueHelpers: ValueHelper(Little Endian) and BigEndianValueHelper(Big Endian). Remember using the correct one. @@ -394,17 +394,17 @@ In this tutorial I will use Modbus.Net.Modbus to tell you how to implement your You should follow the following steps to implement your own protocal. -1.Implement Protocal. (ModbusProtocal.cs, ModbusTcpProtocal.cs) -First: Extend BaseProtocal to ModbusProtocal. +1.Implement Protocol. (ModbusProtocol.cs, ModbusTcpProtocol.cs) +First: Extend BaseProtocol to ModbusProtocol. ```C# -public abstract class ModbusProtocal : BaseProtocal -public class ModbusTcpProtocal : ModbusProtocal +public abstract class ModbusProtocol : BaseProtocol +public class ModbusTcpProtocol : ModbusProtocol ``` "abstract" keyword is optional because if user can use this protocal don't write abstract. -Second: Extend ProtocalUnit, IInputStruct and IOutputStruct. +Second: Extend ProtocolUnit, IInputStruct and IOutputStruct. ```C# -public class ReadDataModbusProtocal : ProtocalUnit +public class ReadDataModbusProtocol : ProtocolUnit { public override byte[] Format(IInputStruct message) { @@ -423,27 +423,27 @@ public class ReadDataModbusProtocal : ProtocalUnit } } ``` -There is another attribute called SpecialProtocalUnitAttribute. -If you add SpecialProtocalUnitAttribute to ProtocalUnit, then the protocal will not run BytesExtend and BytesDecact. +There is another attribute called SpecialProtocolUnitAttribute. +If you add SpecialProtocolUnitAttribute to ProtocolUnit, then the protocal will not run BytesExtend and BytesDecact. ```C# -[SpecialProtocalUnit] -internal class CreateReferenceSiemensProtocal : ProtocalUnit +[SpecialProtocolUnit] +internal class CreateReferenceSiemensProtocol : ProtocolUnit { ... } ``` -2.Implement Protocal based ProtocalLinker. (ModbusTcpProtocalLinker) -ProtocalLinker connect the Protocal to the BaseConnector, so that byte array can be sended using some specific way like Ethenet. +2.Implement Protocol based ProtocolLinker. (ModbusTcpProtocolLinker) +ProtocolLinker connect the Protocol to the BaseConnector, so that byte array can be sended using some specific way like Ethenet. ```C# -public class ModbusTcpProtocalLinker : TcpProtocalLinker +public class ModbusTcpProtocolLinker : TcpProtocolLinker { public override bool CheckRight(byte[] content) ``` CheckRight is the return check function, if you got the wrong bytes answer, you can return false or throw exceptions. -3.Implement Connector based ProtocalLinker and BaseConnector. (TcpProtocalLinker.cs, TcpConnector.cs) (Optional) -If you want to connect to hardware using another way, please implement your own ProtocalLinker and BaseConnector. And please remember that if you want to connector hardware using serial line, please don't use ComConnector in Modbus.Net and implement your own ComConnector. +3.Implement Connector based ProtocolLinker and BaseConnector. (TcpProtocolLinker.cs, TcpConnector.cs) (Optional) +If you want to connect to hardware using another way, please implement your own ProtocolLinker and BaseConnector. And please remember that if you want to connector hardware using serial line, please don't use ComConnector in Modbus.Net and implement your own ComConnector. You should implement all of these functions in BaseConnector. ```C# public abstract string ConnectionToken { get; } @@ -457,11 +457,11 @@ public abstract byte[] SendMsg(byte[] message); public abstract Task SendMsgAsync(byte[] message); ``` -4.Implement ProtocalLinkerBytesExtend (ModbusProtocalLinkerBytesExtend.cs) -If you want to use extend bytes when you send your bytes array to the hardware, you can set ProtocalLinkerBytesExtend. -The name of ProtocalLinkerBytesExtend is ProtocalLinker name + BytesExtend, like ModbusTcpProtocalLinkerBytesExtend. +4.Implement ProtocolLinkerBytesExtend (ModbusProtocolLinkerBytesExtend.cs) +If you want to use extend bytes when you send your bytes array to the hardware, you can set ProtocolLinkerBytesExtend. +The name of ProtocolLinkerBytesExtend is ProtocolLinker name + BytesExtend, like ModbusTcpProtocolLinkerBytesExtend. ```C# -public class ModbusTcpProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend +public class ModbusTcpProtocolLinkerBytesExtend : ProtocolLinkerBytesExtend { public override byte[] BytesExtend(byte[] content) { @@ -482,7 +482,7 @@ public class ModbusTcpProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend } } ``` -For example modbus tcp has a 6 bytes head: 4 bytes 0 and 2 bytes length. And when you get the bytes, please remove the head to fit the ModbusProtocal Unformat function. +For example modbus tcp has a 6 bytes head: 4 bytes 0 and 2 bytes length. And when you get the bytes, please remove the head to fit the ModbusProtocol Unformat function. 5.Implement BaseUtility.cs (ModbusUtility.cs) Implement low level api for Modbus. @@ -492,7 +492,7 @@ public override void SetConnectionType(int connectionType) protected override async Task GetDatasAsync(byte slaveAddress, byte masterAddress, string startAddress, int getByteCount) public override async Task SetDatasAsync(byte slaveAddress, byte masterAddress, string startAddress, object[] setContents) ``` -And don't remember set default AddressTranslator, slaveAddress, masterAddress and Protocal. +And don't remember set default AddressTranslator, slaveAddress, masterAddress and Protocol. ```C# public ModbusUtility(int connectionType, byte slaveAddress, byte masterAddress) : base(slaveAddress, masterAddress) { @@ -544,13 +544,13 @@ public class AddressUnit public int SubAddress { get; set; } = 0; } ``` -First of all, there are two types of coordinates in Modbus.Net Address System - Protocal Coordinate and Abstract Coordinate. +First of all, there are two types of coordinates in Modbus.Net Address System - Protocol Coordinate and Abstract Coordinate. Here is an example of the differences between them: In Register of Modbus, the minimum type is short, but Modbus.Net use type of byte to show the result. If you want to get value from 400003 in protocal coordinate, you actually get 5th and 6th byte value in abstract coordinate. -Version 1.0 and 1.1 used abstract coordinate so you need to convert the address. But fortunatly after 1.2, AddressUnit uses Protocal Coordinate so that you donnot need the convert the address descripted by modbus protocal itself, but this means if you want to get a bit or byte value in modbus, you need to use the subpos system. +Version 1.0 and 1.1 used abstract coordinate so you need to convert the address. But fortunatly after 1.2, AddressUnit uses Protocol Coordinate so that you donnot need the convert the address descripted by modbus protocal itself, but this means if you want to get a bit or byte value in modbus, you need to use the subpos system. For example if you want the get a value from the 6th byte in Hold Register. In traditional modbus you can only get 400003 of 2 bytes and get the 2nd byte from it. But in Modbus.Net there is an easy way to get it. ``` diff --git a/Modbus.Net/Modbus.Net/TcpProtocalLinker.cs b/Modbus.Net/Modbus.Net/TcpProtocalLinker.cs index 057895e..34eb76c 100644 --- a/Modbus.Net/Modbus.Net/TcpProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net/TcpProtocalLinker.cs @@ -5,12 +5,12 @@ namespace Modbus.Net /// /// Tcp连接对象 /// - public abstract class TcpProtocalLinker : ProtocalLinker + public abstract class TcpProtocolLinker : ProtocolLinker { /// /// 构造器 /// - protected TcpProtocalLinker(int port) + protected TcpProtocolLinker(int port) : this(ConfigurationManager.AppSettings["IP"], port) { } @@ -20,7 +20,7 @@ namespace Modbus.Net /// /// Ip地址 /// 端口 - protected TcpProtocalLinker(string ip, int port) + protected TcpProtocolLinker(string ip, int port) : this(ip, port, int.Parse(ConfigurationManager.AppSettings["IPConnectionTimeout"] ?? "5000")) { } @@ -31,7 +31,7 @@ namespace Modbus.Net /// Ip地址 /// 端口 /// 超时时间 - protected TcpProtocalLinker(string ip, int port, int connectionTimeout) + protected TcpProtocolLinker(string ip, int port, int connectionTimeout) { //初始化连接对象 BaseConnector = new TcpConnector(ip, port, connectionTimeout);