diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs index 0680b9f..f8334e6 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs @@ -67,9 +67,9 @@ namespace Modbus.Net.Modbus var transaction = (ushort)(_sendCount % 65536 + 1); var tag = (ushort)0; var leng = (ushort)content.Length; - Array.Copy(BigEndianValueHelper.Instance.GetBytes(transaction), 0, newFormat, 0, 2); - Array.Copy(BigEndianValueHelper.Instance.GetBytes(tag), 0, newFormat, 2, 2); - Array.Copy(BigEndianValueHelper.Instance.GetBytes(leng), 0, newFormat, 4, 2); + Array.Copy(BigEndianLsbValueHelper.Instance.GetBytes(transaction), 0, newFormat, 0, 2); + Array.Copy(BigEndianLsbValueHelper.Instance.GetBytes(tag), 0, newFormat, 2, 2); + Array.Copy(BigEndianLsbValueHelper.Instance.GetBytes(leng), 0, newFormat, 4, 2); Array.Copy(content, 0, newFormat, 6, content.Length); _sendCount++; } diff --git a/Modbus.Net/Modbus.Net.Opc/OpcConnector.cs b/Modbus.Net/Modbus.Net.Opc/OpcConnector.cs index f7a218b..ee871ab 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcConnector.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcConnector.cs @@ -96,11 +96,24 @@ namespace Modbus.Net.Opc if (tag != null) { var result = await Client.ReadAsync(tag); - logger.LogInformation($"Opc Machine {ConnectionToken} Read Opc tag {tag} for value {result.Value}"); + object resultTrans; + if (result.Value.ToString() == "False") + { + resultTrans = (byte)0; + } + else if (result.Value.ToString() == "True") + { + resultTrans = (byte)1; + } + else + { + resultTrans = result.Value; + } + logger.LogInformation($"Opc Machine {ConnectionToken} Read Opc tag {tag} for value {result.Value} {result.Value.GetType().FullName}"); return new OpcParamOut { Success = true, - Value = BigEndianValueHelper.Instance.GetBytes(result.Value, result.Value.GetType()) + Value = BigEndianLsbValueHelper.Instance.GetBytes(resultTrans, resultTrans.GetType()) }; } return new OpcParamOut diff --git a/Modbus.Net/Modbus.Net.Opc/OpcProtocol.cs b/Modbus.Net/Modbus.Net.Opc/OpcProtocol.cs index 9af6609..473648c 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcProtocol.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcProtocol.cs @@ -171,7 +171,7 @@ /// 结构化的输出数据 public override IOutputStruct Unformat(OpcParamOut messageBytes, ref int pos) { - var ansByte = BigEndianValueHelper.Instance.GetByte(messageBytes.Value, ref pos); + var ansByte = BigEndianLsbValueHelper.Instance.GetByte(messageBytes.Value, ref pos); var ans = ansByte != 0; return new WriteRequestOpcOutputStruct(ans); } diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocol.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocol.cs index dcd35d2..5d3f72f 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocol.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocol.cs @@ -162,9 +162,9 @@ namespace Modbus.Net.Siemens public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { pos = 1; - var masterAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); - var slaveAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); - var confirmMessage = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); + var masterAddress = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); + var slaveAddress = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); + var confirmMessage = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); return new ComCreateReferenceSiemensOutputStruct(slaveAddress, masterAddress, confirmMessage); } } @@ -236,19 +236,19 @@ namespace Modbus.Net.Siemens case 0xc0: { pos += 2; - tdpuSize = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); + tdpuSize = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); break; } case 0xc1: { pos += 2; - srcTsap = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref pos); + srcTsap = BigEndianLsbValueHelper.Instance.GetUShort(messageBytes, ref pos); break; } case 0xc2: { pos += 2; - dstTsap = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref pos); + dstTsap = BigEndianLsbValueHelper.Instance.GetUShort(messageBytes, ref pos); break; } } @@ -334,7 +334,7 @@ namespace Modbus.Net.Siemens /// 输出数据 public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { - var confirmByte = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); + var confirmByte = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); return new ComConfirmMessageSiemensOutputStruct(confirmByte); } } @@ -399,11 +399,11 @@ namespace Modbus.Net.Siemens public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { pos = 4; - var pduRef = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref pos); + var pduRef = BigEndianLsbValueHelper.Instance.GetUShort(messageBytes, ref pos); pos = 14; - var maxCalling = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref pos); - var maxCalled = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref pos); - var maxPdu = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref pos); + var maxCalling = BigEndianLsbValueHelper.Instance.GetUShort(messageBytes, ref pos); + var maxCalled = BigEndianLsbValueHelper.Instance.GetUShort(messageBytes, ref pos); + var maxPdu = BigEndianLsbValueHelper.Instance.GetUShort(messageBytes, ref pos); return new EstablishAssociationSiemensOutputStruct(pduRef, maxCalling, maxCalled, maxPdu); } } @@ -563,7 +563,7 @@ namespace Modbus.Net.Siemens var dbBlock = r_message.DbBlock; var area = r_message.Area; var offsetBit = r_message.Offset * 8; - var offsetBitBytes = BigEndianValueHelper.Instance.GetBytes(offsetBit); + var offsetBitBytes = BigEndianLsbValueHelper.Instance.GetBytes(offsetBit); return Format(new byte[4], slaveAddress, masterAddress, (byte)0x6c, protoId, rosctr, redId, pduRef, parLg, datLg, serviceId, numberOfVariables , variableSpec, vAddrLg, syntaxId, type, numberOfElements, dbBlock, area, @@ -579,11 +579,11 @@ namespace Modbus.Net.Siemens public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { pos = 4; - var pduRef = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref pos); + var pduRef = BigEndianLsbValueHelper.Instance.GetUShort(messageBytes, ref pos); pos = 14; - var accessResult = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); - var dataType = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); - var length = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref pos); + var accessResult = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); + var dataType = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); + var length = BigEndianLsbValueHelper.Instance.GetUShort(messageBytes, ref pos); var byteLength = length / 8; var values = new byte[byteLength]; Array.Copy(messageBytes, pos, values, 0, byteLength); @@ -700,7 +700,7 @@ namespace Modbus.Net.Siemens public override byte[] Format(IInputStruct message) { var r_message = (WriteRequestSiemensInputStruct)message; - var valueBytes = BigEndianValueHelper.Instance.ObjectArrayToByteArray(r_message.WriteValue); + var valueBytes = BigEndianLsbValueHelper.Instance.ObjectArrayToByteArray(r_message.WriteValue); var slaveAddress = r_message.SlaveAddress; var masterAddress = r_message.MasterAddress; const byte protoId = 0x32; @@ -719,7 +719,7 @@ namespace Modbus.Net.Siemens var dbBlock = r_message.DbBlock; var area = r_message.Area; var offsetBit = r_message.Offset * 8; - var offsetBitBytes = BigEndianValueHelper.Instance.GetBytes(offsetBit); + var offsetBitBytes = BigEndianLsbValueHelper.Instance.GetBytes(offsetBit); const byte reserved = 0x00; const byte type = (byte)SiemensDataType.OtherAccess; var numberOfWriteBits = (ushort)(valueBytes.Length * 8); @@ -739,16 +739,16 @@ namespace Modbus.Net.Siemens { if (messageBytes.Length == 1) { - var accessResult = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); + var accessResult = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); return new WriteRequestSiemensOutputStruct(0, accessResult == 0xe5 ? SiemensAccessResult.NoError : SiemensAccessResult.InvalidAddress); } else { pos = 4; - var pduRef = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref pos); + var pduRef = BigEndianLsbValueHelper.Instance.GetUShort(messageBytes, ref pos); pos = 14; - var accessResult = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); + var accessResult = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); return new WriteRequestSiemensOutputStruct(pduRef, (SiemensAccessResult)accessResult); } } diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocolLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocolLinkerBytesExtend.cs index a2b37f2..b2b33cf 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocolLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocolLinkerBytesExtend.cs @@ -15,7 +15,7 @@ namespace Modbus.Net.Siemens 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); + Array.Copy(BigEndianLsbValueHelper.Instance.GetBytes((ushort)content.Length), 0, content, 2, 2); return content; } diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensStructDefinition.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensStructDefinition.cs index f6f7c86..d6815cf 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensStructDefinition.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensStructDefinition.cs @@ -7,9 +7,9 @@ get { var pos = 15; - return BigEndianValueHelper.Instance.GetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), ref pos); + return BigEndianLsbValueHelper.Instance.GetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), ref pos); } - set { TodValue = BigEndianValueHelper.Instance.SetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), 15, value); } + set { TodValue = BigEndianLsbValueHelper.Instance.SetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), 15, value); } } public byte K0_4 @@ -17,13 +17,13 @@ get { var pos = 0; - var byteValue = BigEndianValueHelper.Instance.GetByte(BigEndianValueHelper.Instance.GetBytes(TodValue), ref pos); + var byteValue = BigEndianLsbValueHelper.Instance.GetByte(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), ref pos); return (byte)(byteValue%64/4); } set { var pos = 0; - var byteValue = BigEndianValueHelper.Instance.GetByte(BigEndianValueHelper.Instance.GetBytes(TodValue), ref pos); + var byteValue = BigEndianLsbValueHelper.Instance.GetByte(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), ref pos); byteValue = (byte)(byteValue - (byteValue%128/4) + value); TodValue = (ushort)(TodValue%128 + byteValue*128); } @@ -34,9 +34,9 @@ get { var pos = 5; - return BigEndianValueHelper.Instance.GetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), ref pos); + return BigEndianLsbValueHelper.Instance.GetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), ref pos); } - set { TodValue = BigEndianValueHelper.Instance.SetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), 5, value); } + set { TodValue = BigEndianLsbValueHelper.Instance.SetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), 5, value); } } public byte UA @@ -44,15 +44,15 @@ get { var pos = 3; - var low = BigEndianValueHelper.Instance.GetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), ref pos) ? 1 : 0; - var high = BigEndianValueHelper.Instance.GetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), ref pos) ? 1 : 0; + var low = BigEndianLsbValueHelper.Instance.GetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), ref pos) ? 1 : 0; + var high = BigEndianLsbValueHelper.Instance.GetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), ref pos) ? 1 : 0; high *= 2; return (byte) (high + low); } set { - TodValue = BigEndianValueHelper.Instance.SetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), 3, value % 2 >= 1); - TodValue = BigEndianValueHelper.Instance.SetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), 4, value / 2 >= 1); + TodValue = BigEndianLsbValueHelper.Instance.SetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), 3, value % 2 >= 1); + TodValue = BigEndianLsbValueHelper.Instance.SetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), 4, value / 2 >= 1); } } public bool UZS @@ -60,9 +60,9 @@ get { var pos = 2; - return BigEndianValueHelper.Instance.GetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), ref pos); + return BigEndianLsbValueHelper.Instance.GetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), ref pos); } - set { TodValue = BigEndianValueHelper.Instance.SetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), 2, value); } + set { TodValue = BigEndianLsbValueHelper.Instance.SetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), 2, value); } } public bool ESY @@ -70,9 +70,9 @@ get { var pos = 1; - return BigEndianValueHelper.Instance.GetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), ref pos); + return BigEndianLsbValueHelper.Instance.GetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), ref pos); } - set { TodValue = BigEndianValueHelper.Instance.SetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), 1, value); } + set { TodValue = BigEndianLsbValueHelper.Instance.SetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), 1, value); } } public bool SYA @@ -80,9 +80,9 @@ get { var pos = 0; - return BigEndianValueHelper.Instance.GetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), ref pos); + return BigEndianLsbValueHelper.Instance.GetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), ref pos); } - set { TodValue = BigEndianValueHelper.Instance.SetBit(BigEndianValueHelper.Instance.GetBytes(TodValue), 0, value); } + set { TodValue = BigEndianLsbValueHelper.Instance.SetBit(BigEndianLsbValueHelper.Instance.GetBytes(TodValue), 0, value); } } public ushort TodValue { get; set; } diff --git a/Modbus.Net/Modbus.Net/Helper/AssemblyHelper.cs b/Modbus.Net/Modbus.Net/Helper/AssemblyHelper.cs new file mode 100644 index 0000000..37c95d7 --- /dev/null +++ b/Modbus.Net/Modbus.Net/Helper/AssemblyHelper.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Modbus.Net +{ + /// + /// 程序集辅助类 + /// + public static class AssemblyHelper + { + /// + /// 获取与Modbus.Net相关的所有程序集 + /// + /// + public static List GetAllLibraryAssemblies() + { + List allAssemblies = new List(); + string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + + allAssemblies.Add(Assembly.Load("Modbus.Net")); + + foreach (string dll in Directory.GetFiles(path, "Modbus.Net.*.dll")) + allAssemblies.Add(Assembly.LoadFile(dll)); + + return allAssemblies; + } + } +} diff --git a/Modbus.Net/Modbus.Net/Value/CRC16.cs b/Modbus.Net/Modbus.Net/Helper/CRC16.cs similarity index 100% rename from Modbus.Net/Modbus.Net/Value/CRC16.cs rename to Modbus.Net/Modbus.Net/Helper/CRC16.cs diff --git a/Modbus.Net/Modbus.Net/Value/ValueHelper.cs b/Modbus.Net/Modbus.Net/Helper/ValueHelper.cs similarity index 73% rename from Modbus.Net/Modbus.Net/Value/ValueHelper.cs rename to Modbus.Net/Modbus.Net/Helper/ValueHelper.cs index 78ad456..76b9e38 100644 --- a/Modbus.Net/Modbus.Net/Value/ValueHelper.cs +++ b/Modbus.Net/Modbus.Net/Helper/ValueHelper.cs @@ -8,16 +8,17 @@ using System.Text; namespace Modbus.Net { /// - /// 值与字节数组之间转换的辅助类(小端格式),这是一个Singleton类 + /// 值与字节数组之间转换的辅助类 /// - public class ValueHelper + public abstract class ValueHelper { - private static readonly ILogger logger = LogProvider.CreateLogger(); - /// /// 兼容数据类型对应的字节长度 /// - public Dictionary ByteLength = new Dictionary + /// + /// 兼容数据类型对应的字节长度 + /// + public static Dictionary ByteLength => new Dictionary { {"System.Boolean", 0.125}, {"System.Byte", 1}, @@ -31,10 +32,301 @@ namespace Modbus.Net {"System.Double", 8} }; + /// + /// 将一个byte数字转换为一个byte元素的数组。 + /// + /// byte数字 + /// byte数组 + public abstract byte[] GetBytes(byte value); + + /// + /// 将short数字转换为byte数组 + /// + /// 待转换的值 + /// 转换后的byte数组 + public abstract byte[] GetBytes(short value); + + /// + /// 将int数字转换为byte数组 + /// + /// 待转换的值 + /// 转换后的byte数组 + public abstract byte[] GetBytes(int value); + + /// + /// 将long数字转换为byte数组 + /// + /// 待转换的值 + /// 转换后的byte数组 + public abstract byte[] GetBytes(long value); + + /// + /// 将ushort数字转换为byte数组 + /// + /// 待转换的值 + /// 转换后的byte数组 + public abstract byte[] GetBytes(ushort value); + + /// + /// 将uint数字转换为byte数组 + /// + /// 待转换的值 + /// 转换后的byte数组 + public abstract byte[] GetBytes(uint value); + + /// + /// 将ulong数字转换为byte数组 + /// + /// 待转换的值 + /// 转换后的byte数组 + public abstract byte[] GetBytes(ulong value); + + /// + /// 将float数字转换为byte数组 + /// + /// 待转换的值 + /// 转换后的byte数组 + public abstract byte[] GetBytes(float value); + + /// + /// 将double数字转换为byte数组 + /// + /// 待转换的值 + /// 转换后的byte数组 + public abstract byte[] GetBytes(double value); + + /// + /// 将object数字转换为byte数组 + /// + /// 待转换的值 + /// 待转换的值的类型 + /// 转换后的byte数组 + public abstract byte[] GetBytes(object value, Type type); + + /// + /// 将byte数组中相应的位置转换为对应类型的数字 + /// + /// 待转换的字节数组 + /// 转换数字的位置 + /// 转换数字的比特位置(仅Type为bool的时候有效) + /// 转换的类型 + /// 转换出的数字 + public abstract object GetValue(byte[] data, ref int pos, ref int subPos, Type t); + + /// + /// 将byte数组中相应的位置转换为byte数字 + /// + /// 待转换的数组 + /// 转换数字的位置 + /// 转换出的数字 + public abstract byte GetByte(byte[] data, ref int pos); + + /// + /// 将byte数组中相应的位置转换为short数字 + /// + /// 待转换的数组 + /// 转换数字的位置 + /// 转换出的数字 + public abstract short GetShort(byte[] data, ref int pos); + + /// + /// 将byte数组中相应的位置转换为int数字 + /// + /// 待转换的数组 + /// 转换数字的位置 + /// 转换出的数字 + public abstract int GetInt(byte[] data, ref int pos); + + /// + /// 将byte数组中相应的位置转换为long数字 + /// + /// 待转换的数组 + /// 转换数字的位置 + /// 转换出的数字 + public abstract long GetLong(byte[] data, ref int pos); + + /// + /// 将byte数组中相应的位置转换为ushort数字 + /// + /// 待转换的数组 + /// 转换数字的位置 + /// 转换出的数字 + public abstract ushort GetUShort(byte[] data, ref int pos); + + /// + /// 将byte数组中相应的位置转换为uint数字 + /// + /// 待转换的数组 + /// 转换数字的位置 + /// 转换出的数字 + public abstract uint GetUInt(byte[] data, ref int pos); + + /// + /// 将byte数组中相应的位置转换为ulong数字 + /// + /// 待转换的数组 + /// 转换数字的位置 + /// 转换出的数字 + public abstract ulong GetULong(byte[] data, ref int pos); + + /// + /// 将byte数组中相应的位置转换为float数字 + /// + /// 待转换的数组 + /// 转换数字的位置 + /// 转换出的数字 + public abstract float GetFloat(byte[] data, ref int pos); + + /// + /// 将byte数组中相应的位置转换为double数字 + /// + /// 待转换的数组 + /// 转换数字的位置 + /// 转换出的数字 + public abstract double GetDouble(byte[] data, ref int pos); + + /// + /// 将byte数组中相应的位置转换为字符串 + /// + /// 待转换的数组 + /// 转换的个数 + /// 转换数字的位置 + /// 编码方法 + /// 转换出的字符串 + public abstract string GetString(byte[] data, int count, ref int pos, Encoding encoding); + + /// + /// 将byte数组中相应的位置转换为8个bit数字 + /// + /// 待转换的数组 + /// 转换数字的位置 + /// 转换出的位数组 + public abstract bool[] GetBits(byte[] data, ref int pos); + + /// + /// 获取一个byte中相对应的bit数组展开中第n个位置中的bit元素。 + /// + /// byte数字 + /// bit数组中的对应位置 + /// 小数位 + /// 对应位置的bit元素 + public abstract bool GetBit(byte number, ref int pos, ref int subPos); + + /// + /// 获取一个字节数组中某个Bit位的数据 + /// + /// byte数字 + /// bit数组中的对应位置 + /// 小数位 + /// 对应位置的bit元素 + public abstract bool GetBit(byte[] number, ref int pos, ref int subPos); + + /// + /// 反转一个字节的8个Bit位 + /// + /// 原始bit数组 + /// 反转的bit数组 + public abstract byte ReverseByte(byte originalByte); + + /// + /// 将待转换的对象数组转换为需要发送的byte数组 + /// + /// object数组 + /// byte数组 + public abstract byte[] ObjectArrayToByteArray(object[] contents); + + /// + /// 将byte数组转换为用户指定类型的数组,通过object数组的方式返回,用户需要再把object转换为自己需要的类型,或调用ObjectArrayToDestinationArray返回单一类型的目标数组。 + /// + /// byte数组 + /// 单一的类型和需要转换的个数的键值对 + /// object数组 + public abstract object[] ByteArrayToObjectArray(byte[] contents, KeyValuePair translateTypeAndCount); + + /// + /// 将一个byte数组转换成用户指定类型的数组,使用模板参数确定需要转换的类型 + /// + /// 目标数组类型 + /// 待转换的数组 + /// 转换的个数 + /// 以T为类型的数组 + public abstract T[] ByteArrayToDestinationArray(byte[] contents, int getCount); + + /// + /// 将byte数组转换为用户指定类型的数组,通过object数组的方式返回,用户需要再把object转换为自己需要的类型,或调用ObjectArrayToDestinationArray返回单一类型的目标数组。 + /// + /// byte数组 + /// + /// 一连串类型和需要转换的个数的键值对,该方法会依次转换每一个需要转的目标数据类型。比如:typeof(int),5; typeof(short),3 + /// 会转换出8个元素(当然前提是byte数组足够长的时候),5个int和3个short,然后全部变为object类型返回。 + /// + /// object数组 + public abstract object[] ByteArrayToObjectArray(byte[] contents, IEnumerable> translateTypeAndCount); + + /// + /// 将object数组转换为目标类型的单一数组 + /// + /// 需要转换的目标类型 + /// object数组 + /// 目标数组 + public abstract T[] ObjectArrayToDestinationArray(object[] contents); + + /// + /// 在一个数组中写一个值 + /// + /// 待写入的字节数组 + /// 设置的位置 + /// 设置的比特位位置(仅setValue为bit的时候有效) + /// 写入的值 + /// 写入是否成功 + public abstract bool SetValue(byte[] contents, int setPos, int subPos, object setValue); + + /// + /// 将short值设置到byte数组中 + /// + /// 待设置的byte数组 + /// 设置的位置 + /// 要设置的值 + /// 设置是否成功 + public abstract bool SetValue(byte[] contents, int pos, object setValue); + + /// + /// 设置一组数据中的一个bit + /// + /// 待设置的字节数组 + /// 位置 + /// bit位置 + /// bit数 + /// 设置是否成功 + public abstract bool SetBit(byte[] contents, int pos, int subPos, bool setValue); + + /// + /// 根据端格式获取ValueHelper实例 + /// + /// 端格式 + /// 对应的ValueHelper实例 + public static ValueHelper GetInstance(Endian endian) + { + foreach (var assembly in AssemblyHelper.GetAllLibraryAssemblies()) + { + var valueHelper = assembly.GetType("Modbus.Net."+endian.ToString() + "ValueHelper")?.GetProperty("Instance")?.GetValue(null, null) as ValueHelper; + if (valueHelper != null) return valueHelper; + } + return null; + } + } + + /// + /// 值与字节数组之间转换的辅助类(小端格式),这是一个Singleton类 + /// + public class LittleEndianLsbValueHelper : ValueHelper + { + private static readonly ILogger logger = LogProvider.CreateLogger(); + /// /// 构造器 /// - protected ValueHelper() + protected LittleEndianLsbValueHelper() { } @@ -53,7 +345,7 @@ namespace Modbus.Net /// /// byte数字 /// byte数组 - public byte[] GetBytes(byte value) + public override byte[] GetBytes(byte value) { return new[] { value }; } @@ -63,7 +355,7 @@ namespace Modbus.Net /// /// 待转换的值 /// 转换后的byte数组 - public virtual byte[] GetBytes(short value) + public override byte[] GetBytes(short value) { return BitConverter.GetBytes(value); } @@ -73,7 +365,7 @@ namespace Modbus.Net /// /// 待转换的值 /// 转换后的byte数组 - public virtual byte[] GetBytes(int value) + public override byte[] GetBytes(int value) { return BitConverter.GetBytes(value); } @@ -83,7 +375,7 @@ namespace Modbus.Net /// /// 待转换的值 /// 转换后的byte数组 - public virtual byte[] GetBytes(long value) + public override byte[] GetBytes(long value) { return BitConverter.GetBytes(value); } @@ -93,7 +385,7 @@ namespace Modbus.Net /// /// 待转换的值 /// 转换后的byte数组 - public virtual byte[] GetBytes(ushort value) + public override byte[] GetBytes(ushort value) { return BitConverter.GetBytes(value); } @@ -103,7 +395,7 @@ namespace Modbus.Net /// /// 待转换的值 /// 转换后的byte数组 - public virtual byte[] GetBytes(uint value) + public override byte[] GetBytes(uint value) { return BitConverter.GetBytes(value); } @@ -113,7 +405,7 @@ namespace Modbus.Net /// /// 待转换的值 /// 转换后的byte数组 - public virtual byte[] GetBytes(ulong value) + public override byte[] GetBytes(ulong value) { return BitConverter.GetBytes(value); } @@ -123,7 +415,7 @@ namespace Modbus.Net /// /// 待转换的值 /// 转换后的byte数组 - public virtual byte[] GetBytes(float value) + public override byte[] GetBytes(float value) { return BitConverter.GetBytes(value); } @@ -133,7 +425,7 @@ namespace Modbus.Net /// /// 待转换的值 /// 转换后的byte数组 - public virtual byte[] GetBytes(double value) + public override byte[] GetBytes(double value) { return BitConverter.GetBytes(value); } @@ -144,7 +436,7 @@ namespace Modbus.Net /// 待转换的值 /// 待转换的值的类型 /// 转换后的byte数组 - public virtual byte[] GetBytes(object value, Type type) + public override byte[] GetBytes(object value, Type type) { switch (type.FullName) { @@ -193,6 +485,11 @@ namespace Modbus.Net var bytes = _Instance.GetBytes((byte)value); return bytes; } + case "System.Boolean": + { + var bytes = _Instance.GetBytes((bool)value ? (byte)1 : (byte)0); + return bytes; + } default: { throw new NotImplementedException("没有实现除整数以外的其它转换方式"); @@ -208,7 +505,7 @@ namespace Modbus.Net /// 转换数字的比特位置(仅Type为bool的时候有效) /// 转换的类型 /// 转换出的数字 - public virtual object GetValue(byte[] data, ref int pos, ref int subPos, Type t) + public override object GetValue(byte[] data, ref int pos, ref int subPos, Type t) { switch (t.FullName) { @@ -275,7 +572,7 @@ namespace Modbus.Net /// 待转换的数组 /// 转换数字的位置 /// 转换出的数字 - public virtual byte GetByte(byte[] data, ref int pos) + public override byte GetByte(byte[] data, ref int pos) { var t = data[pos]; pos += 1; @@ -288,7 +585,7 @@ namespace Modbus.Net /// 待转换的数组 /// 转换数字的位置 /// 转换出的数字 - public virtual short GetShort(byte[] data, ref int pos) + public override short GetShort(byte[] data, ref int pos) { var t = BitConverter.ToInt16(data, pos); pos += 2; @@ -301,7 +598,7 @@ namespace Modbus.Net /// 待转换的数组 /// 转换数字的位置 /// 转换出的数字 - public virtual int GetInt(byte[] data, ref int pos) + public override int GetInt(byte[] data, ref int pos) { var t = BitConverter.ToInt32(data, pos); pos += 4; @@ -314,7 +611,7 @@ namespace Modbus.Net /// 待转换的数组 /// 转换数字的位置 /// 转换出的数字 - public virtual long GetLong(byte[] data, ref int pos) + public override long GetLong(byte[] data, ref int pos) { var t = BitConverter.ToInt64(data, pos); pos += 8; @@ -327,7 +624,7 @@ namespace Modbus.Net /// 待转换的数组 /// 转换数字的位置 /// 转换出的数字 - public virtual ushort GetUShort(byte[] data, ref int pos) + public override ushort GetUShort(byte[] data, ref int pos) { var t = BitConverter.ToUInt16(data, pos); pos += 2; @@ -340,7 +637,7 @@ namespace Modbus.Net /// 待转换的数组 /// 转换数字的位置 /// 转换出的数字 - public virtual uint GetUInt(byte[] data, ref int pos) + public override uint GetUInt(byte[] data, ref int pos) { var t = BitConverter.ToUInt32(data, pos); pos += 4; @@ -353,7 +650,7 @@ namespace Modbus.Net /// 待转换的数组 /// 转换数字的位置 /// 转换出的数字 - public virtual ulong GetULong(byte[] data, ref int pos) + public override ulong GetULong(byte[] data, ref int pos) { var t = BitConverter.ToUInt64(data, pos); pos += 8; @@ -366,7 +663,7 @@ namespace Modbus.Net /// 待转换的数组 /// 转换数字的位置 /// 转换出的数字 - public virtual float GetFloat(byte[] data, ref int pos) + public override float GetFloat(byte[] data, ref int pos) { var t = BitConverter.ToSingle(data, pos); pos += 4; @@ -379,7 +676,7 @@ namespace Modbus.Net /// 待转换的数组 /// 转换数字的位置 /// 转换出的数字 - public virtual double GetDouble(byte[] data, ref int pos) + public override double GetDouble(byte[] data, ref int pos) { var t = BitConverter.ToDouble(data, pos); pos += 8; @@ -394,7 +691,7 @@ namespace Modbus.Net /// 转换数字的位置 /// 编码方法 /// 转换出的字符串 - public virtual string GetString(byte[] data, int count, ref int pos, Encoding encoding) + public override string GetString(byte[] data, int count, ref int pos, Encoding encoding) { var t = encoding.GetString(data, pos, count); pos += count; @@ -407,7 +704,7 @@ namespace Modbus.Net /// 待转换的数组 /// 转换数字的位置 /// 转换出的位数组 - public virtual bool[] GetBits(byte[] data, ref int pos) + public override bool[] GetBits(byte[] data, ref int pos) { var t = new bool[8]; var temp = data[pos]; @@ -427,7 +724,7 @@ namespace Modbus.Net /// bit数组中的对应位置 /// 小数位 /// 对应位置的bit元素 - public bool GetBit(byte number, ref int pos, ref int subPos) + public override bool GetBit(byte number, ref int pos, ref int subPos) { if (subPos < 0 && subPos >= 8) throw new IndexOutOfRangeException(); var ans = number % 2; @@ -454,7 +751,7 @@ namespace Modbus.Net /// bit数组中的对应位置 /// 小数位 /// 对应位置的bit元素 - public virtual bool GetBit(byte[] number, ref int pos, ref int subPos) + public override bool GetBit(byte[] number, ref int pos, ref int subPos) { return GetBit(number[pos], ref pos, ref subPos); } @@ -464,7 +761,7 @@ namespace Modbus.Net /// /// 原始bit数组 /// 反转的bit数组 - public virtual byte ReverseByte(byte originalByte) + public override byte ReverseByte(byte originalByte) { byte result = 0; for (var i = 0; i < 8; i++) @@ -481,7 +778,7 @@ namespace Modbus.Net /// /// object数组 /// byte数组 - public virtual byte[] ObjectArrayToByteArray(object[] contents) + public override byte[] ObjectArrayToByteArray(object[] contents) { var b = false; //先查找传入的结构中有没有数组,有的话将其打开 @@ -604,7 +901,7 @@ namespace Modbus.Net /// byte数组 /// 单一的类型和需要转换的个数的键值对 /// object数组 - public virtual object[] ByteArrayToObjectArray(byte[] contents, KeyValuePair translateTypeAndCount) + public override object[] ByteArrayToObjectArray(byte[] contents, KeyValuePair translateTypeAndCount) { return ByteArrayToObjectArray(contents, new List> { translateTypeAndCount }); } @@ -616,7 +913,7 @@ namespace Modbus.Net /// 待转换的数组 /// 转换的个数 /// 以T为类型的数组 - public virtual T[] ByteArrayToDestinationArray(byte[] contents, int getCount) + public override T[] ByteArrayToDestinationArray(byte[] contents, int getCount) { var objectArray = _Instance.ByteArrayToObjectArray(contents, new KeyValuePair(typeof(T), getCount)); @@ -632,7 +929,7 @@ namespace Modbus.Net /// 会转换出8个元素(当然前提是byte数组足够长的时候),5个int和3个short,然后全部变为object类型返回。 /// /// object数组 - public virtual object[] ByteArrayToObjectArray(byte[] contents, + public override object[] ByteArrayToObjectArray(byte[] contents, IEnumerable> translateTypeAndCount) { var translation = new List(); @@ -729,7 +1026,7 @@ namespace Modbus.Net /// 需要转换的目标类型 /// object数组 /// 目标数组 - public T[] ObjectArrayToDestinationArray(object[] contents) + public override T[] ObjectArrayToDestinationArray(object[] contents) { var array = new T[contents.Length]; Array.Copy(contents, array, contents.Length); @@ -744,7 +1041,7 @@ namespace Modbus.Net /// 设置的比特位位置(仅setValue为bit的时候有效) /// 写入的值 /// 写入是否成功 - public bool SetValue(byte[] contents, int setPos, int subPos, object setValue) + public override bool SetValue(byte[] contents, int setPos, int subPos, object setValue) { var type = setValue.GetType(); @@ -814,7 +1111,7 @@ namespace Modbus.Net /// 设置的位置 /// 要设置的值 /// 设置是否成功 - public virtual bool SetValue(byte[] contents, int pos, object setValue) + public override bool SetValue(byte[] contents, int pos, object setValue) { try { @@ -864,7 +1161,7 @@ namespace Modbus.Net /// bit位置 /// bit数 /// 设置是否成功 - public virtual bool SetBit(byte[] contents, int pos, int subPos, bool setValue) + public override bool SetBit(byte[] contents, int pos, int subPos, bool setValue) { try { @@ -890,35 +1187,7 @@ namespace Modbus.Net /// /// ValueHelper单例的实例 /// - public static ValueHelper Instance => _instance ?? (_instance = new ValueHelper()); - - /// - /// 根据端格式获取ValueHelper实例 - /// - /// 端格式 - /// 对应的ValueHelper实例 - public static ValueHelper GetInstance(Endian endian) - { - switch (endian) - { - case Endian.LittleEndianLsb: - { - return Instance; - } - case Endian.BigEndianLsb: - { - return BigEndianValueHelper.Instance; - } - case Endian.BigEndianMsb: - { - return BigEndianMsbValueHelper.Instance; - } - default: - { - return Instance; - } - } - } + public static ValueHelper Instance => _instance ?? (_instance = new LittleEndianLsbValueHelper()); #endregion } @@ -926,14 +1195,14 @@ namespace Modbus.Net /// /// 值与字节数组之间转换的辅助类(大端格式),这是一个Singleton类 /// - public class BigEndianValueHelper : ValueHelper + public class BigEndianLsbValueHelper : LittleEndianLsbValueHelper { - private static BigEndianValueHelper _bigEndianInstance; + private static BigEndianLsbValueHelper _bigEndianInstance; /// /// 构造器 /// - protected BigEndianValueHelper() + protected BigEndianLsbValueHelper() { } @@ -950,8 +1219,8 @@ namespace Modbus.Net /// /// 覆盖的获取实例的方法 /// - public new static BigEndianValueHelper Instance - => _bigEndianInstance ?? (_bigEndianInstance = new BigEndianValueHelper()); + public new static BigEndianLsbValueHelper Instance + => _bigEndianInstance ?? (_bigEndianInstance = new BigEndianLsbValueHelper()); /// /// 将short数字转换为byte数组 @@ -1169,9 +1438,9 @@ namespace Modbus.Net /// /// 值与字节数组之间转换的辅助类(大端-大端位格式),这是一个Singleton类 /// - public class BigEndianMsbValueHelper : BigEndianValueHelper + public class BigEndianMsbValueHelper : BigEndianLsbValueHelper { - private static BigEndianValueHelper _bigEndianInstance; + private static BigEndianMsbValueHelper _bigEndianInstance; /// /// 构造函数 @@ -1198,7 +1467,7 @@ namespace Modbus.Net /// /// 覆盖的实例获取方法 /// - public new static BigEndianValueHelper Instance + public new static BigEndianMsbValueHelper Instance => _bigEndianInstance ?? (_bigEndianInstance = new BigEndianMsbValueHelper()); /// diff --git a/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs b/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs index 179db88..2c25604 100644 --- a/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs +++ b/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs @@ -124,7 +124,7 @@ namespace Modbus.Net AddressHelper.MapProtocolGetCountToAbstractByteCount( preNum - (int)Math.Floor(initNum), AddressTranslator.GetAreaByteLength(address.Area), - BigEndianValueHelper.Instance.ByteLength[preType.FullName])), + ValueHelper.ByteLength[preType.FullName])), DataType = typeof(byte), OriginalAddresses = originalAddresses.ToList() }); @@ -153,7 +153,7 @@ namespace Modbus.Net Math.Ceiling( AddressHelper.MapProtocolGetCountToAbstractByteCount( preNum - (int)Math.Floor(initNum), AddressTranslator.GetAreaByteLength(area), - BigEndianValueHelper.Instance.ByteLength[preType.FullName])), + ValueHelper.ByteLength[preType.FullName])), DataType = typeof(byte), OriginalAddresses = originalAddresses.ToList() }); @@ -173,9 +173,9 @@ namespace Modbus.Net foreach (var communicationUnit in ans) { var oldByteCount = communicationUnit.GetCount * - BigEndianValueHelper.Instance.ByteLength[communicationUnit.DataType.FullName]; + ValueHelper.ByteLength[communicationUnit.DataType.FullName]; var oldOriginalAddresses = communicationUnit.OriginalAddresses.ToList(); - while (oldByteCount * BigEndianValueHelper.Instance.ByteLength[communicationUnit.DataType.FullName] > + while (oldByteCount * ValueHelper.ByteLength[communicationUnit.DataType.FullName] > MaxLength) { var newOriginalAddresses = new List>(); @@ -184,9 +184,9 @@ namespace Modbus.Net do { var currentAddressUnit = oldOriginalAddresses.First(); - newByteCount += BigEndianValueHelper.Instance.ByteLength[currentAddressUnit.DataType.FullName]; + newByteCount += ValueHelper.ByteLength[currentAddressUnit.DataType.FullName]; if (newByteCount > MaxLength) break; - oldByteCount -= BigEndianValueHelper.Instance.ByteLength[currentAddressUnit.DataType.FullName]; + oldByteCount -= ValueHelper.ByteLength[currentAddressUnit.DataType.FullName]; newOriginalAddresses.Add(currentAddressUnit); oldOriginalAddresses.RemoveAt(0); } while (newByteCount < MaxLength); @@ -199,7 +199,7 @@ namespace Modbus.Net GetCount = (int) Math.Ceiling(newByteCount / - BigEndianValueHelper.Instance.ByteLength[communicationUnit.DataType.FullName]), + ValueHelper.ByteLength[communicationUnit.DataType.FullName]), OriginalAddresses = newOriginalAddresses }; @@ -214,7 +214,7 @@ namespace Modbus.Net communicationUnit.GetCount = (int) Math.Ceiling(oldByteCount / - BigEndianValueHelper.Instance.ByteLength[communicationUnit.DataType.FullName]); + ValueHelper.ByteLength[communicationUnit.DataType.FullName]); communicationUnit.OriginalAddresses = oldOriginalAddresses; newAns.Add(communicationUnit); } @@ -309,7 +309,7 @@ namespace Modbus.Net continusAddress.Address, preCommunicationUnit.Address, AddressTranslator.GetAreaByteLength(continusAddress.Area)) - preCommunicationUnit.GetCount * - BigEndianValueHelper.Instance.ByteLength[ + ValueHelper.ByteLength[ preCommunicationUnit.DataType.FullName]) }; addressesGaps.Add(gap); @@ -327,8 +327,8 @@ namespace Modbus.Net nowAddress = continusAddresses[index]; index--; var preAddress = continusAddresses[index]; - if (nowAddress.GetCount * BigEndianValueHelper.Instance.ByteLength[nowAddress.DataType.FullName] + - preAddress.GetCount * BigEndianValueHelper.Instance.ByteLength[preAddress.DataType.FullName] + + if (nowAddress.GetCount * ValueHelper.ByteLength[nowAddress.DataType.FullName] + + preAddress.GetCount * ValueHelper.ByteLength[preAddress.DataType.FullName] + orderedGap.GapNumber > MaxLength) continue; jumpNumberInner -= orderedGap.GapNumber; if (jumpNumberInner < 0) break; @@ -341,10 +341,10 @@ namespace Modbus.Net Address = preAddress.Address, GetCount = (int) - (preAddress.GetCount * BigEndianValueHelper.Instance.ByteLength[preAddress.DataType.FullName]) + + (preAddress.GetCount * ValueHelper.ByteLength[preAddress.DataType.FullName]) + orderedGap.GapNumber + (int) - (nowAddress.GetCount * BigEndianValueHelper.Instance.ByteLength[nowAddress.DataType.FullName]), + (nowAddress.GetCount * ValueHelper.ByteLength[nowAddress.DataType.FullName]), DataType = typeof(byte), OriginalAddresses = preAddress.OriginalAddresses.ToList().Union(nowAddress.OriginalAddresses) }; @@ -385,7 +385,7 @@ namespace Modbus.Net public override IEnumerable> Combine(IEnumerable> addresses) { var addressUnits = addresses as IList> ?? addresses.ToList(); - var count = addressUnits.Sum(address => BigEndianValueHelper.Instance.ByteLength[address.DataType.FullName]); + var count = addressUnits.Sum(address => ValueHelper.ByteLength[address.DataType.FullName]); return new AddressCombinerNumericJump((int)(count * Percentage / 100.0), MaxLength, AddressTranslator) .Combine( diff --git a/Modbus.Net/Modbus.Net/Machine/AddressHelper.cs b/Modbus.Net/Modbus.Net/Machine/AddressHelper.cs index ec4b4da..40db6b1 100644 --- a/Modbus.Net/Modbus.Net/Machine/AddressHelper.cs +++ b/Modbus.Net/Modbus.Net/Machine/AddressHelper.cs @@ -80,7 +80,7 @@ namespace Modbus.Net double byteLength) { return protocolAddress + - BigEndianValueHelper.Instance.ByteLength[nextPositionBetweenType.FullName] / byteLength; + ValueHelper.ByteLength[nextPositionBetweenType.FullName] / byteLength; } /// @@ -92,7 +92,7 @@ namespace Modbus.Net public static double GetAbstractCoordinateNextPosition(double abstractAddress, Type nextPositionBetweenType) { return abstractAddress + - BigEndianValueHelper.Instance.ByteLength[nextPositionBetweenType.FullName]; + ValueHelper.ByteLength[nextPositionBetweenType.FullName]; } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs index 8eb42b4..c78caf6 100644 --- a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs +++ b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs @@ -192,7 +192,7 @@ namespace Modbus.Net communicateAddress.SubAddress), (int) Math.Ceiling(communicateAddress.GetCount * - BigEndianValueHelper.Instance.ByteLength[ + ValueHelper.ByteLength[ communicateAddress.DataType.FullName])); @@ -210,7 +210,7 @@ namespace Modbus.Net else if (datas.Datas.Length != 0 && datas.Datas.Length < (int) Math.Ceiling(communicateAddress.GetCount * - BigEndianValueHelper.Instance.ByteLength[ + ValueHelper.ByteLength[ communicateAddress.DataType.FullName])) { return new ReturnStruct>>() @@ -437,7 +437,7 @@ namespace Modbus.Net AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address, 0), (int) Math.Ceiling(communicateAddress.GetCount * - BigEndianValueHelper.Instance.ByteLength[ + ValueHelper.ByteLength[ communicateAddress.DataType.FullName])); var valueHelper = ValueHelper.GetInstance(BaseUtility.Endian); @@ -458,7 +458,7 @@ namespace Modbus.Net else if (datas.Datas.Length < (int) Math.Ceiling(communicateAddress.GetCount * - BigEndianValueHelper.Instance.ByteLength[ + ValueHelper.ByteLength[ communicateAddress.DataType.FullName])) return new ReturnStruct() { diff --git a/Modbus.Net/Modbus.Net/README.md b/Modbus.Net/Modbus.Net/README.md index 17434dd..9cffc9c 100644 --- a/Modbus.Net/Modbus.Net/README.md +++ b/Modbus.Net/Modbus.Net/README.md @@ -288,7 +288,8 @@ But after ":", property should match constructor except protocol, which refer to The main target of Modbus.Net is building a high extensable hardware communication protocol, so we allow everyone to extend the protocol. -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. +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 three ValueHelpers: LittleEndianLsbValueHelper(Little Endian), BigEndianLsbValueHelper(Big Endian) and BigEndianMsbValueHelper(Big Endian with bit reverse). Remember using the correct one.
+If you want to write a new one, just write in namespace "Modbus.Net" in assmenly start with "Modbus.Net." and Modbus.Net will automatically load it. In this tutorial I will use Modbus.Net.Modbus to tell you how to implement your own protocol. @@ -314,9 +315,9 @@ public class ReadDataModbusProtocol : ProtocolUnit public override IOutputStruct Unformat(byte[] messageBytes, ref int pos) { - byte slaveAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); - byte functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); - byte dataCount = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos); + byte slaveAddress = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); + byte functionCode = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); + byte dataCount = BigEndianLsbValueHelper.Instance.GetByte(messageBytes, ref pos); byte[] dataValue = new byte[dataCount]; Array.Copy(messageBytes, 3, dataValue, 0, dataCount); return new ReadDataModbusOutputStruct(slaveAddress, functionCode, dataCount, dataValue); @@ -368,8 +369,8 @@ public class ModbusTcpProtocolLinkerBytesExtend : ProtocolLinkerBytesExtend byte[] newFormat = new byte[6 + content.Length]; int tag = 0; ushort leng = (ushort)content.Length; - Array.Copy(BigEndianValueHelper.Instance.GetBytes(tag), 0, newFormat, 0, 4); - Array.Copy(BigEndianValueHelper.Instance.GetBytes(leng), 0, newFormat, 4, 2); + Array.Copy(BigEndianLsbValueHelper.Instance.GetBytes(tag), 0, newFormat, 0, 4); + Array.Copy(BigEndianLsbValueHelper.Instance.GetBytes(leng), 0, newFormat, 4, 2); Array.Copy(content, 0, newFormat, 6, content.Length); return newFormat; } diff --git a/Modbus.Net/Modbus.Net/Utility/BaseUtility.cs b/Modbus.Net/Modbus.Net/Utility/BaseUtility.cs index 313f23f..b0d45c7 100644 --- a/Modbus.Net/Modbus.Net/Utility/BaseUtility.cs +++ b/Modbus.Net/Modbus.Net/Utility/BaseUtility.cs @@ -109,7 +109,7 @@ namespace Modbus.Net try { var typeName = getTypeAndCount.Key.FullName; - var bCount = BigEndianValueHelper.Instance.ByteLength[typeName]; + var bCount = ValueHelper.ByteLength[typeName]; var getReturnValue = await GetDatasAsync(startAddress, (int)Math.Ceiling(bCount * getTypeAndCount.Value)); var getBytes = getReturnValue; @@ -230,7 +230,7 @@ namespace Modbus.Net var bAllCount = ( from getTypeAndCount in translateTypeAndCount let typeName = getTypeAndCount.Key.FullName - let bCount = BigEndianValueHelper.Instance.ByteLength[typeName] + let bCount = ValueHelper.ByteLength[typeName] select (int)Math.Ceiling(bCount * getTypeAndCount.Value)).Sum(); var getReturnValue = await GetDatasAsync(startAddress, bAllCount); var getBytes = getReturnValue; diff --git a/Samples/CrossLamp/Controllers/HomeController.cs b/Samples/CrossLamp/Controllers/HomeController.cs index e336dc9..21c5666 100644 --- a/Samples/CrossLamp/Controllers/HomeController.cs +++ b/Samples/CrossLamp/Controllers/HomeController.cs @@ -35,7 +35,7 @@ namespace CrossLamp.Controllers } Lamp light = new Lamp(); object[] lampsbyte = (await _utility.GetDatasAsync("0X 1", new KeyValuePair(typeof(bool), 7))).Datas; - bool[] lamps = BigEndianValueHelper.Instance.ObjectArrayToDestinationArray(lampsbyte); + bool[] lamps = BigEndianLsbValueHelper.Instance.ObjectArrayToDestinationArray(lampsbyte); if (lamps[0]) { light.MainLamp = LightLamp.Red.ToString(); diff --git a/Samples/TripleAdd/Controllers/HomeController.cs b/Samples/TripleAdd/Controllers/HomeController.cs index 99c1ae1..b760401 100644 --- a/Samples/TripleAdd/Controllers/HomeController.cs +++ b/Samples/TripleAdd/Controllers/HomeController.cs @@ -46,7 +46,7 @@ namespace TripleAdd.Controllers await utility.ConnectAsync(); } object[] getNum = (await utility.GetDatasAsync("4X 1", new KeyValuePair(typeof(ushort), 4))).Datas; - ushort[] getNumUshorts = BigEndianValueHelper.Instance.ObjectArrayToDestinationArray(getNum); + ushort[] getNumUshorts = BigEndianLsbValueHelper.Instance.ObjectArrayToDestinationArray(getNum); return SetValue(getNumUshorts); }