2016-01-07 update 1 version 1.1 support little endian protocal and change the description of TaskManager ReturnValue and AddressTranslator.
This commit is contained in:
@@ -39,7 +39,7 @@ namespace Modbus.Net.FBox
|
||||
};
|
||||
}
|
||||
|
||||
public override KeyValuePair<int, int> 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, int>(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, int>(int.Parse(tail),
|
||||
AreaCodeDictionary[head]);
|
||||
new AddressDef()
|
||||
{
|
||||
Area = AreaCodeDictionary[head],
|
||||
Address = int.Parse(tail)
|
||||
};
|
||||
}
|
||||
|
||||
public string GetAreaName(int code)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ namespace Modbus.Net.FBox
|
||||
ConnectionType = (FBoxType) connectionType;
|
||||
}
|
||||
|
||||
protected override async Task<byte[]> GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount)
|
||||
public override async Task<GetDataReturnDef> 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)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<package >
|
||||
<metadata>
|
||||
<id>Modbus.Net.FBox</id>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.1</version>
|
||||
<title>Modbus.Net.FBox</title>
|
||||
<authors>Chris L.(Luo Sheng)</authors>
|
||||
<owners>Hangzhou Delian Information and Science Technology Co., Ltd.</owners>
|
||||
@@ -13,7 +13,7 @@
|
||||
<copyright>Copyright 2015 Hangzhou Delian Science and Technology Co.,Ltd.</copyright>
|
||||
<tags>hardware communicate protocal fbox Delian</tags>
|
||||
<dependencies>
|
||||
<dependency id="Modbus.Net" version="1.0.0"></dependency>
|
||||
<dependency id="Modbus.Net" version="1.1.0"></dependency>
|
||||
<dependency id="Thinktecture.IdentityModel.Client" version="4.0.0"></dependency>
|
||||
<dependency id="Microsoft.AspNet.SignalR.Client" version="2.2.0"></dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -54,17 +54,23 @@ namespace Modbus.Net.Modbus
|
||||
};
|
||||
}
|
||||
|
||||
public override KeyValuePair<int, int> 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, int>(TransDictionary[head] + int.Parse(tail) - 1,
|
||||
ReadFunctionCodeDictionary[head])
|
||||
: new KeyValuePair<int, int>(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<int, int> 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, int>(int.Parse(tail) - 1,
|
||||
ReadFunctionCodeDictionary[head])
|
||||
: new KeyValuePair<int, int>(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,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
|
||||
<metadata>
|
||||
<id>Modbus.Net.Modbus</id>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
<title>Modbus.Net.Modbus</title>
|
||||
<authors>Chris L.(Luo Sheng)</authors>
|
||||
<owners>Hangzhou Delian Information and Science Technology Co.,Ltd.</owners>
|
||||
@@ -13,7 +13,7 @@
|
||||
<copyright>Copyright 2015 Hangzhou Delian Science and Technology Co.,Ltd.</copyright>
|
||||
<tags>hardware communicate protocal modbus Delian</tags>
|
||||
<dependencies>
|
||||
<dependency id="Modbus.Net" version="1.0.0" />
|
||||
<dependency id="Modbus.Net" version="1.1.0" />
|
||||
</dependencies>
|
||||
</metadata>
|
||||
<files>
|
||||
|
||||
@@ -58,9 +58,9 @@ namespace Modbus.Net.Modbus
|
||||
public ReadDataModbusInputStruct(byte belongAddress, string startAddress, ushort getCount, AddressTranslator addressTranslator)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
KeyValuePair<int, int> 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<int, int> 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;
|
||||
|
||||
@@ -65,7 +65,7 @@ namespace Modbus.Net.Modbus
|
||||
ModbusType = (ModbusType) connectionType;
|
||||
}
|
||||
|
||||
protected override async Task<byte[]> GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount)
|
||||
public override async Task<GetDataReturnDef> 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
|
||||
{
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
|
||||
<metadata>
|
||||
<id>Modbus.Net.OPC</id>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
<title>Modbus.Net.OPC</title>
|
||||
<authors>Chris L.(Luo Sheng)</authors>
|
||||
<owners>Hangzhou Delian Information and Science Technology</owners>
|
||||
@@ -13,7 +13,7 @@
|
||||
<copyright>Copyright 2015 Hangzhou Delian Science and Technology Co.,Ltd.</copyright>
|
||||
<tags>hardware communicate protocal OPC DA Delian</tags>
|
||||
<dependencies>
|
||||
<dependency id="Modbus.Net" version="1.0.0" />
|
||||
<dependency id="Modbus.Net" version="1.1.0" />
|
||||
<dependency id="Modbus.Net.h-opc" version="0.4.0" />
|
||||
</dependencies>
|
||||
</metadata>
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Modbus.Net.OPC
|
||||
{
|
||||
}
|
||||
|
||||
protected override async Task<byte[]> GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount)
|
||||
public override async Task<GetDataReturnDef> 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)
|
||||
{
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -26,9 +26,10 @@ namespace Modbus.Net.Siemens
|
||||
};
|
||||
}
|
||||
|
||||
public override KeyValuePair<int, int> 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, int>(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, int>(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, int>(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, int>(int.Parse(tail),
|
||||
AreaCodeDictionary[head]);
|
||||
new AddressDef()
|
||||
{
|
||||
Area = AreaCodeDictionary[head],
|
||||
Address = int.Parse(tail)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
|
||||
<metadata>
|
||||
<id>Modbus.Net.Siemens</id>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
<title>Modbus.Net.Siemens</title>
|
||||
<authors>Chris L.(Luo Sheng)</authors>
|
||||
<owners>Hangzhou Delian Information and Science Technology Co.,Ltd.</owners>
|
||||
@@ -13,7 +13,7 @@
|
||||
<copyright>Copyright 2015 Hangzhou Delian Science and Technology Co.,Ltd.</copyright>
|
||||
<tags>hardware communicate protocal Siemens profinet Delian</tags>
|
||||
<dependencies>
|
||||
<dependency id="Modbus.Net" version="1.0.0" />
|
||||
<dependency id="Modbus.Net" version="1.1.0" />
|
||||
</dependencies>
|
||||
</metadata>
|
||||
<files>
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<byte[]> SendReceiveAsync(params object[] content)
|
||||
public override async Task<byte[]> 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)
|
||||
|
||||
@@ -121,7 +121,7 @@ namespace Modbus.Net.Siemens
|
||||
ConnectionType = (SiemensType) connectionType;
|
||||
}
|
||||
|
||||
protected override async Task<byte[]> GetDatasAsync(byte belongAddress, byte materAddress, string startAddress, int getByteCount)
|
||||
public override async Task<GetDataReturnDef> 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)
|
||||
{
|
||||
|
||||
@@ -3,6 +3,12 @@ using System.Collections.Generic;
|
||||
|
||||
namespace Modbus.Net
|
||||
{
|
||||
public class AddressDef
|
||||
{
|
||||
public int Area { get; set; }
|
||||
public int Address { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 地址翻译器
|
||||
/// </summary>
|
||||
@@ -14,7 +20,7 @@ namespace Modbus.Net
|
||||
/// <param name="address">地址前地址</param>
|
||||
/// <param name="isRead">是否为读取,是为读取,否为写入</param>
|
||||
/// <returns>Key为转换后的地址,Value为辅助码</returns>
|
||||
public abstract KeyValuePair<int,int> AddressTranslate(string address, bool isRead);
|
||||
public abstract AddressDef AddressTranslate(string address, bool isRead);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -22,14 +28,18 @@ namespace Modbus.Net
|
||||
/// </summary>
|
||||
public class AddressTranslatorBase : AddressTranslator
|
||||
{
|
||||
public override KeyValuePair<int, int> 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<int, int>(num2, num1);
|
||||
return new AddressDef()
|
||||
{
|
||||
Area = num1,
|
||||
Address = num2
|
||||
};
|
||||
}
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@@ -135,14 +135,16 @@ namespace Modbus.Net
|
||||
foreach (var communicateAddress in CommunicateAddresses)
|
||||
{
|
||||
//获取数据
|
||||
var datas =
|
||||
var datasReturn =
|
||||
await
|
||||
BaseUtility.GetDatasAsync<byte>(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
|
||||
});
|
||||
}
|
||||
|
||||
@@ -64,19 +64,21 @@ namespace Modbus.Net
|
||||
/// <summary>
|
||||
/// 发送协议内容并接收,一般方法
|
||||
/// </summary>
|
||||
/// <param name="isLittleEndian">是否是小端格式</param>
|
||||
/// <param name="content">写入的内容,使用对象数组描述</param>
|
||||
/// <returns>从设备获取的字节流</returns>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送协议内容并接收,一般方法
|
||||
/// </summary>
|
||||
/// <param name="isLittleEndian">是否是小端格式</param>
|
||||
/// <param name="content">写入的内容,使用对象数组描述</param>
|
||||
/// <returns>从设备获取的字节流</returns>
|
||||
public virtual async Task<byte[]> SendReceiveAsync(params object[] content)
|
||||
public virtual async Task<byte[]> 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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
@@ -50,7 +56,7 @@ namespace Modbus.Net
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的byte数据</returns>
|
||||
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
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的byte数据</returns>
|
||||
protected abstract Task<byte[]> GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount);
|
||||
public abstract Task<GetDataReturnDef> GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount);
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
|
||||
<metadata>
|
||||
<id>Modbus.Net</id>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
<title>Modbus.Net</title>
|
||||
<authors>Chris L.(Luo Sheng)</authors>
|
||||
<owners>Hangzhou Delian Information and Science Technology Co.,Ltd.</owners>
|
||||
|
||||
@@ -35,5 +35,5 @@ using System.Runtime.InteropServices;
|
||||
// 方法是按如下所示使用“*”:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
|
||||
[assembly: AssemblyVersion("1.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0")]
|
||||
[assembly: AssemblyVersion("1.1.0")]
|
||||
[assembly: AssemblyFileVersion("1.1.0")]
|
||||
@@ -4,6 +4,8 @@ namespace Modbus.Net
|
||||
{
|
||||
public abstract class ProtocalUnit : IProtocalFormatting
|
||||
{
|
||||
public bool IsLittleEndian { get; protected set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 从输入结构格式化
|
||||
/// </summary>
|
||||
@@ -18,7 +20,7 @@ namespace Modbus.Net
|
||||
/// <returns>格式化后的字节流</returns>
|
||||
public virtual byte[] Format(params object[] message)
|
||||
{
|
||||
return TranslateContent(message);
|
||||
return TranslateContent(IsLittleEndian, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -32,17 +34,19 @@ namespace Modbus.Net
|
||||
/// <summary>
|
||||
/// 转换静态方法,把对象数组转换为字节数组。
|
||||
/// </summary>
|
||||
/// <param name="isLittleEndian">是否是小端格式</param>
|
||||
/// <param name="contents">对象数组</param>
|
||||
/// <returns>字节数组</returns>
|
||||
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
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -7,6 +7,12 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net
|
||||
{
|
||||
public class TaskReturnDef
|
||||
{
|
||||
public int MachineId { get; set; }
|
||||
public Dictionary<string, ReturnUnit> ReturnValues { get; set; }
|
||||
}
|
||||
|
||||
public static class TimeRestore
|
||||
{
|
||||
public static int Restore = 0;
|
||||
@@ -215,7 +221,7 @@ namespace Modbus.Net
|
||||
/// 返回数据代理
|
||||
/// </summary>
|
||||
/// <param name="returnValue"></param>
|
||||
public delegate void ReturnValuesDelegate(KeyValuePair<int, Dictionary<string,ReturnUnit>> returnValue);
|
||||
public delegate void ReturnValuesDelegate(TaskReturnDef returnValue);
|
||||
|
||||
/// <summary>
|
||||
/// 返回数据事件
|
||||
@@ -559,7 +565,11 @@ namespace Modbus.Net
|
||||
{
|
||||
MoveMachineToLinked(machine.Id);
|
||||
}
|
||||
ReturnValues?.Invoke(new KeyValuePair<int, Dictionary<string,ReturnUnit>>(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<int, Dictionary<string,ReturnUnit>>(machine.Id, null));
|
||||
ReturnValues?.Invoke(new TaskReturnDef()
|
||||
{
|
||||
MachineId = machine.Id,
|
||||
ReturnValues = null
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<AddressUnit>()
|
||||
{
|
||||
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);
|
||||
|
||||
@@ -43,14 +43,14 @@ namespace Siemens_S7_200.UI.WPF.TaskTest
|
||||
{
|
||||
//唯一的参数包含返回值,是一个唯一标识符(machine的第二个参数),返回值(类型ReturnUnit)的键值对。
|
||||
value = new List<string>();
|
||||
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");
|
||||
}
|
||||
};
|
||||
//启动任务
|
||||
|
||||
Reference in New Issue
Block a user