diff --git a/Modbus.Net/Modbus.Net.FBox/AddressTranslatorFBox.cs b/Modbus.Net/Modbus.Net.FBox/AddressTranslatorFBox.cs index e5afca3..b1c99a0 100644 --- a/Modbus.Net/Modbus.Net.FBox/AddressTranslatorFBox.cs +++ b/Modbus.Net/Modbus.Net.FBox/AddressTranslatorFBox.cs @@ -39,7 +39,7 @@ namespace Modbus.Net.FBox }; } - public override KeyValuePair AddressTranslate(string address, bool isRead) + public override AddressDef AddressTranslate(string address, bool isRead) { var tmpAddress = address.Trim().ToUpper(); if (tmpAddress.Substring(0, 2) == "DB") @@ -47,15 +47,21 @@ namespace Modbus.Net.FBox var addressSplit = tmpAddress.Split(' '); if (addressSplit.Length != 2) throw new FormatException(); addressSplit[0] = addressSplit[0].Substring(2); - return new KeyValuePair(int.Parse(addressSplit[1]), - int.Parse(addressSplit[0]) + AreaCodeDictionary["DB"]); + return new AddressDef() + { + Area = int.Parse(addressSplit[0]) + AreaCodeDictionary["DB"], + Address = int.Parse(addressSplit[1]) + }; } var tmpAddressArray = (tmpAddress.Split(' ')); string head = tmpAddressArray[0]; string tail = tmpAddressArray[1]; return - new KeyValuePair(int.Parse(tail), - AreaCodeDictionary[head]); + new AddressDef() + { + Area = AreaCodeDictionary[head], + Address = int.Parse(tail) + }; } public string GetAreaName(int code) diff --git a/Modbus.Net/Modbus.Net.FBox/FBoxProtocal.cs b/Modbus.Net/Modbus.Net.FBox/FBoxProtocal.cs index 425c512..5bf0f95 100644 --- a/Modbus.Net/Modbus.Net.FBox/FBoxProtocal.cs +++ b/Modbus.Net/Modbus.Net.FBox/FBoxProtocal.cs @@ -26,8 +26,8 @@ namespace Modbus.Net.FBox public ReadRequestFBoxInputStruct(string startAddress, ushort getCount, AddressTranslator addressTranslator) { var address = addressTranslator.AddressTranslate(startAddress, true); - Address = address.Key; - Area = address.Value; + Address = address.Address; + Area = address.Area; GetCount = getCount; } diff --git a/Modbus.Net/Modbus.Net.FBox/FBoxUtility.cs b/Modbus.Net/Modbus.Net.FBox/FBoxUtility.cs index d59d8f3..ebaa7f7 100644 --- a/Modbus.Net/Modbus.Net.FBox/FBoxUtility.cs +++ b/Modbus.Net/Modbus.Net.FBox/FBoxUtility.cs @@ -60,7 +60,7 @@ namespace Modbus.Net.FBox ConnectionType = (FBoxType) connectionType; } - protected override async Task GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount) + public override async Task GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount) { try { @@ -68,7 +68,11 @@ namespace Modbus.Net.FBox var readRequestSiemensOutputStruct = await Wrapper.SendReceiveAsync(Wrapper[typeof(ReadRequestFBoxProtocal)], readRequestFBoxInputStruct) as ReadRequestFBoxOutputStruct; - return readRequestSiemensOutputStruct?.GetValue; + return new GetDataReturnDef() + { + ReturnValue = readRequestSiemensOutputStruct?.GetValue, + IsLittleEndian = Wrapper[typeof (ReadRequestFBoxProtocal)].IsLittleEndian + }; } catch (Exception) { diff --git a/Modbus.Net/Modbus.Net.FBox/Modbus.Net.FBox.nuspec b/Modbus.Net/Modbus.Net.FBox/Modbus.Net.FBox.nuspec index 7d08634..c18ffdd 100644 --- a/Modbus.Net/Modbus.Net.FBox/Modbus.Net.FBox.nuspec +++ b/Modbus.Net/Modbus.Net.FBox/Modbus.Net.FBox.nuspec @@ -2,7 +2,7 @@ Modbus.Net.FBox - 1.0.0 + 1.0.1 Modbus.Net.FBox Chris L.(Luo Sheng) Hangzhou Delian Information and Science Technology Co., Ltd. @@ -13,7 +13,7 @@ Copyright 2015 Hangzhou Delian Science and Technology Co.,Ltd. hardware communicate protocal fbox Delian - + diff --git a/Modbus.Net/Modbus.Net.FBox/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net.FBox/Properties/AssemblyInfo.cs index 8303f9e..7f84241 100644 --- a/Modbus.Net/Modbus.Net.FBox/Properties/AssemblyInfo.cs +++ b/Modbus.Net/Modbus.Net.FBox/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, // 方法是按如下所示使用“*”: : // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.0.1")] +[assembly: AssemblyFileVersion("1.0.1")] diff --git a/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs b/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs index 3a77cf0..a4b5923 100644 --- a/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs +++ b/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs @@ -54,17 +54,23 @@ namespace Modbus.Net.Modbus }; } - public override KeyValuePair AddressTranslate(string address, bool isRead) + public override AddressDef AddressTranslate(string address, bool isRead) { address = address.ToUpper(); string[] splitString = address.Split(' '); string head = splitString[0]; string tail = splitString[1]; return isRead - ? new KeyValuePair(TransDictionary[head] + int.Parse(tail) - 1, - ReadFunctionCodeDictionary[head]) - : new KeyValuePair(TransDictionary[head] + int.Parse(tail) - 1, - WriteFunctionCodeDictionary[head]); + ? new AddressDef() + { + Area = ReadFunctionCodeDictionary[head], + Address = TransDictionary[head] + int.Parse(tail) - 1, + } + : new AddressDef() + { + Area = WriteFunctionCodeDictionary[head], + Address = TransDictionary[head] + int.Parse(tail) - 1, + }; } } @@ -92,17 +98,23 @@ namespace Modbus.Net.Modbus }; } - public override KeyValuePair AddressTranslate(string address, bool isRead) + public override AddressDef AddressTranslate(string address, bool isRead) { address = address.ToUpper(); string[] splitString = address.Split(' '); string head = splitString[0]; string tail = splitString[1]; return isRead - ? new KeyValuePair(int.Parse(tail) - 1, - ReadFunctionCodeDictionary[head]) - : new KeyValuePair(int.Parse(tail) - 1, - WriteFunctionCodeDictionary[head]); + ? new AddressDef() + { + Area = ReadFunctionCodeDictionary[head], + Address = int.Parse(tail) - 1, + } + : new AddressDef() + { + Area = WriteFunctionCodeDictionary[head], + Address = int.Parse(tail) - 1, + }; } } } diff --git a/Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus.nuspec b/Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus.nuspec index 5205c31..c0c2c80 100644 --- a/Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus.nuspec +++ b/Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus.nuspec @@ -2,7 +2,7 @@ Modbus.Net.Modbus - 1.0.0 + 1.1.0 Modbus.Net.Modbus Chris L.(Luo Sheng) Hangzhou Delian Information and Science Technology Co.,Ltd. @@ -13,7 +13,7 @@ Copyright 2015 Hangzhou Delian Science and Technology Co.,Ltd. hardware communicate protocal modbus Delian - + diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocal.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocal.cs index 195bcac..ca303be 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocal.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocal.cs @@ -58,9 +58,9 @@ namespace Modbus.Net.Modbus public ReadDataModbusInputStruct(byte belongAddress, string startAddress, ushort getCount, AddressTranslator addressTranslator) { BelongAddress = belongAddress; - KeyValuePair translateAddress = addressTranslator.AddressTranslate(startAddress, true); - FunctionCode = (byte)translateAddress.Value; - StartAddress = (ushort)translateAddress.Key; + var translateAddress = addressTranslator.AddressTranslate(startAddress, true); + FunctionCode = (byte)translateAddress.Area; + StartAddress = (ushort)translateAddress.Address; GetCount = getCount; } @@ -121,9 +121,9 @@ namespace Modbus.Net.Modbus public WriteDataModbusInputStruct(byte belongAddress, string startAddress, object[] writeValue, AddressTranslator addressTranslator) { BelongAddress = belongAddress; - KeyValuePair translateAddress = addressTranslator.AddressTranslate(startAddress, false); - FunctionCode = (byte)translateAddress.Value; - StartAddress = (ushort)translateAddress.Key; + var translateAddress = addressTranslator.AddressTranslate(startAddress, false); + FunctionCode = (byte)translateAddress.Area; + StartAddress = (ushort)translateAddress.Address; WriteCount = (ushort)writeValue.Length; WriteByteCount = 0; WriteValue = writeValue; diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs index 642132e..c8245be 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs @@ -65,7 +65,7 @@ namespace Modbus.Net.Modbus ModbusType = (ModbusType) connectionType; } - protected override async Task GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount) + public override async Task GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount) { try { @@ -74,7 +74,11 @@ namespace Modbus.Net.Modbus var outputStruct = await Wrapper.SendReceiveAsync(Wrapper[typeof (ReadDataModbusProtocal)], inputStruct) as ReadDataModbusOutputStruct; - return outputStruct?.DataValue; + return new GetDataReturnDef() + { + ReturnValue = outputStruct?.DataValue, + IsLittleEndian = Wrapper[typeof(ReadDataModbusProtocal)].IsLittleEndian, + }; } catch { diff --git a/Modbus.Net/Modbus.Net.Modbus/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net.Modbus/Properties/AssemblyInfo.cs index 5faedd1..77358cf 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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.1.0")] +[assembly: AssemblyFileVersion("1.1.0")] diff --git a/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.nuspec b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.nuspec index 0024262..bc2d1e6 100644 --- a/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.nuspec +++ b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.nuspec @@ -2,7 +2,7 @@ Modbus.Net.OPC - 1.0.0 + 1.1.0 Modbus.Net.OPC Chris L.(Luo Sheng) Hangzhou Delian Information and Science Technology @@ -13,7 +13,7 @@ Copyright 2015 Hangzhou Delian Science and Technology Co.,Ltd. hardware communicate protocal OPC DA Delian - + diff --git a/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs b/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs index 9f88ecc..ca3d84b 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs @@ -19,7 +19,7 @@ namespace Modbus.Net.OPC { } - protected override async Task GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount) + public override async Task GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount) { try { @@ -27,7 +27,11 @@ namespace Modbus.Net.OPC var readRequestOpcOutputStruct = await Wrapper.SendReceiveAsync(Wrapper[typeof(ReadRequestOpcProtocal)], readRequestOpcInputStruct) as ReadRequestOpcOutputStruct; - return readRequestOpcOutputStruct?.GetValue; + return new GetDataReturnDef() + { + ReturnValue = readRequestOpcOutputStruct?.GetValue, + IsLittleEndian = Wrapper[typeof (ReadRequestOpcProtocal)].IsLittleEndian + }; } catch (Exception) { diff --git a/Modbus.Net/Modbus.Net.OPC/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net.OPC/Properties/AssemblyInfo.cs index 6770401..21b2727 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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.1.0")] +[assembly: AssemblyFileVersion("1.1.0")] diff --git a/Modbus.Net/Modbus.Net.Siemens/AddressTranslatorSiemens.cs b/Modbus.Net/Modbus.Net.Siemens/AddressTranslatorSiemens.cs index daae1cd..cae1a48 100644 --- a/Modbus.Net/Modbus.Net.Siemens/AddressTranslatorSiemens.cs +++ b/Modbus.Net/Modbus.Net.Siemens/AddressTranslatorSiemens.cs @@ -26,9 +26,10 @@ namespace Modbus.Net.Siemens }; } - public override KeyValuePair AddressTranslate(string address, bool isRead) + public override AddressDef AddressTranslate(string address, bool isRead) { - /*address = address.ToUpper(); + /* + address = address.ToUpper(); if (address.Substring(0, 2) == "DB") { var addressSplit = address.Split('.'); @@ -36,8 +37,11 @@ namespace Modbus.Net.Siemens addressSplit[0] = addressSplit[0].Substring(2); if (addressSplit[1].Substring(0, 2) == "DB") addressSplit[1] = addressSplit[1].Substring(2); - return new KeyValuePair(int.Parse(addressSplit[1]), - int.Parse(addressSplit[0])*256 + AreaCodeDictionary["DB"]); + return new AddressDef() + { + Area = int.Parse(addressSplit[0])*256 + AreaCodeDictionary["DB"], + Address = int.Parse(addressSplit[1]) + }; } int i = 0; int t; @@ -49,8 +53,11 @@ namespace Modbus.Net.Siemens string head = address.Substring(0, i); string tail = address.Substring(i); return - new KeyValuePair(int.Parse(tail), - AreaCodeDictionary[head]); + new AddressDef() + { + Area = AreaCodeDictionary[head], + Address = int.Parse(tail) + }; */ string[] splitString = address.Split(' '); string head = splitString[0]; @@ -58,12 +65,18 @@ namespace Modbus.Net.Siemens if (head.Length > 1 && head.Substring(0, 2) == "DB") { head = head.Substring(2); - return new KeyValuePair(int.Parse(tail), - int.Parse(head)*256 + AreaCodeDictionary["DB"]); + return new AddressDef() + { + Area = int.Parse(head)*256 + AreaCodeDictionary["DB"], + Address = int.Parse(tail) + }; } return - new KeyValuePair(int.Parse(tail), - AreaCodeDictionary[head]); + new AddressDef() + { + Area = AreaCodeDictionary[head], + Address = int.Parse(tail) + }; } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens.nuspec b/Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens.nuspec index 43c96d1..c1dff2e 100644 --- a/Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens.nuspec +++ b/Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens.nuspec @@ -2,7 +2,7 @@ Modbus.Net.Siemens - 1.0.0 + 1.1.0 Modbus.Net.Siemens Chris L.(Luo Sheng) Hangzhou Delian Information and Science Technology Co.,Ltd. @@ -13,7 +13,7 @@ Copyright 2015 Hangzhou Delian Science and Technology Co.,Ltd. hardware communicate protocal Siemens profinet Delian - + diff --git a/Modbus.Net/Modbus.Net.Siemens/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net.Siemens/Properties/AssemblyInfo.cs index 36fa095..80773a8 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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.1.0")] +[assembly: AssemblyFileVersion("1.1.0")] diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocal.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocal.cs index 70c521a..5a62444 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocal.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocal.cs @@ -190,8 +190,8 @@ namespace Modbus.Net.Siemens PduRef = pduRef; TypeCode = (byte) getType; var address = addressTranslator.AddressTranslate(startAddress, true); - Offset = address.Key; - int area = address.Value; + Offset = address.Address; + int area = address.Area; Area = (byte)(area%256); DbBlock = Area == 0x84 ? (ushort)(area/256) : (ushort)0; NumberOfElements = getCount; @@ -272,8 +272,8 @@ namespace Modbus.Net.Siemens { PduRef = pduRef; var address = addressTranslator.AddressTranslate(startAddress, true); - Offset = address.Key; - int area = address.Value; + Offset = address.Address; + int area = address.Area; Area = (byte)(area % 256); DbBlock = Area == 0x84 ? (ushort)(area / 256) : (ushort)0; WriteValue = writeValue; diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocal.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocal.cs index 90848d1..7841e4d 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocal.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocal.cs @@ -30,18 +30,18 @@ namespace Modbus.Net.Siemens _connectTryCount = 0; } - public override byte[] SendReceive(params object[] content) + public override byte[] SendReceive(bool isLittleEndian, params object[] content) { - return AsyncHelper.RunSync(() => SendReceiveAsync(content)); + return AsyncHelper.RunSync(() => SendReceiveAsync(isLittleEndian, content)); } - public override async Task SendReceiveAsync(params object[] content) + public override async Task SendReceiveAsync(bool isLittleEndian, params object[] content) { if (ProtocalLinker == null || !ProtocalLinker.IsConnected) { await ConnectAsync(); } - return await base.SendReceiveAsync(content); + return await base.SendReceiveAsync(isLittleEndian, content); } public override OutputStruct SendReceive(ProtocalUnit unit, InputStruct content) diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs index 6cfdfb7..9077889 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs @@ -121,7 +121,7 @@ namespace Modbus.Net.Siemens ConnectionType = (SiemensType) connectionType; } - protected override async Task GetDatasAsync(byte belongAddress, byte materAddress, string startAddress, int getByteCount) + public override async Task GetDatasAsync(byte belongAddress, byte materAddress, string startAddress, int getByteCount) { try { @@ -130,7 +130,11 @@ namespace Modbus.Net.Siemens await Wrapper.SendReceiveAsync(Wrapper[typeof (ReadRequestSiemensProtocal)], readRequestSiemensInputStruct) as ReadRequestSiemensOutputStruct; - return readRequestSiemensOutputStruct?.GetValue; + return new GetDataReturnDef() + { + ReturnValue = readRequestSiemensOutputStruct?.GetValue, + IsLittleEndian = Wrapper[typeof (ReadRequestSiemensProtocal)].IsLittleEndian + }; } catch (Exception) { diff --git a/Modbus.Net/Modbus.Net/AddressTranslator.cs b/Modbus.Net/Modbus.Net/AddressTranslator.cs index f08c6b9..ce95ffa 100644 --- a/Modbus.Net/Modbus.Net/AddressTranslator.cs +++ b/Modbus.Net/Modbus.Net/AddressTranslator.cs @@ -3,6 +3,12 @@ using System.Collections.Generic; namespace Modbus.Net { + public class AddressDef + { + public int Area { get; set; } + public int Address { get; set; } + } + /// /// 地址翻译器 /// @@ -14,7 +20,7 @@ namespace Modbus.Net /// 地址前地址 /// 是否为读取,是为读取,否为写入 /// Key为转换后的地址,Value为辅助码 - public abstract KeyValuePair AddressTranslate(string address, bool isRead); + public abstract AddressDef AddressTranslate(string address, bool isRead); } /// @@ -22,14 +28,18 @@ namespace Modbus.Net /// public class AddressTranslatorBase : AddressTranslator { - public override KeyValuePair AddressTranslate(string address, bool isRead) + public override AddressDef AddressTranslate(string address, bool isRead) { int num1,num2; string[] split = address.Split(':'); if (split.Length != 2) throw new FormatException(); if (int.TryParse(split[0], out num1) && int.TryParse(split[1], out num2)) { - return new KeyValuePair(num2, num1); + return new AddressDef() + { + Area = num1, + Address = num2 + }; } throw new FormatException(); } diff --git a/Modbus.Net/Modbus.Net/BaseMachine.cs b/Modbus.Net/Modbus.Net/BaseMachine.cs index a6d51f2..69a6588 100644 --- a/Modbus.Net/Modbus.Net/BaseMachine.cs +++ b/Modbus.Net/Modbus.Net/BaseMachine.cs @@ -135,14 +135,16 @@ namespace Modbus.Net foreach (var communicateAddress in CommunicateAddresses) { //获取数据 - var datas = + var datasReturn = await - BaseUtility.GetDatasAsync(2, 0, + BaseUtility.GetDatasAsync(2, 0, AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address), (int) Math.Ceiling(communicateAddress.GetCount* BigEndianValueHelper.Instance.ByteLength[ communicateAddress.DataType.FullName])); + var datas = datasReturn.ReturnValue; + //如果没有数据,终止 if (datas == null || datas.Length == 0 || datas.Length != (int) @@ -165,8 +167,9 @@ namespace Modbus.Net { PlcValue = Double.Parse( - BigEndianValueHelper.Instance.GetValue(datas, ref pos, address.DataType) - .ToString())*address.Zoom, + datasReturn.IsLittleEndian ? ValueHelper.Instance.GetValue(datas, ref pos, address.DataType) + .ToString() : BigEndianValueHelper.Instance.GetValue(datas, ref pos, address.DataType) + .ToString()) *address.Zoom, UnitExtend = address.UnitExtend }); } diff --git a/Modbus.Net/Modbus.Net/BaseProtocal.cs b/Modbus.Net/Modbus.Net/BaseProtocal.cs index 2b1584b..7165bf7 100644 --- a/Modbus.Net/Modbus.Net/BaseProtocal.cs +++ b/Modbus.Net/Modbus.Net/BaseProtocal.cs @@ -64,19 +64,21 @@ namespace Modbus.Net /// /// 发送协议内容并接收,一般方法 /// + /// 是否是小端格式 /// 写入的内容,使用对象数组描述 /// 从设备获取的字节流 - public virtual byte[] SendReceive(params object[] content) + public virtual byte[] SendReceive(bool isLittleEndian, params object[] content) { - return AsyncHelper.RunSync(() => SendReceiveAsync(content)); + return AsyncHelper.RunSync(() => SendReceiveAsync(isLittleEndian, content)); } /// /// 发送协议内容并接收,一般方法 /// + /// 是否是小端格式 /// 写入的内容,使用对象数组描述 /// 从设备获取的字节流 - public virtual async Task SendReceiveAsync(params object[] content) + public virtual async Task SendReceiveAsync(bool isLittleEndian, params object[] content) { if (ProtocalLinker == null || !ProtocalLinker.IsConnected) { @@ -84,7 +86,7 @@ namespace Modbus.Net } if (ProtocalLinker != null) { - return await ProtocalLinker.SendReceiveAsync(ProtocalUnit.TranslateContent(content)); + return await ProtocalLinker.SendReceiveAsync(ProtocalUnit.TranslateContent(isLittleEndian, content)); } else { diff --git a/Modbus.Net/Modbus.Net/BaseUtility.cs b/Modbus.Net/Modbus.Net/BaseUtility.cs index d29b5bf..eb8b883 100644 --- a/Modbus.Net/Modbus.Net/BaseUtility.cs +++ b/Modbus.Net/Modbus.Net/BaseUtility.cs @@ -5,6 +5,12 @@ using System.Threading.Tasks; namespace Modbus.Net { + public class GetDataReturnDef + { + public byte[] ReturnValue { get; set; } + public bool IsLittleEndian { get; set; } + } + public abstract class BaseUtility { /// @@ -50,7 +56,7 @@ namespace Modbus.Net /// 开始地址 /// 获取字节数个数 /// 接收到的byte数据 - protected virtual byte[] GetDatas(byte belongAddress, byte masterAddress, string startAddress, int getByteCount) + public virtual GetDataReturnDef GetDatas(byte belongAddress, byte masterAddress, string startAddress, int getByteCount) { return AsyncHelper.RunSync(() => GetDatasAsync(belongAddress, masterAddress, startAddress, getByteCount)); } @@ -63,7 +69,7 @@ namespace Modbus.Net /// 开始地址 /// 获取字节数个数 /// 接收到的byte数据 - protected abstract Task GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount); + public abstract Task GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount); /// /// 获取数据 @@ -94,9 +100,12 @@ namespace Modbus.Net { string typeName = getTypeAndCount.Key.FullName; double bCount = BigEndianValueHelper.Instance.ByteLength[typeName]; - byte[] getBytes = await GetDatasAsync(belongAddress, masterAddress, startAddress, - (int) Math.Ceiling(bCount*getTypeAndCount.Value)); - return BigEndianValueHelper.Instance.ByteArrayToObjectArray(getBytes, getTypeAndCount); + var getReturnValue = await GetDatasAsync(belongAddress, masterAddress, startAddress, + (int)Math.Ceiling(bCount * getTypeAndCount.Value)); + var getBytes = getReturnValue.ReturnValue; + return getReturnValue.IsLittleEndian + ? ValueHelper.Instance.ByteArrayToObjectArray(getBytes, getTypeAndCount) + : BigEndianValueHelper.Instance.ByteArrayToObjectArray(getBytes, getTypeAndCount); } catch (Exception) { @@ -177,8 +186,11 @@ namespace Modbus.Net let typeName = getTypeAndCount.Key.FullName let bCount = BigEndianValueHelper.Instance.ByteLength[typeName] select (int) Math.Ceiling(bCount*getTypeAndCount.Value)).Sum(); - byte[] getBytes = await GetDatasAsync(belongAddress, masterAddress, startAddress, bAllCount); - return BigEndianValueHelper.Instance.ByteArrayToObjectArray(getBytes, translateTypeAndCount); + var getReturnValue = await GetDatasAsync(belongAddress, masterAddress, startAddress, bAllCount); + byte[] getBytes = getReturnValue.ReturnValue; + return getReturnValue.IsLittleEndian + ? ValueHelper.Instance.ByteArrayToObjectArray(getBytes, translateTypeAndCount) + : BigEndianValueHelper.Instance.ByteArrayToObjectArray(getBytes, translateTypeAndCount); } catch (Exception) { diff --git a/Modbus.Net/Modbus.Net/ModBus.Net.nuspec b/Modbus.Net/Modbus.Net/ModBus.Net.nuspec index 8cebae7..63ec49f 100644 --- a/Modbus.Net/Modbus.Net/ModBus.Net.nuspec +++ b/Modbus.Net/Modbus.Net/ModBus.Net.nuspec @@ -2,7 +2,7 @@ Modbus.Net - 1.0.0 + 1.1.0 Modbus.Net Chris L.(Luo Sheng) Hangzhou Delian Information and Science Technology Co.,Ltd. diff --git a/Modbus.Net/Modbus.Net/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net/Properties/AssemblyInfo.cs index d0130fd..e94cc1c 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.0.0")] -[assembly: AssemblyFileVersion("1.0.0")] \ No newline at end of file +[assembly: AssemblyVersion("1.1.0")] +[assembly: AssemblyFileVersion("1.1.0")] \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/ProtocalUnit.cs b/Modbus.Net/Modbus.Net/ProtocalUnit.cs index 7b4fdc0..7f1255e 100644 --- a/Modbus.Net/Modbus.Net/ProtocalUnit.cs +++ b/Modbus.Net/Modbus.Net/ProtocalUnit.cs @@ -4,6 +4,8 @@ namespace Modbus.Net { public abstract class ProtocalUnit : IProtocalFormatting { + public bool IsLittleEndian { get; protected set; } = false; + /// /// 从输入结构格式化 /// @@ -18,7 +20,7 @@ namespace Modbus.Net /// 格式化后的字节流 public virtual byte[] Format(params object[] message) { - return TranslateContent(message); + return TranslateContent(IsLittleEndian, message); } /// @@ -32,17 +34,19 @@ namespace Modbus.Net /// /// 转换静态方法,把对象数组转换为字节数组。 /// + /// 是否是小端格式 /// 对象数组 /// 字节数组 - public static byte[] TranslateContent(params object[] contents) + public static byte[] TranslateContent(bool isLittleEndian, params object[] contents) { - return BigEndianValueHelper.Instance.ObjectArrayToByteArray(contents); + return isLittleEndian + ? ValueHelper.Instance.ObjectArrayToByteArray(contents) + : BigEndianValueHelper.Instance.ObjectArrayToByteArray(contents); } } public abstract class SpecialProtocalUnit : ProtocalUnit { - } /// diff --git a/Modbus.Net/Modbus.Net/TaskManager.cs b/Modbus.Net/Modbus.Net/TaskManager.cs index 4fd09ca..2ff3103 100644 --- a/Modbus.Net/Modbus.Net/TaskManager.cs +++ b/Modbus.Net/Modbus.Net/TaskManager.cs @@ -7,6 +7,12 @@ using System.Threading.Tasks; namespace Modbus.Net { + public class TaskReturnDef + { + public int MachineId { get; set; } + public Dictionary ReturnValues { get; set; } + } + public static class TimeRestore { public static int Restore = 0; @@ -215,7 +221,7 @@ namespace Modbus.Net /// 返回数据代理 /// /// - public delegate void ReturnValuesDelegate(KeyValuePair> returnValue); + public delegate void ReturnValuesDelegate(TaskReturnDef returnValue); /// /// 返回数据事件 @@ -559,7 +565,11 @@ namespace Modbus.Net { MoveMachineToLinked(machine.Id); } - ReturnValues?.Invoke(new KeyValuePair>(machine.Id, ans)); + ReturnValues?.Invoke(new TaskReturnDef() + { + MachineId = machine.Id, + ReturnValues = ans + }); } catch (Exception e) { @@ -567,7 +577,11 @@ namespace Modbus.Net { MoveMachineToUnlinked(machine.Id); } - ReturnValues?.Invoke(new KeyValuePair>(machine.Id, null)); + ReturnValues?.Invoke(new TaskReturnDef() + { + MachineId = machine.Id, + ReturnValues = null + }); } } } diff --git a/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs b/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs index b3f7726..045bde1 100644 --- a/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs +++ b/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs @@ -52,10 +52,10 @@ namespace NA200H.UI.WPF //machine.AddressTranslator = new AddressTranslatorNA200H(); machine = new SiemensMachine(SiemensType.Tcp, "192.168.3.11", SiemensMachineModel.S7_300, new List() { - new AddressUnit() {Id = 1, Area = "V", Address = 1, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, - new AddressUnit() {Id = 2, Area = "V", Address = 3, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, - new AddressUnit() {Id = 3, Area = "V", Address = 5, CommunicationTag = "Add3", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, - new AddressUnit() {Id = 4, Area = "V", Address = 7, CommunicationTag = "Ans", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0} + new AddressUnit() {Id = 1, Area = "V", Address = 0, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + new AddressUnit() {Id = 2, Area = "V", Address = 2, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + new AddressUnit() {Id = 3, Area = "V", Address = 4, CommunicationTag = "Add3", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + new AddressUnit() {Id = 4, Area = "V", Address = 6, CommunicationTag = "Ans", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0} }); var result = machine.GetDatas(); var resultFormat = BaseMachine.MapGetValuesToSetValues(result); diff --git a/Modbus.Net/Simense_S7_200.UI.WPF.TaskTest/MainWindow.xaml.cs b/Modbus.Net/Simense_S7_200.UI.WPF.TaskTest/MainWindow.xaml.cs index e0e8d25..396f2e7 100644 --- a/Modbus.Net/Simense_S7_200.UI.WPF.TaskTest/MainWindow.xaml.cs +++ b/Modbus.Net/Simense_S7_200.UI.WPF.TaskTest/MainWindow.xaml.cs @@ -43,14 +43,14 @@ namespace Siemens_S7_200.UI.WPF.TaskTest { //唯一的参数包含返回值,是一个唯一标识符(machine的第二个参数),返回值(类型ReturnUnit)的键值对。 value = new List(); - if (returnValues.Value != null) + if (returnValues.ReturnValues != null) { - value = from val in returnValues.Value select val.Key + " " + val.Value.PlcValue; + value = from val in returnValues.ReturnValues select val.Key + " " + val.Value.PlcValue; siemensItems.Dispatcher.Invoke(() => siemensItems.ItemsSource = value); } else { - Console.WriteLine($"ip {returnValues.Key} not return value"); + Console.WriteLine($"ip {returnValues.MachineId} not return value"); } }; //启动任务