From a3fcf6798f5fca238dcabf9ed41ab07577e05b98 Mon Sep 17 00:00:00 2001 From: parallelbgls Date: Wed, 8 Feb 2017 15:42:22 +0800 Subject: [PATCH] 2017-2-8 update 1 Add interfaces. --- .../Modbus.Net.Modbus/ModbusProtocal.cs | 32 ++++----- .../ModbusProtocalLinkerBytesExtend.cs | 18 ++--- .../Properties/AssemblyInfo.cs | 4 +- Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs | 20 +++--- .../Modbus.Net.OPC/Properties/AssemblyInfo.cs | 4 +- .../Properties/AssemblyInfo.cs | 4 +- .../Modbus.Net.Siemens/SiemensPpiProtocal.cs | 2 +- .../Modbus.Net.Siemens/SiemensProtocal.cs | 70 +++++++++---------- .../SiemensProtocalLinkerBytesExtend.cs | 12 ++-- .../Modbus.Net.Siemens/SiemensTcpProtocal.cs | 6 +- Modbus.Net/Modbus.Net/AddressCombiner.cs | 2 +- Modbus.Net/Modbus.Net/BaseProtocal.cs | 6 +- Modbus.Net/Modbus.Net/IProtocal.cs | 43 ++++++++++++ Modbus.Net/Modbus.Net/IProtocalFormatting.cs | 4 +- Modbus.Net/Modbus.Net/IProtocalLinker.cs | 60 ++++++++++++++++ ...xtend.cs => IProtocalLinkerBytesExtend.cs} | 6 +- Modbus.Net/Modbus.Net/Modbus.Net.csproj | 4 +- .../Modbus.Net/Properties/AssemblyInfo.cs | 4 +- Modbus.Net/Modbus.Net/ProtocalLinker.cs | 4 +- Modbus.Net/Modbus.Net/ProtocalUnit.cs | 12 ++-- Modbus.Net/Modbus.Net/README.md | 6 +- 21 files changed, 214 insertions(+), 109 deletions(-) create mode 100644 Modbus.Net/Modbus.Net/IProtocal.cs create mode 100644 Modbus.Net/Modbus.Net/IProtocalLinker.cs rename Modbus.Net/Modbus.Net/{ProtocalLinkerBytesExtend.cs => IProtocalLinkerBytesExtend.cs} (76%) diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocal.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocal.cs index 68eb5a6..793117b 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocal.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocal.cs @@ -64,7 +64,7 @@ namespace Modbus.Net.Modbus #region 读PLC数据 - public class ReadDataModbusInputStruct : InputStruct + public class ReadDataModbusInputStruct : IInputStruct { public ReadDataModbusInputStruct(byte slaveAddress, string startAddress, ushort getCount, AddressTranslator addressTranslator) @@ -85,7 +85,7 @@ namespace Modbus.Net.Modbus public ushort GetCount { get; } } - public class ReadDataModbusOutputStruct : OutputStruct + public class ReadDataModbusOutputStruct : IOutputStruct { public ReadDataModbusOutputStruct(byte slaveAddress, byte functionCode, int dataCount, byte[] dataValue) @@ -107,14 +107,14 @@ namespace Modbus.Net.Modbus public class ReadDataModbusProtocal : ProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (ReadDataModbusInputStruct) message; return Format(r_message.SlaveAddress, r_message.FunctionCode, r_message.StartAddress, r_message.GetCount); } - public override OutputStruct Unformat(byte[] messageBytes, ref int pos) + public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { var slaveAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); var functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); @@ -136,7 +136,7 @@ namespace Modbus.Net.Modbus #region 写PLC数据 - public class WriteDataModbusInputStruct : InputStruct + public class WriteDataModbusInputStruct : IInputStruct { public WriteDataModbusInputStruct(byte slaveAddress, string startAddress, object[] writeValue, AddressTranslator addressTranslator) @@ -165,7 +165,7 @@ namespace Modbus.Net.Modbus public byte[] WriteValue { get; } } - public class WriteDataModbusOutputStruct : OutputStruct + public class WriteDataModbusOutputStruct : IOutputStruct { public WriteDataModbusOutputStruct(byte slaveAddress, byte functionCode, ushort startAddress, ushort writeCount) @@ -190,7 +190,7 @@ namespace Modbus.Net.Modbus /// public class WriteDataModbusProtocal : ProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (WriteDataModbusInputStruct) message; var functionCode = r_message.FunctionCode; @@ -207,7 +207,7 @@ namespace Modbus.Net.Modbus return formattingBytes; } - public override OutputStruct Unformat(byte[] messageBytes, ref int flag) + public override IOutputStruct Unformat(byte[] messageBytes, ref int flag) { var slaveAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag); var functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag); @@ -222,7 +222,7 @@ namespace Modbus.Net.Modbus #region 读PLC时间 - public class GetSystemTimeModbusInputStruct : InputStruct + public class GetSystemTimeModbusInputStruct : IInputStruct { public GetSystemTimeModbusInputStruct(byte slaveAddress) { @@ -241,7 +241,7 @@ namespace Modbus.Net.Modbus public ushort GetCount { get; } } - public class GetSystemTimeModbusOutputStruct : OutputStruct + public class GetSystemTimeModbusOutputStruct : IOutputStruct { public GetSystemTimeModbusOutputStruct(byte slaveAddress, byte functionCode, byte writeByteCount, ushort year, byte day, byte month, ushort hour, byte second, byte minute, @@ -267,14 +267,14 @@ namespace Modbus.Net.Modbus /// public class GetSystemTimeModbusProtocal : ProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (GetSystemTimeModbusInputStruct) message; return Format(r_message.SlaveAddress, r_message.FunctionCode, r_message.StartAddress, r_message.GetCount); } - public override OutputStruct Unformat(byte[] messageBytes, ref int flag) + public override IOutputStruct Unformat(byte[] messageBytes, ref int flag) { var slaveAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag); var functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag); @@ -295,7 +295,7 @@ namespace Modbus.Net.Modbus #region 写PLC时间 - public class SetSystemTimeModbusInputStruct : InputStruct + public class SetSystemTimeModbusInputStruct : IInputStruct { public SetSystemTimeModbusInputStruct(byte slaveAddress, DateTime time) { @@ -338,7 +338,7 @@ namespace Modbus.Net.Modbus public ushort Millisecond { get; } } - public class SetSystemTimeModbusOutputStruct : OutputStruct + public class SetSystemTimeModbusOutputStruct : IOutputStruct { public SetSystemTimeModbusOutputStruct(byte slaveAddress, byte functionCode, ushort startAddress, ushort writeCount) @@ -363,7 +363,7 @@ namespace Modbus.Net.Modbus /// public class SetSystemTimeModbusProtocal : ProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (SetSystemTimeModbusInputStruct) message; return Format(r_message.SlaveAddress, r_message.FunctionCode, @@ -372,7 +372,7 @@ namespace Modbus.Net.Modbus r_message.Month, r_message.Hour, r_message.Second, r_message.Minute, r_message.Millisecond); } - public override OutputStruct Unformat(byte[] messageBytes, ref int flag) + public override IOutputStruct Unformat(byte[] messageBytes, ref int flag) { var slaveAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag); var functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag); diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocalLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocalLinkerBytesExtend.cs index 1338d9f..b079c45 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocalLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocalLinkerBytesExtend.cs @@ -8,9 +8,9 @@ namespace Modbus.Net.Modbus /// /// Tcp协议字节伸缩 /// - public class ModbusTcpProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend + public class ModbusTcpProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend { - public override byte[] BytesExtend(byte[] content) + public byte[] BytesExtend(byte[] content) { //Modbus/Tcp协议扩张,前面加6个字节,前面4个为0,后面两个为协议整体内容的长度 var newFormat = new byte[6 + content.Length]; @@ -22,7 +22,7 @@ namespace Modbus.Net.Modbus return newFormat; } - public override byte[] BytesDecact(byte[] content) + public byte[] BytesDecact(byte[] content) { //Modbus/Tcp协议收缩,抛弃前面6个字节的内容 var newContent = new byte[content.Length - 6]; @@ -31,9 +31,9 @@ namespace Modbus.Net.Modbus } } - public class ModbusRtuProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend + public class ModbusRtuProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend { - public override byte[] BytesExtend(byte[] content) + public byte[] BytesExtend(byte[] content) { var crc = new byte[2]; //Modbus/Rtu协议扩张,增加CRC校验 @@ -44,7 +44,7 @@ namespace Modbus.Net.Modbus return newFormat; } - public override byte[] BytesDecact(byte[] content) + public byte[] BytesDecact(byte[] content) { //Modbus/Rtu协议收缩,抛弃后面2个字节的内容 var newContent = new byte[content.Length - 2]; @@ -53,9 +53,9 @@ namespace Modbus.Net.Modbus } } - public class ModbusAsciiProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend + public class ModbusAsciiProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend { - public override byte[] BytesExtend(byte[] content) + public byte[] BytesExtend(byte[] content) { //Modbus/Ascii协议扩张,前面增加:,后面增加LRC校验和尾字符 var newContent = new List(); @@ -70,7 +70,7 @@ namespace Modbus.Net.Modbus return newContent.ToArray(); } - public override byte[] BytesDecact(byte[] content) + public byte[] BytesDecact(byte[] content) { //Modbus/Ascii协议收缩,抛弃头尾。 var newContent = new List(); diff --git a/Modbus.Net/Modbus.Net.Modbus/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net.Modbus/Properties/AssemblyInfo.cs index 1505b06..bc0114a 100644 --- a/Modbus.Net/Modbus.Net.Modbus/Properties/AssemblyInfo.cs +++ b/Modbus.Net/Modbus.Net.Modbus/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, // 方法是按如下所示使用“*”: : // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.0")] -[assembly: AssemblyFileVersion("1.2.0")] +[assembly: AssemblyVersion("1.2.2")] +[assembly: AssemblyFileVersion("1.2.2")] diff --git a/Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs b/Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs index 74e1d29..4b2aa53 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcProtocal.cs @@ -14,7 +14,7 @@ namespace Modbus.Net.OPC #region 读数据 - public class ReadRequestOpcInputStruct : InputStruct + public class ReadRequestOpcInputStruct : IInputStruct { public ReadRequestOpcInputStruct(string tag) { @@ -24,7 +24,7 @@ namespace Modbus.Net.OPC public string Tag { get; } } - public class ReadRequestOpcOutputStruct : OutputStruct + public class ReadRequestOpcOutputStruct : IOutputStruct { public ReadRequestOpcOutputStruct(byte[] value) { @@ -34,15 +34,15 @@ namespace Modbus.Net.OPC public byte[] GetValue { get; private set; } } - public class ReadRequestOpcProtocal : SpecialProtocalUnit + public class ReadRequestOpcProtocal : ProtocalUnit, ISpecialProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (ReadRequestOpcInputStruct) message; return Format((byte) 0x00, Encoding.UTF8.GetBytes(r_message.Tag)); } - public override OutputStruct Unformat(byte[] messageBytes, ref int pos) + public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { return new ReadRequestOpcOutputStruct(messageBytes); } @@ -52,7 +52,7 @@ namespace Modbus.Net.OPC #region 写数据 - public class WriteRequestOpcInputStruct : InputStruct + public class WriteRequestOpcInputStruct : IInputStruct { public WriteRequestOpcInputStruct(string tag, object setValue) { @@ -64,7 +64,7 @@ namespace Modbus.Net.OPC public object SetValue { get; } } - public class WriteRequestOpcOutputStruct : OutputStruct + public class WriteRequestOpcOutputStruct : IOutputStruct { public WriteRequestOpcOutputStruct(bool writeResult) { @@ -74,9 +74,9 @@ namespace Modbus.Net.OPC public bool WriteResult { get; private set; } } - public class WriteRequestOpcProtocal : SpecialProtocalUnit + public class WriteRequestOpcProtocal : ProtocalUnit, ISpecialProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (WriteRequestOpcInputStruct) message; var tag = Encoding.UTF8.GetBytes(r_message.Tag); @@ -84,7 +84,7 @@ namespace Modbus.Net.OPC return Format((byte) 0x01, tag, 0x00ffff00, fullName, 0x00ffff00, r_message.SetValue); } - public override OutputStruct Unformat(byte[] messageBytes, ref int pos) + public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { var ansByte = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); var ans = ansByte != 0; diff --git a/Modbus.Net/Modbus.Net.OPC/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net.OPC/Properties/AssemblyInfo.cs index 8928de7..0b5dd52 100644 --- a/Modbus.Net/Modbus.Net.OPC/Properties/AssemblyInfo.cs +++ b/Modbus.Net/Modbus.Net.OPC/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, // 方法是按如下所示使用“*”: : // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.0")] -[assembly: AssemblyFileVersion("1.2.0")] +[assembly: AssemblyVersion("1.2.2")] +[assembly: AssemblyFileVersion("1.2.2")] diff --git a/Modbus.Net/Modbus.Net.Siemens/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net.Siemens/Properties/AssemblyInfo.cs index 900db89..06db3c9 100644 --- a/Modbus.Net/Modbus.Net.Siemens/Properties/AssemblyInfo.cs +++ b/Modbus.Net/Modbus.Net.Siemens/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, // 方法是按如下所示使用“*”: : // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.0")] -[assembly: AssemblyFileVersion("1.2.0")] +[assembly: AssemblyVersion("1.2.2")] +[assembly: AssemblyFileVersion("1.2.2")] diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocal.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocal.cs index 2522fb7..1778dd3 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocal.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocal.cs @@ -34,7 +34,7 @@ namespace Modbus.Net.Siemens return await base.SendReceiveAsync(isLittleEndian, content); } - private async Task ForceSendReceiveAsync(ProtocalUnit unit, InputStruct content) + private async Task ForceSendReceiveAsync(ProtocalUnit unit, IInputStruct content) { return await base.SendReceiveAsync(unit, content); } diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocal.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocal.cs index 36fbec5..f065781 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocal.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocal.cs @@ -111,7 +111,7 @@ namespace Modbus.Net.Siemens #region 串口连接建立 - internal class ComCreateReferenceSiemensInputStruct : InputStruct + internal class ComCreateReferenceSiemensInputStruct : IInputStruct { public ComCreateReferenceSiemensInputStruct(byte slaveAddress, byte masterAddress) { @@ -123,7 +123,7 @@ namespace Modbus.Net.Siemens public byte MasterAddress { get; set; } } - internal class ComCreateReferenceSiemensOutputStruct : OutputStruct + internal class ComCreateReferenceSiemensOutputStruct : IOutputStruct { public ComCreateReferenceSiemensOutputStruct(byte slaveAddress, byte masterAddress, byte confirmMessage) { @@ -137,9 +137,9 @@ namespace Modbus.Net.Siemens public byte ConfirmMessage { get; set; } } - internal class ComCreateReferenceSiemensProtocal : SpecialProtocalUnit + internal class ComCreateReferenceSiemensProtocal : ProtocalUnit, ISpecialProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (ComCreateReferenceSiemensInputStruct) message; var crc = (r_message.SlaveAddress + r_message.MasterAddress + 0x49)%256; @@ -147,7 +147,7 @@ namespace Modbus.Net.Siemens (byte) 0x16); } - public override OutputStruct Unformat(byte[] messageBytes, ref int pos) + public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { pos = 1; var masterAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); @@ -161,7 +161,7 @@ namespace Modbus.Net.Siemens #region 以太网建立连接 - internal class CreateReferenceSiemensInputStruct : InputStruct + internal class CreateReferenceSiemensInputStruct : IInputStruct { public byte TdpuSize; @@ -177,7 +177,7 @@ namespace Modbus.Net.Siemens } } - internal class CreateReferenceSiemensOutputStruct : OutputStruct + internal class CreateReferenceSiemensOutputStruct : IOutputStruct { public CreateReferenceSiemensOutputStruct(byte tdpuSize, ushort srcTsap, ushort dstTsap) { @@ -191,9 +191,9 @@ namespace Modbus.Net.Siemens public ushort TsapDst { get; private set; } } - internal class CreateReferenceSiemensProtocal : SpecialProtocalUnit + internal class CreateReferenceSiemensProtocal : ProtocalUnit, ISpecialProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (CreateReferenceSiemensInputStruct) message; const ushort head = 0x0300; @@ -213,7 +213,7 @@ namespace Modbus.Net.Siemens srcTsapCode, srcTsapContent, dstTsapCode, dstTsapContent); } - public override OutputStruct Unformat(byte[] messageBytes, ref int pos) + public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { pos = 11; byte tdpuSize = 0; @@ -247,7 +247,7 @@ namespace Modbus.Net.Siemens #region 串口消息确认 - public class ComConfirmMessageSiemensInputStruct : InputStruct + public class ComConfirmMessageSiemensInputStruct : IInputStruct { public ComConfirmMessageSiemensInputStruct(byte slaveAddress, byte masterAddress) { @@ -259,7 +259,7 @@ namespace Modbus.Net.Siemens public byte MasterAddress { get; set; } } - public class ComConfirmMessageSiemensOutputStruct : OutputStruct + public class ComConfirmMessageSiemensOutputStruct : IOutputStruct { public ComConfirmMessageSiemensOutputStruct(byte confirmByte) { @@ -269,9 +269,9 @@ namespace Modbus.Net.Siemens public byte ConfirmByte { get; set; } } - public class ComConfirmMessageSiemensProtocal : SpecialProtocalUnit + public class ComConfirmMessageSiemensProtocal : ProtocalUnit, ISpecialProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (ComConfirmMessageSiemensInputStruct) message; var crc = r_message.SlaveAddress + r_message.MasterAddress + 0x5c%256; @@ -279,7 +279,7 @@ namespace Modbus.Net.Siemens (byte) 0x16); } - public override OutputStruct Unformat(byte[] messageBytes, ref int pos) + public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { var confirmByte = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); return new ComConfirmMessageSiemensOutputStruct(confirmByte); @@ -290,7 +290,7 @@ namespace Modbus.Net.Siemens #region 以太网连接确认 - internal class EstablishAssociationSiemensInputStruct : InputStruct + internal class EstablishAssociationSiemensInputStruct : IInputStruct { public EstablishAssociationSiemensInputStruct(ushort pduRef, ushort maxCalling, ushort maxCalled, ushort maxPdu) { @@ -306,7 +306,7 @@ namespace Modbus.Net.Siemens public ushort MaxPdu { get; } } - internal class EstablishAssociationSiemensOutputStruct : OutputStruct + internal class EstablishAssociationSiemensOutputStruct : IOutputStruct { public EstablishAssociationSiemensOutputStruct(ushort pduRef, ushort maxCalling, ushort maxCalled, ushort maxPdu) { @@ -324,7 +324,7 @@ namespace Modbus.Net.Siemens internal class EstablishAssociationSiemensProtocal : ProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (EstablishAssociationSiemensInputStruct) message; const byte protoId = 0x32; @@ -342,7 +342,7 @@ namespace Modbus.Net.Siemens maxCalled, maxPdu); } - public override OutputStruct Unformat(byte[] messageBytes, ref int pos) + public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { pos = 4; var pduRef = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref pos); @@ -358,7 +358,7 @@ namespace Modbus.Net.Siemens #region 读数据请求 - public class ReadRequestSiemensInputStruct : InputStruct + public class ReadRequestSiemensInputStruct : IInputStruct { public ReadRequestSiemensInputStruct(byte slaveAddress, byte masterAddress, ushort pduRef, SiemensTypeCode getType, string startAddress, ushort getCount, AddressTranslator addressTranslator) @@ -385,7 +385,7 @@ namespace Modbus.Net.Siemens public int Offset { get; } } - public class ReadRequestSiemensOutputStruct : OutputStruct + public class ReadRequestSiemensOutputStruct : IOutputStruct { public ReadRequestSiemensOutputStruct(ushort pduRef, SiemensAccessResult accessResult, SiemensDataType dataType, ushort getLength, byte[] value) @@ -406,7 +406,7 @@ namespace Modbus.Net.Siemens public class ReadRequestSiemensProtocal : ProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (ReadRequestSiemensInputStruct) message; var slaveAddress = r_message.SlaveAddress; @@ -434,7 +434,7 @@ namespace Modbus.Net.Siemens offsetBitBytes.Skip(1).ToArray()); } - public override OutputStruct Unformat(byte[] messageBytes, ref int pos) + public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { pos = 4; var pduRef = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref pos); @@ -454,7 +454,7 @@ namespace Modbus.Net.Siemens #region 写数据请求 - public class WriteRequestSiemensInputStruct : InputStruct + public class WriteRequestSiemensInputStruct : IInputStruct { public WriteRequestSiemensInputStruct(byte slaveAddress, byte masterAddress, ushort pduRef, string startAddress, object[] writeValue, AddressTranslator addressTranslator) @@ -479,7 +479,7 @@ namespace Modbus.Net.Siemens public object[] WriteValue { get; } } - public class WriteRequestSiemensOutputStruct : OutputStruct + public class WriteRequestSiemensOutputStruct : IOutputStruct { public WriteRequestSiemensOutputStruct(ushort pduRef, SiemensAccessResult accessResult) { @@ -493,7 +493,7 @@ namespace Modbus.Net.Siemens public class WriteRequestSiemensProtocal : ProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { var r_message = (WriteRequestSiemensInputStruct) message; var valueBytes = BigEndianValueHelper.Instance.ObjectArrayToByteArray(r_message.WriteValue); @@ -525,7 +525,7 @@ namespace Modbus.Net.Siemens offsetBitBytes.Skip(1).ToArray(), reserved, type, numberOfWriteBits, valueBytes); } - public override OutputStruct Unformat(byte[] messageBytes, ref int pos) + public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { if (messageBytes.Length == 1) { @@ -548,7 +548,7 @@ namespace Modbus.Net.Siemens /* #region 读时间请求 - public class ReadTimeSiemensInputStruct : InputStruct + public class ReadTimeSiemensInputStruct : IInputStruct { public ReadTimeSiemensInputStruct(ushort pduRef) { @@ -558,7 +558,7 @@ namespace Modbus.Net.Siemens public ushort PduRef { get; private set; } } - public class ReadTimeSiemensOutputStruct : OutputStruct + public class ReadTimeSiemensOutputStruct : IOutputStruct { public ReadTimeSiemensOutputStruct(ushort pduRef, DateTime dateTime, TodClockStatus todClockStatus) { @@ -574,12 +574,12 @@ namespace Modbus.Net.Siemens public class ReadTimeSiemensProtocal : ProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { throw new NotImplementedException(); } - public override OutputStruct Unformat(byte[] messageBytes, ref int pos) + public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { throw new NotImplementedException(); } @@ -589,7 +589,7 @@ namespace Modbus.Net.Siemens #region 写时间请求 - public class WriteTimeSiemensInputStruct : InputStruct + public class WriteTimeSiemensInputStruct : IInputStruct { public WriteTimeSiemensInputStruct(ushort pduRef, DateTime dateTime, TodClockStatus todClockStatus) { @@ -603,7 +603,7 @@ namespace Modbus.Net.Siemens public TodClockStatus TodClockStatus { get; private set; } } - public class WriteTimeSiemensOutputStruct : OutputStruct + public class WriteTimeSiemensOutputStruct : IOutputStruct { public WriteTimeSiemensOutputStruct(ushort pduRef, byte errCod) { @@ -617,12 +617,12 @@ namespace Modbus.Net.Siemens public class WriteTimeSiemensProtocal : ProtocalUnit { - public override byte[] Format(InputStruct message) + public override byte[] Format(IInputStruct message) { throw new NotImplementedException(); } - public override OutputStruct Unformat(byte[] messageBytes, ref int pos) + public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { throw new NotImplementedException(); } diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocalLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocalLinkerBytesExtend.cs index 09194b3..180c244 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocalLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocalLinkerBytesExtend.cs @@ -5,16 +5,16 @@ namespace Modbus.Net.Siemens /// /// 西门子Tcp协议扩展 /// - public class SiemensTcpProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend + public class SiemensTcpProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend { - public override byte[] BytesExtend(byte[] content) + public byte[] BytesExtend(byte[] content) { Array.Copy(new byte[] {0x03, 0x00, 0x00, 0x00, 0x02, 0xf0, 0x80}, 0, content, 0, 7); Array.Copy(BigEndianValueHelper.Instance.GetBytes((ushort) content.Length), 0, content, 2, 2); return content; } - public override byte[] BytesDecact(byte[] content) + public byte[] BytesDecact(byte[] content) { var newContent = new byte[content.Length - 7]; Array.Copy(content, 7, newContent, 0, newContent.Length); @@ -25,9 +25,9 @@ namespace Modbus.Net.Siemens /// /// 西门子Ppi协议扩展 /// - public class SiemensPpiProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend + public class SiemensPpiProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend { - public override byte[] BytesExtend(byte[] content) + public byte[] BytesExtend(byte[] content) { var newContent = new byte[content.Length + 2]; Array.Copy(content, 0, newContent, 0, content.Length); @@ -44,7 +44,7 @@ namespace Modbus.Net.Siemens return newContent; } - public override byte[] BytesDecact(byte[] content) + public byte[] BytesDecact(byte[] content) { var newContent = new byte[content.Length - 9]; Array.Copy(content, 7, newContent, 0, newContent.Length); diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocal.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocal.cs index 82c5a3d..7d042a2 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocal.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocal.cs @@ -55,12 +55,12 @@ namespace Modbus.Net.Siemens return await base.SendReceiveAsync(isLittleEndian, content); } - public override OutputStruct SendReceive(ProtocalUnit unit, InputStruct content) + public override IOutputStruct SendReceive(ProtocalUnit unit, IInputStruct content) { return AsyncHelper.RunSync(() => SendReceiveAsync(unit, content)); } - public override async Task SendReceiveAsync(ProtocalUnit unit, InputStruct content) + public override async Task SendReceiveAsync(ProtocalUnit unit, IInputStruct content) { if (ProtocalLinker != null && ProtocalLinker.IsConnected) return await base.SendReceiveAsync(unit, content); if (_connectTryCount > 10) return null; @@ -71,7 +71,7 @@ namespace Modbus.Net.Siemens .ContinueWith(answer => answer.Result ? base.SendReceiveAsync(unit, content) : null); } - private async Task ForceSendReceiveAsync(ProtocalUnit unit, InputStruct content) + private async Task ForceSendReceiveAsync(ProtocalUnit unit, IInputStruct content) { return await base.SendReceiveAsync(unit, content); } diff --git a/Modbus.Net/Modbus.Net/AddressCombiner.cs b/Modbus.Net/Modbus.Net/AddressCombiner.cs index 9c389c7..90c53b6 100644 --- a/Modbus.Net/Modbus.Net/AddressCombiner.cs +++ b/Modbus.Net/Modbus.Net/AddressCombiner.cs @@ -233,7 +233,7 @@ namespace Modbus.Net var preAddress = continusAddresses[index]; continusAddresses.RemoveAt(index); continusAddresses.RemoveAt(index); - //变为新的地址段 + //合并两个已有的地址段,变为一个新的地址段 var newAddress = new CommunicationUnit { Area = nowAddress.Area, diff --git a/Modbus.Net/Modbus.Net/BaseProtocal.cs b/Modbus.Net/Modbus.Net/BaseProtocal.cs index 471a6b5..f74b6d7 100644 --- a/Modbus.Net/Modbus.Net/BaseProtocal.cs +++ b/Modbus.Net/Modbus.Net/BaseProtocal.cs @@ -102,7 +102,7 @@ namespace Modbus.Net /// 协议的实例 /// 输入信息的结构化描述 /// 输出信息的结构化描述 - public virtual OutputStruct SendReceive(ProtocalUnit unit, InputStruct content) + public virtual IOutputStruct SendReceive(ProtocalUnit unit, IInputStruct content) { return AsyncHelper.RunSync(() => SendReceiveAsync(unit, content)); } @@ -113,7 +113,7 @@ namespace Modbus.Net /// 协议的实例 /// 输入信息的结构化描述 /// 输出信息的结构化描述 - public virtual async Task SendReceiveAsync(ProtocalUnit unit, InputStruct content) + public virtual async Task SendReceiveAsync(ProtocalUnit unit, IInputStruct content) { var t = 0; var formatContent = unit.Format(content); @@ -121,7 +121,7 @@ namespace Modbus.Net { byte[] receiveContent; //如果为特别处理协议的话,跳过协议扩展收缩 - if (unit is SpecialProtocalUnit) + if (unit is ISpecialProtocalUnit) { receiveContent = await ProtocalLinker.SendReceiveWithoutExtAndDecAsync(formatContent); } diff --git a/Modbus.Net/Modbus.Net/IProtocal.cs b/Modbus.Net/Modbus.Net/IProtocal.cs new file mode 100644 index 0000000..f7f2f18 --- /dev/null +++ b/Modbus.Net/Modbus.Net/IProtocal.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Modbus.Net +{ + public interface IProtocal + { + /// + /// 发送协议内容并接收,一般方法 + /// + /// 是否是小端格式 + /// 写入的内容,使用对象数组描述 + /// 从设备获取的字节流 + byte[] SendReceive(bool isLittleEndian, params object[] content); + + /// + /// 发送协议内容并接收,一般方法 + /// + /// 是否是小端格式 + /// 写入的内容,使用对象数组描述 + /// 从设备获取的字节流 + Task SendReceiveAsync(bool isLittleEndian, params object[] content); + + /// + /// 发送协议,通过传入需要使用的协议内容和输入结构 + /// + /// 协议的实例 + /// 输入信息的结构化描述 + /// 输出信息的结构化描述 + IOutputStruct SendReceive(ProtocalUnit unit, IInputStruct content); + + /// + /// 发送协议,通过传入需要使用的协议内容和输入结构 + /// + /// 协议的实例 + /// 输入信息的结构化描述 + /// 输出信息的结构化描述 + Task SendReceiveAsync(ProtocalUnit unit, IInputStruct content); + } +} diff --git a/Modbus.Net/Modbus.Net/IProtocalFormatting.cs b/Modbus.Net/Modbus.Net/IProtocalFormatting.cs index ca73ccc..eca6262 100644 --- a/Modbus.Net/Modbus.Net/IProtocalFormatting.cs +++ b/Modbus.Net/Modbus.Net/IProtocalFormatting.cs @@ -10,7 +10,7 @@ /// /// 结构化的输入数据 /// 格式化后的字节流 - byte[] Format(InputStruct message); + byte[] Format(IInputStruct message); /// /// 从对象的参数数组格式化 @@ -25,6 +25,6 @@ /// 返回数据的字节流 /// 转换标记位 /// 结构化的输出数据 - OutputStruct Unformat(byte[] messageBytes, ref int pos); + IOutputStruct Unformat(byte[] messageBytes, ref int pos); } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/IProtocalLinker.cs b/Modbus.Net/Modbus.Net/IProtocalLinker.cs new file mode 100644 index 0000000..9b03f50 --- /dev/null +++ b/Modbus.Net/Modbus.Net/IProtocalLinker.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Modbus.Net +{ + public interface IProtocalLinker + { + /// + /// 发送并接收数据 + /// + /// 发送协议的内容 + /// 接收协议的内容 + byte[] SendReceive(byte[] content); + + /// + /// 发送并接收数据 + /// + /// 发送协议的内容 + /// 接收协议的内容 + Task SendReceiveAsync(byte[] content); + + /// + /// 发送并接收数据,不进行协议扩展和收缩,用于特殊协议 + /// + /// 发送协议的内容 + /// 接收协议的内容 + byte[] SendReceiveWithoutExtAndDec(byte[] content); + + /// + /// 发送并接收数据,不进行协议扩展和收缩,用于特殊协议 + /// + /// 发送协议的内容 + /// 接收协议的内容 + Task SendReceiveWithoutExtAndDecAsync(byte[] content); + + /// + /// 检查接收的数据是否正确 + /// + /// 接收协议的内容 + /// 协议是否是正确的 + bool? CheckRight(byte[] content); + + /// + /// 协议内容扩展,发送时根据需要扩展 + /// + /// 扩展前的基本协议内容 + /// 扩展后的协议内容 + byte[] BytesExtend(byte[] content); + + /// + /// 协议内容缩减,接收时根据需要缩减 + /// + /// 缩减前的完整协议内容 + /// 缩减后的协议内容 + byte[] BytesDecact(byte[] content); + } +} diff --git a/Modbus.Net/Modbus.Net/ProtocalLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net/IProtocalLinkerBytesExtend.cs similarity index 76% rename from Modbus.Net/Modbus.Net/ProtocalLinkerBytesExtend.cs rename to Modbus.Net/Modbus.Net/IProtocalLinkerBytesExtend.cs index 9c27752..4dafffe 100644 --- a/Modbus.Net/Modbus.Net/ProtocalLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net/IProtocalLinkerBytesExtend.cs @@ -3,20 +3,20 @@ /// /// 协议字节伸缩 /// - public abstract class ProtocalLinkerBytesExtend + public interface IProtocalLinkerBytesExtend { /// /// 协议扩展,协议内容发送前调用 /// /// 扩展前的原始协议内容 /// 扩展后的协议内容 - public abstract byte[] BytesExtend(byte[] content); + byte[] BytesExtend(byte[] content); /// /// 协议收缩,协议内容接收后调用 /// /// 收缩前的完整协议内容 /// 收缩后的协议内容 - public abstract byte[] BytesDecact(byte[] content); + byte[] BytesDecact(byte[] content); } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/Modbus.Net.csproj b/Modbus.Net/Modbus.Net/Modbus.Net.csproj index ff1a8bf..584ec11 100644 --- a/Modbus.Net/Modbus.Net/Modbus.Net.csproj +++ b/Modbus.Net/Modbus.Net/Modbus.Net.csproj @@ -68,9 +68,11 @@ + + - + diff --git a/Modbus.Net/Modbus.Net/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net/Properties/AssemblyInfo.cs index 2b9969d..2c4d38c 100644 --- a/Modbus.Net/Modbus.Net/Properties/AssemblyInfo.cs +++ b/Modbus.Net/Modbus.Net/Properties/AssemblyInfo.cs @@ -35,5 +35,5 @@ using System.Runtime.InteropServices; // 方法是按如下所示使用“*”: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.0")] -[assembly: AssemblyFileVersion("1.2.0")] \ No newline at end of file +[assembly: AssemblyVersion("1.2.2")] +[assembly: AssemblyFileVersion("1.2.2")] \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/ProtocalLinker.cs b/Modbus.Net/Modbus.Net/ProtocalLinker.cs index 0f5b54c..cfc83ea 100644 --- a/Modbus.Net/Modbus.Net/ProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net/ProtocalLinker.cs @@ -113,7 +113,7 @@ namespace Modbus.Net //自动查找相应的协议放缩类,命令规则为——当前的实际类名(注意是继承后的)+"BytesExtend"。 var bytesExtend = Assembly.Load(GetType().Assembly.FullName).CreateInstance(GetType().FullName + "BytesExtend") as - ProtocalLinkerBytesExtend; + IProtocalLinkerBytesExtend; return bytesExtend?.BytesExtend(content); } @@ -127,7 +127,7 @@ namespace Modbus.Net //自动查找相应的协议放缩类,命令规则为——当前的实际类名(注意是继承后的)+"BytesExtend"。 var bytesExtend = Assembly.Load(GetType().Assembly.GetName().Name).CreateInstance(GetType().FullName + "BytesExtend") as - ProtocalLinkerBytesExtend; + IProtocalLinkerBytesExtend; return bytesExtend?.BytesDecact(content); } } diff --git a/Modbus.Net/Modbus.Net/ProtocalUnit.cs b/Modbus.Net/Modbus.Net/ProtocalUnit.cs index 520b4eb..2c24d23 100644 --- a/Modbus.Net/Modbus.Net/ProtocalUnit.cs +++ b/Modbus.Net/Modbus.Net/ProtocalUnit.cs @@ -17,7 +17,7 @@ namespace Modbus.Net /// /// 结构化的输入数据 /// 格式化后的字节流 - public abstract byte[] Format(InputStruct message); + public abstract byte[] Format(IInputStruct message); /// /// 从对象的参数数组格式化 @@ -35,7 +35,7 @@ namespace Modbus.Net /// 返回数据的字节流 /// 转换标记位 /// 结构化的输出数据 - public abstract OutputStruct Unformat(byte[] messageBytes, ref int pos); + public abstract IOutputStruct Unformat(byte[] messageBytes, ref int pos); /// /// 转换静态方法,把对象数组转换为字节数组。 @@ -52,23 +52,23 @@ namespace Modbus.Net } /// - /// 特殊协议单元,继承这个类的协议不会执行BytesExtend和BytesDecact + /// 特殊协议单元,写入这个协议不会执行BytesExtend和BytesDecact /// - public abstract class SpecialProtocalUnit : ProtocalUnit + public interface ISpecialProtocalUnit { } /// /// 输入结构 /// - public class InputStruct + public interface IInputStruct { } /// /// 输出结构 /// - public class OutputStruct + public interface IOutputStruct { } diff --git a/Modbus.Net/Modbus.Net/README.md b/Modbus.Net/Modbus.Net/README.md index 4df83f9..308d285 100644 --- a/Modbus.Net/Modbus.Net/README.md +++ b/Modbus.Net/Modbus.Net/README.md @@ -551,9 +551,9 @@ Remember subpos system cannot cross a byte in current version. If you want to cr * New Samples (Complete) ###Version 1.2.2 -* Address Utility (In Road) -* More functions in TaskManager (In Road) -* More interfaces (In Road) +* Address Utility (Not Test) +* More functions in TaskManager (Not Test) +* More interfaces (Not Test) ###Version 1.2.3 * OPC UA Support (In Road)