Compare commits
10 Commits
e38c16e899
...
e7cad88bcf
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e7cad88bcf | ||
|
|
7f304e826f | ||
|
|
51ded333d9 | ||
|
|
62e6fcbfb1 | ||
|
|
0f757d6996 | ||
|
|
80d6e843af | ||
|
|
40b2b39d2b | ||
|
|
c3c4125a4b | ||
|
|
b4c57a42d0 | ||
|
|
21c8e34934 |
@@ -28,9 +28,9 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否为大端
|
/// 是否为大端
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected new bool LittleEndian => false;
|
public override bool LittleEndian => true;
|
||||||
|
|
||||||
protected new bool LittleEndianBit => false;
|
public override bool LittleEndianBit => false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 覆盖的获取实例的方法
|
/// 覆盖的获取实例的方法
|
||||||
@@ -154,9 +154,9 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否为大端
|
/// 是否为大端
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected new bool LittleEndian => true;
|
public override bool LittleEndian => true;
|
||||||
|
|
||||||
protected new bool LittleEndianBit => false;
|
public override bool LittleEndianBit => true;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 覆盖的获取实例的方法
|
/// 覆盖的获取实例的方法
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0" />
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace Modbus.Net.HJ212
|
namespace Modbus.Net.HJ212
|
||||||
{
|
{
|
||||||
public class HJ212Controller : FifoController
|
public class HJ212Controller : NoResponseController
|
||||||
{
|
{
|
||||||
public HJ212Controller(string ip, int port) : base(int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "FetchSleepTime")))
|
public HJ212Controller(string ip, int port) : base(int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "FetchSleepTime")))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ namespace Modbus.Net.HJ212
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">设备的ID号</param>
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
public HJ212Machine(TKey id, string connectionString, string st, string cn, string pw, string mn)
|
public HJ212Machine(TKey id, string alias, string connectionString, string st, string cn, string pw, string mn)
|
||||||
: base(id, null, true)
|
: base(id, alias, null, true)
|
||||||
{
|
{
|
||||||
BaseUtility = new HJ212Utility(connectionString);
|
BaseUtility = new HJ212Utility(connectionString);
|
||||||
ST = st;
|
ST = st;
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
namespace Modbus.Net.HJ212
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Modbus.Net.HJ212
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// HJ212协议连接器
|
/// HJ212协议连接器
|
||||||
@@ -9,6 +15,45 @@
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override async Task<byte[]> SendReceiveAsync(byte[] content)
|
||||||
|
{
|
||||||
|
if (content.Length <= 1000)
|
||||||
|
return await base.SendReceiveAsync(content);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string contentString = Encoding.ASCII.GetString(content);
|
||||||
|
string[] formats = { "yyyyMMddHHmmssffff", "yyyyMMddHHmmssfff", "yyyyMMddHHmmss" };
|
||||||
|
DateTime startTime = DateTime.ParseExact(contentString.Substring(3, 18), formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
|
||||||
|
int dataStartIdx = contentString.IndexOf("&&") + 2;
|
||||||
|
string head = contentString.Substring(0, dataStartIdx).Substring(21);
|
||||||
|
string[] contentUnitAll = contentString.Substring(dataStartIdx).Split(';')[1].Split(',');
|
||||||
|
List<string> contentSplitAll = new List<string>();
|
||||||
|
string newContent = "DataTime=" + startTime.ToString("yyyyMMddHHmmss") + ";";
|
||||||
|
foreach (var contentUnit in contentUnitAll)
|
||||||
|
{
|
||||||
|
if (newContent.Length + contentUnit.Length > 1000 - dataStartIdx)
|
||||||
|
{
|
||||||
|
contentSplitAll.Add("QN=" + startTime.ToString("yyyyMMddHHmmssffff") + head + newContent[..^1]);
|
||||||
|
startTime = startTime.AddSeconds(1);
|
||||||
|
newContent = "DataTime=" + startTime.ToString("yyyyMMddHHmmss") + ";" + contentUnit + ",";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
newContent += contentUnit + ",";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
contentSplitAll.Add("QN=" + startTime.ToString("yyyyMMddHHmmssffff") + head + newContent[..^1]);
|
||||||
|
var receiveBytesAll = new List<byte>();
|
||||||
|
foreach (var contentSplit in contentSplitAll)
|
||||||
|
{
|
||||||
|
var extBytes = BytesExtend(Encoding.ASCII.GetBytes(contentSplit));
|
||||||
|
var receiveBytes = await SendReceiveWithoutExtAndDecAsync(extBytes);
|
||||||
|
receiveBytesAll.AddRange(receiveBytes == null ? null : receiveBytes.Length == 0 ? receiveBytes : BytesDecact(receiveBytes));
|
||||||
|
}
|
||||||
|
return receiveBytesAll.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 检查接收的数据是否正确
|
/// 检查接收的数据是否正确
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -33,14 +33,14 @@ namespace Modbus.Net.HJ212
|
|||||||
{
|
{
|
||||||
var writeRequestHJ212InputStruct =
|
var writeRequestHJ212InputStruct =
|
||||||
new WriteRequestHJ212InputStruct((string)setContents[0], (string)setContents[1], (string)setContents[2], (string)setContents[3], (List<Dictionary<string, string>>)setContents[4], (DateTime)setContents[5]);
|
new WriteRequestHJ212InputStruct((string)setContents[0], (string)setContents[1], (string)setContents[2], (string)setContents[3], (List<Dictionary<string, string>>)setContents[4], (DateTime)setContents[5]);
|
||||||
var writeRequestOpcOutputStruct =
|
var writeRequestHJ212OutputStruct =
|
||||||
await
|
await
|
||||||
Wrapper.SendReceiveAsync<WriteRequestHJ212OutputStruct>(Wrapper[typeof(WriteRequestHJ212Protocol)],
|
Wrapper.SendReceiveAsync<WriteRequestHJ212OutputStruct>(Wrapper[typeof(WriteRequestHJ212Protocol)],
|
||||||
writeRequestHJ212InputStruct);
|
writeRequestHJ212InputStruct);
|
||||||
return new ReturnStruct<bool>
|
return new ReturnStruct<bool>
|
||||||
{
|
{
|
||||||
Datas = writeRequestOpcOutputStruct?.GetValue != null,
|
Datas = writeRequestHJ212OutputStruct?.GetValue != null,
|
||||||
IsSuccess = writeRequestOpcOutputStruct?.GetValue != null,
|
IsSuccess = writeRequestHJ212OutputStruct?.GetValue != null,
|
||||||
ErrorCode = 0,
|
ErrorCode = 0,
|
||||||
ErrorMsg = null,
|
ErrorMsg = null,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -20,10 +20,10 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
/// <param name="endian">端格式</param>
|
||||||
public ModbusMachine(TKey id, ModbusType connectionType, string connectionString,
|
public ModbusMachine(TKey id, string alias, ModbusType connectionType, string connectionString,
|
||||||
IEnumerable<AddressUnit<TUnitKey, int, int>> getAddresses, bool keepConnect, byte slaveAddress, byte masterAddress,
|
IEnumerable<AddressUnit<TUnitKey, int, int>> getAddresses, bool keepConnect, byte slaveAddress, byte masterAddress,
|
||||||
Endian endian)
|
Endian endian)
|
||||||
: base(id, getAddresses, keepConnect, slaveAddress, masterAddress)
|
: base(id, alias, getAddresses, keepConnect, slaveAddress, masterAddress)
|
||||||
{
|
{
|
||||||
BaseUtility = new ModbusUtility(connectionType, connectionString, slaveAddress, masterAddress, endian);
|
BaseUtility = new ModbusUtility(connectionType, connectionString, slaveAddress, masterAddress, endian);
|
||||||
AddressFormater = new AddressFormaterModbus();
|
AddressFormater = new AddressFormaterModbus();
|
||||||
@@ -41,10 +41,10 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
/// <param name="endian">端格式</param>
|
||||||
public ModbusMachine(TKey id, ModbusType connectionType, string connectionString,
|
public ModbusMachine(TKey id, string alias, ModbusType connectionType, string connectionString,
|
||||||
IEnumerable<AddressUnit<TUnitKey, int, int>> getAddresses, byte slaveAddress, byte masterAddress,
|
IEnumerable<AddressUnit<TUnitKey, int, int>> getAddresses, byte slaveAddress, byte masterAddress,
|
||||||
Endian endian)
|
Endian endian)
|
||||||
: this(id, connectionType, connectionString, getAddresses, true, slaveAddress, masterAddress, endian)
|
: this(id, alias, connectionType, connectionString, getAddresses, true, slaveAddress, masterAddress, endian)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Reflection.Metadata.Ecma335;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AddressUnit = Modbus.Net.AddressUnit<string, int, int>;
|
using AddressUnit = Modbus.Net.AddressUnit<string, int, int>;
|
||||||
using DataReturnDef = Modbus.Net.DataReturnDef<string, double>;
|
using DataReturnDef = Modbus.Net.DataReturnDef<string, double>;
|
||||||
@@ -11,8 +12,7 @@ namespace Modbus.Net.Modbus
|
|||||||
{
|
{
|
||||||
public class ModbusRtuDataReceiver
|
public class ModbusRtuDataReceiver
|
||||||
{
|
{
|
||||||
private ModbusRtuProtocolReceiver _receiver;
|
private Dictionary<ModbusRtuProtocolReceiver, DateTime> _receivers;
|
||||||
|
|
||||||
private readonly IConfigurationRoot configuration = new ConfigurationBuilder()
|
private readonly IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||||
.SetBasePath(Directory.GetCurrentDirectory())
|
.SetBasePath(Directory.GetCurrentDirectory())
|
||||||
.AddJsonFile("appsettings.json")
|
.AddJsonFile("appsettings.json")
|
||||||
@@ -57,12 +57,16 @@ namespace Modbus.Net.Modbus
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ModbusRtuDataReceiver(MachineDataType dataType)
|
public ModbusRtuDataReceiver(MachineDataType dataType, int minimumElapse = 0)
|
||||||
{
|
{
|
||||||
_receiver = new ModbusRtuProtocolReceiver(ConfigurationReader.GetValue("Receiver", "d:connectionString"), int.Parse(ConfigurationReader.GetValue("Receiver", "g:slaveAddress")));
|
_receivers = new Dictionary<ModbusRtuProtocolReceiver,DateTime>();
|
||||||
var machineName = ConfigurationReader.GetValue("Receiver", "a:id");
|
var receiversDef = configuration.GetSection("Modbus.Net").GetSection("Receiver").GetChildren();
|
||||||
var addressMapName = ConfigurationReader.GetValue("Receiver", "e:addressMap");
|
foreach (var receiverDef in receiversDef)
|
||||||
var endian = ValueHelper.GetInstance(Endian.Parse(ConfigurationReader.GetValue("Receiver", "i:endian")));
|
{
|
||||||
|
var machineName = receiverDef.GetValue<string>("a:id");
|
||||||
|
var _receiver = new ModbusRtuProtocolReceiver(receiverDef.GetValue<string>("e:connectionString"), receiverDef.GetValue<int>("h:slaveAddress"));
|
||||||
|
var addressMapName = receiverDef.GetValue<string>("f:addressMap");
|
||||||
|
var endian = ValueHelper.GetInstance(Endian.Parse(receiverDef.GetValue<string>("j:endian")));
|
||||||
_receiver.DataProcess = receiveContent =>
|
_receiver.DataProcess = receiveContent =>
|
||||||
{
|
{
|
||||||
var returnTime = DateTime.Now;
|
var returnTime = DateTime.Now;
|
||||||
@@ -137,12 +141,16 @@ namespace Modbus.Net.Modbus
|
|||||||
value = Math.Round(value, addressMap[i].DecimalPos);
|
value = Math.Round(value, addressMap[i].DecimalPos);
|
||||||
AddValueToValueDic(valueDic, returnDic, addressMap[i], value, dataType);
|
AddValueToValueDic(valueDic, returnDic, addressMap[i], value, dataType);
|
||||||
}
|
}
|
||||||
|
if (machineName == "EventData" || (returnTime - _receivers[_receiver]).TotalSeconds + 0.5 >= minimumElapse)
|
||||||
|
{
|
||||||
if (ReturnValueDictionary != null)
|
if (ReturnValueDictionary != null)
|
||||||
{
|
{
|
||||||
var dataReturn = new DataReturnDef();
|
var dataReturn = new DataReturnDef();
|
||||||
dataReturn.MachineId = machineName;
|
dataReturn.MachineId = machineName;
|
||||||
dataReturn.ReturnValues = new ReturnStruct<Dictionary<string, ReturnUnit<double>>>() { IsSuccess = true, Datas = returnDic };
|
dataReturn.ReturnValues = new ReturnStruct<Dictionary<string, ReturnUnit<double>>>() { IsSuccess = true, Datas = returnDic };
|
||||||
ReturnValueDictionary(dataReturn);
|
ReturnValueDictionary(dataReturn);
|
||||||
|
_receivers[_receiver] = returnTime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -190,11 +198,17 @@ namespace Modbus.Net.Modbus
|
|||||||
if (returnBytes != null) return returnBytes;
|
if (returnBytes != null) return returnBytes;
|
||||||
else return null;
|
else return null;
|
||||||
};
|
};
|
||||||
|
_receivers.Add(_receiver, DateTime.MinValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> ConnectAsync()
|
public async Task<bool> ConnectAsync()
|
||||||
{
|
{
|
||||||
return await _receiver.ConnectAsync();
|
var result = await Task.FromResult(Parallel.ForEach(_receivers, async _receiver =>
|
||||||
|
{
|
||||||
|
await _receiver.Key.ConnectAsync();
|
||||||
|
}));
|
||||||
|
return result.IsCompleted;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ namespace Modbus.Net.Siemens
|
|||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="src">本机模块位,0到7,仅200使用,其它型号不要填写</param>
|
/// <param name="src">本机模块位,0到7,仅200使用,其它型号不要填写</param>
|
||||||
/// <param name="dst">PLC模块位,0到7,仅200使用,其它型号不要填写</param>
|
/// <param name="dst">PLC模块位,0到7,仅200使用,其它型号不要填写</param>
|
||||||
public SiemensMachine(TKey id, SiemensType connectionType, string connectionString, SiemensMachineModel model,
|
public SiemensMachine(TKey id, string alias, SiemensType connectionType, string connectionString, SiemensMachineModel model,
|
||||||
IEnumerable<AddressUnit<TUnitKey, int, int>> getAddresses, bool keepConnect, byte slaveAddress, byte masterAddress, byte src = 1, byte dst = 0)
|
IEnumerable<AddressUnit<TUnitKey, int, int>> getAddresses, bool keepConnect, byte slaveAddress, byte masterAddress, byte src = 1, byte dst = 0)
|
||||||
: base(id, getAddresses, keepConnect, slaveAddress, masterAddress)
|
: base(id, alias, getAddresses, keepConnect, slaveAddress, masterAddress)
|
||||||
{
|
{
|
||||||
BaseUtility = new SiemensUtility(connectionType, connectionString, model, slaveAddress, masterAddress, src, dst);
|
BaseUtility = new SiemensUtility(connectionType, connectionString, model, slaveAddress, masterAddress, src, dst);
|
||||||
AddressFormater = new AddressFormaterSiemens();
|
AddressFormater = new AddressFormaterSiemens();
|
||||||
@@ -44,9 +44,9 @@ namespace Modbus.Net.Siemens
|
|||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="src">本机模块位,0到7,仅200使用,其它型号不要填写</param>
|
/// <param name="src">本机模块位,0到7,仅200使用,其它型号不要填写</param>
|
||||||
/// <param name="dst">PLC模块位,0到7,仅200使用,其它型号不要填写</param>
|
/// <param name="dst">PLC模块位,0到7,仅200使用,其它型号不要填写</param>
|
||||||
public SiemensMachine(TKey id, SiemensType connectionType, string connectionString, SiemensMachineModel model,
|
public SiemensMachine(TKey id, string alias, SiemensType connectionType, string connectionString, SiemensMachineModel model,
|
||||||
IEnumerable<AddressUnit<TUnitKey, int, int>> getAddresses, byte slaveAddress, byte masterAddress, byte src = 1, byte dst = 0)
|
IEnumerable<AddressUnit<TUnitKey, int, int>> getAddresses, byte slaveAddress, byte masterAddress, byte src = 1, byte dst = 0)
|
||||||
: this(id, connectionType, connectionString, model, getAddresses, true, slaveAddress, masterAddress, src, dst)
|
: this(id, alias, connectionType, connectionString, model, getAddresses, true, slaveAddress, masterAddress, src, dst)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,16 @@ namespace Modbus.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class ValueHelper
|
public abstract class ValueHelper
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 是否为小端格式
|
||||||
|
/// </summary>
|
||||||
|
public abstract bool LittleEndian { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 位是否为小端格式
|
||||||
|
/// </summary>
|
||||||
|
public abstract bool LittleEndianBit { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 兼容数据类型对应的字节长度
|
/// 兼容数据类型对应的字节长度
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -347,12 +357,12 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 协议中的内容构造是否小端的,默认是小端构造协议。
|
/// 协议中的内容构造是否小端的,默认是小端构造协议。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static bool LittleEndian => true;
|
public override bool LittleEndian => true;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 协议中的比特位内容构造是否小端的,默认是小端构造协议。
|
/// 协议中的比特位内容构造是否小端的,默认是小端构造协议。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static bool LittleEndianBit => true;
|
public override bool LittleEndianBit => true;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将一个byte数字转换为一个byte元素的数组。
|
/// 将一个byte数字转换为一个byte元素的数组。
|
||||||
@@ -1228,7 +1238,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否为大端
|
/// 是否为大端
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected new bool LittleEndian => false;
|
public override bool LittleEndian => false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 覆盖的获取实例的方法
|
/// 覆盖的获取实例的方法
|
||||||
@@ -1471,12 +1481,12 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否为小端
|
/// 是否为小端
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected new bool LittleEndian => false;
|
public override bool LittleEndian => false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否为小端位
|
/// 是否为小端位
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected new bool LittleEndianBit => false;
|
public override bool LittleEndianBit => false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 覆盖的实例获取方法
|
/// 覆盖的实例获取方法
|
||||||
|
|||||||
@@ -398,4 +398,59 @@ namespace Modbus.Net
|
|||||||
addressUnits);
|
addressUnits);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ReadAddressesDesc
|
||||||
|
{
|
||||||
|
public string Area { get; set; }
|
||||||
|
|
||||||
|
public int Address { get; set; }
|
||||||
|
|
||||||
|
public int GetCount { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 固定读取地址的范围,无视所有其它规则
|
||||||
|
/// </summary>
|
||||||
|
public class AddressCombinerStatic<TKey> : AddressCombiner<TKey, int, int> where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
protected IEnumerable<ReadAddressesDesc> ReadAddresses { get; set; }
|
||||||
|
|
||||||
|
protected AddressTranslator AddressTranslator { get; set; }
|
||||||
|
|
||||||
|
public AddressCombinerStatic(IEnumerable<ReadAddressesDesc> readAddresses, AddressTranslator addressTranslator)
|
||||||
|
{
|
||||||
|
AddressTranslator = addressTranslator;
|
||||||
|
ReadAddresses = readAddresses;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 组合地址
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="addresses">需要组合的地址</param>
|
||||||
|
/// <returns>组合后的地址</returns>
|
||||||
|
public override IEnumerable<CommunicationUnit<TKey, int, int>> Combine(IEnumerable<AddressUnit<TKey, int, int>> addresses)
|
||||||
|
{
|
||||||
|
List<CommunicationUnit<TKey, int, int>> communicationUnits = new List<CommunicationUnit<TKey, int, int>>();
|
||||||
|
foreach (var readAddress in ReadAddresses)
|
||||||
|
{
|
||||||
|
var originalAddresses = new List<AddressUnit<TKey, int, int>>();
|
||||||
|
foreach (var address in addresses)
|
||||||
|
{
|
||||||
|
if (address.Area == readAddress.Area && address.Address >= readAddress.Address && address.Address < readAddress.Address + readAddress.GetCount)
|
||||||
|
originalAddresses.Add(address);
|
||||||
|
}
|
||||||
|
communicationUnits.Add(new CommunicationUnit<TKey, int, int>
|
||||||
|
{
|
||||||
|
Area = readAddress.Area,
|
||||||
|
Address = readAddress.Address,
|
||||||
|
GetCount = readAddress.GetCount * (int)
|
||||||
|
Math.Ceiling(AddressTranslator.GetAreaByteLength(readAddress.Area)),
|
||||||
|
DataType = typeof(byte),
|
||||||
|
GetOriginalCount = originalAddresses.Count,
|
||||||
|
OriginalAddresses = originalAddresses.ToList()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return communicationUnits;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -25,8 +25,8 @@ namespace Modbus.Net
|
|||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
/// <param name="slaveAddress">从站地址</param>
|
/// <param name="slaveAddress">从站地址</param>
|
||||||
/// <param name="masterAddress">主站地址</param>
|
/// <param name="masterAddress">主站地址</param>
|
||||||
protected BaseMachine(TKey id, IEnumerable<AddressUnit<TUnitKey, int, int>> getAddresses, bool keepConnect, byte slaveAddress,
|
protected BaseMachine(TKey id, string alias, IEnumerable<AddressUnit<TUnitKey, int, int>> getAddresses, bool keepConnect, byte slaveAddress,
|
||||||
byte masterAddress) : base(id, getAddresses, keepConnect)
|
byte masterAddress) : base(id, alias, getAddresses, keepConnect)
|
||||||
{
|
{
|
||||||
SlaveAddress = slaveAddress;
|
SlaveAddress = slaveAddress;
|
||||||
MasterAddress = masterAddress;
|
MasterAddress = masterAddress;
|
||||||
@@ -149,6 +149,10 @@ namespace Modbus.Net
|
|||||||
var localMainPos = (int)localPos;
|
var localMainPos = (int)localPos;
|
||||||
//字节坐标的子地址位置
|
//字节坐标的子地址位置
|
||||||
var localSubPos = (int)((localPos - localMainPos) * 8);
|
var localSubPos = (int)((localPos - localMainPos) * 8);
|
||||||
|
if (ValueHelper.GetInstance(BaseUtility.Endian).LittleEndianBit == true && AddressTranslator.GetAreaByteLength(communicateAddress.Area) > 1 && ValueHelper.ByteLength[address.DataType.FullName] < AddressTranslator.GetAreaByteLength(communicateAddress.Area))
|
||||||
|
{
|
||||||
|
localMainPos =(int)(2 * ((int)(localMainPos / AddressTranslator.GetAreaByteLength(communicateAddress.Area)) * AddressTranslator.GetAreaByteLength(communicateAddress.Area)) + (AddressTranslator.GetAreaByteLength(communicateAddress.Area) - 1) - localMainPos);
|
||||||
|
}
|
||||||
|
|
||||||
//根据类型选择返回结果的键是通讯标识还是地址
|
//根据类型选择返回结果的键是通讯标识还是地址
|
||||||
string key;
|
string key;
|
||||||
@@ -510,8 +514,8 @@ namespace Modbus.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">设备的ID号</param>
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
||||||
protected BaseMachine(TKey id, IEnumerable<AddressUnit<TUnitKey, TAddressKey, TSubAddressKey>> getAddresses)
|
protected BaseMachine(TKey id, string alias, IEnumerable<AddressUnit<TUnitKey, TAddressKey, TSubAddressKey>> getAddresses)
|
||||||
: this(id, getAddresses, false)
|
: this(id, alias, getAddresses, false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -521,11 +525,22 @@ namespace Modbus.Net
|
|||||||
/// <param name="id">设备的ID号</param>
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
protected BaseMachine(TKey id, IEnumerable<AddressUnit<TUnitKey, TAddressKey, TSubAddressKey>> getAddresses, bool keepConnect)
|
protected BaseMachine(TKey id, string alias, IEnumerable<AddressUnit<TUnitKey, TAddressKey, TSubAddressKey>> getAddresses, bool keepConnect)
|
||||||
{
|
{
|
||||||
Id = id;
|
Id = id;
|
||||||
GetAddresses = getAddresses;
|
GetAddresses = getAddresses;
|
||||||
KeepConnect = keepConnect;
|
KeepConnect = keepConnect;
|
||||||
|
if (alias.Contains(':'))
|
||||||
|
{
|
||||||
|
var aliasArray = alias.Split(':');
|
||||||
|
ProjectName = aliasArray[0];
|
||||||
|
MachineName = aliasArray[1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ProjectName = "";
|
||||||
|
MachineName = alias;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly int _maxErrorCount = 3;
|
private readonly int _maxErrorCount = 3;
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
namespace Modbus.Net.Machine
|
|
||||||
{
|
|
||||||
public class BaseServer
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -56,7 +56,7 @@ namespace AnyType.Controllers
|
|||||||
Value = 0,
|
Value = 0,
|
||||||
Type = unitValue.DataType.Name
|
Type = unitValue.DataType.Name
|
||||||
};
|
};
|
||||||
var machine = new ModbusMachine("1", ModbusType.Tcp, "10.10.18.251:502", addressUnits, true, 2, 0, Endian.BigEndianLsb);
|
var machine = new ModbusMachine("1", "", ModbusType.Tcp, "10.10.18.251:502", addressUnits, true, 2, 0, Endian.BigEndianLsb);
|
||||||
//启动任务
|
//启动任务
|
||||||
await MachineJobSchedulerCreator.CreateScheduler("Trigger1", -1, 1).Result.From(machine.Id, machine, MachineDataType.CommunicationTag).Result.Query("Query1",
|
await MachineJobSchedulerCreator.CreateScheduler("Trigger1", -1, 1).Result.From(machine.Id, machine, MachineDataType.CommunicationTag).Result.Query("Query1",
|
||||||
returnValues =>
|
returnValues =>
|
||||||
|
|||||||
@@ -8,8 +8,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0" />
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ namespace TripleAdd.Controllers
|
|||||||
{
|
{
|
||||||
if (machine == null)
|
if (machine == null)
|
||||||
{
|
{
|
||||||
machine = new ModbusMachine<string, string>("1", ModbusType.Tcp, "10.10.18.251", new List<AddressUnit>()
|
machine = new ModbusMachine<string, string>("1", "", ModbusType.Tcp, "10.10.18.251", new List<AddressUnit>()
|
||||||
{
|
{
|
||||||
new AddressUnit() {Id = "1", Area = "4X", Address = 1, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0},
|
new AddressUnit() {Id = "1", Area = "4X", Address = 1, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0},
|
||||||
new AddressUnit() {Id = "2", Area = "4X", Address = 2, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0},
|
new AddressUnit() {Id = "2", Area = "4X", Address = 2, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0},
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ namespace Modbus.Net.Tests
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
_baseMachine = new SiemensMachine<int, int>(2, SiemensType.Tcp, _machineIp, SiemensMachineModel.S7_1200, _addressUnits, true, 2, 0, 1, 0)
|
_baseMachine = new SiemensMachine<int, int>(2, "", SiemensType.Tcp, _machineIp, SiemensMachineModel.S7_1200, _addressUnits, true, 2, 0, 1, 0)
|
||||||
{
|
{
|
||||||
ProjectName = "Project 1",
|
ProjectName = "Project 1",
|
||||||
MachineName = "Test 2"
|
MachineName = "Test 2"
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ namespace Modbus.Net.Tests
|
|||||||
[TestInitialize]
|
[TestInitialize]
|
||||||
public void Init()
|
public void Init()
|
||||||
{
|
{
|
||||||
_modbusTcpMachine = new ModbusMachine<string, string>("1", ModbusType.Tcp, _machineIp, null, true, 1, 0, Endian.BigEndianLsb);
|
_modbusTcpMachine = new ModbusMachine<string, string>("1", "",ModbusType.Tcp, _machineIp, null, true, 1, 0, Endian.BigEndianLsb);
|
||||||
|
|
||||||
_modbusTcpMachine2 = new ModbusMachine<string, string>("2", ModbusType.Tcp, _machineIp, null, true, 1, 0, Endian.LittleEndianLsb);
|
_modbusTcpMachine2 = new ModbusMachine<string, string>("2", "", ModbusType.Tcp, _machineIp, null, true, 1, 0, Endian.LittleEndianLsb);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace Modbus.Net.Tests
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void GetUtility()
|
public void GetUtility()
|
||||||
{
|
{
|
||||||
BaseMachine<int, int> baseMachine = new ModbusMachine<int, int>(1, ModbusType.Tcp, _machineIp, null, true, 2, 0, Endian.BigEndianLsb);
|
BaseMachine<int, int> baseMachine = new ModbusMachine<int, int>(1, "", ModbusType.Tcp, _machineIp, null, true, 2, 0, Endian.BigEndianLsb);
|
||||||
var utility = baseMachine.GetUtilityMethods<IUtilityMethodDatas>();
|
var utility = baseMachine.GetUtilityMethods<IUtilityMethodDatas>();
|
||||||
var methods = utility.GetType().GetRuntimeMethods();
|
var methods = utility.GetType().GetRuntimeMethods();
|
||||||
Assert.AreEqual(methods.FirstOrDefault(method => method.Name == "GetDatasAsync") != null, true);
|
Assert.AreEqual(methods.FirstOrDefault(method => method.Name == "GetDatasAsync") != null, true);
|
||||||
@@ -24,7 +24,7 @@ namespace Modbus.Net.Tests
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public async Task InvokeUtility()
|
public async Task InvokeUtility()
|
||||||
{
|
{
|
||||||
BaseMachine<int, int> baseMachine = new ModbusMachine<int, int>(1, ModbusType.Tcp, _machineIp, null, true, 2, 0, Endian.BigEndianLsb);
|
BaseMachine<int, int> baseMachine = new ModbusMachine<int, int>(1, "", ModbusType.Tcp, _machineIp, null, true, 2, 0, Endian.BigEndianLsb);
|
||||||
await baseMachine.BaseUtility.ConnectAsync();
|
await baseMachine.BaseUtility.ConnectAsync();
|
||||||
var success = await baseMachine.BaseUtility.GetUtilityMethods<IUtilityMethodDatas>().SetDatasAsync("4X 1", new object[] { (byte)11 }, 1);
|
var success = await baseMachine.BaseUtility.GetUtilityMethods<IUtilityMethodDatas>().SetDatasAsync("4X 1", new object[] { (byte)11 }, 1);
|
||||||
Assert.AreEqual(success.IsSuccess, true);
|
Assert.AreEqual(success.IsSuccess, true);
|
||||||
@@ -36,7 +36,7 @@ namespace Modbus.Net.Tests
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public async Task InvokeMachine()
|
public async Task InvokeMachine()
|
||||||
{
|
{
|
||||||
BaseMachine<int, int> baseMachine = new ModbusMachine<int, int>(1, ModbusType.Tcp, _machineIp, new List<AddressUnit>
|
BaseMachine<int, int> baseMachine = new ModbusMachine<int, int>(1, "",ModbusType.Tcp, _machineIp, new List<AddressUnit>
|
||||||
{
|
{
|
||||||
new AddressUnit
|
new AddressUnit
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ namespace Modbus.Net.Tests
|
|||||||
[TestInitialize]
|
[TestInitialize]
|
||||||
public void Init()
|
public void Init()
|
||||||
{
|
{
|
||||||
_modbusRtuMachine1 = new ModbusMachine<string, string>("1", ModbusType.Rtu, _machineCom, null, true, 1, 0, Endian.BigEndianLsb);
|
_modbusRtuMachine1 = new ModbusMachine<string, string>("1", "", ModbusType.Rtu, _machineCom, null, true, 1, 0, Endian.BigEndianLsb);
|
||||||
_modbusRtuMachine2 = new ModbusMachine<string, string>("2", ModbusType.Rtu, _machineCom, null, true, 2, 0, Endian.BigEndianLsb);
|
_modbusRtuMachine2 = new ModbusMachine<string, string>("2", "", ModbusType.Rtu, _machineCom, null, true, 2, 0, Endian.BigEndianLsb);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
|||||||
@@ -22,11 +22,11 @@ namespace Modbus.Net.Tests
|
|||||||
[TestInitialize]
|
[TestInitialize]
|
||||||
public void Init()
|
public void Init()
|
||||||
{
|
{
|
||||||
_modbusTcpMachine = new ModbusMachine<string, string>("1", ModbusType.Tcp, _machineIp, null, true, 2, 0, Endian.BigEndianLsb);
|
_modbusTcpMachine = new ModbusMachine<string, string>("1", "", ModbusType.Tcp, _machineIp, null, true, 2, 0, Endian.BigEndianLsb);
|
||||||
|
|
||||||
_modbusRtuMachine = new ModbusMachine<string, string>("2", ModbusType.Rtu, _machineCom, null, true, 2, 0, Endian.BigEndianLsb);
|
_modbusRtuMachine = new ModbusMachine<string, string>("2", "", ModbusType.Rtu, _machineCom, null, true, 2, 0, Endian.BigEndianLsb);
|
||||||
|
|
||||||
_modbusAsciiMachine = new ModbusMachine<string, string>("3", ModbusType.Ascii, _machineCom2, null, true, 2, 0, Endian.BigEndianLsb);
|
_modbusAsciiMachine = new ModbusMachine<string, string>("3", "", ModbusType.Ascii, _machineCom2, null, true, 2, 0, Endian.BigEndianLsb);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ namespace Modbus.Net.Tests
|
|||||||
[TestInitialize]
|
[TestInitialize]
|
||||||
public void Init()
|
public void Init()
|
||||||
{
|
{
|
||||||
_siemensTcpMachine = new SiemensMachine<string, string>("1", SiemensType.Tcp, _machineIp, SiemensMachineModel.S7_1200, null, true, 2, 0);
|
_siemensTcpMachine = new SiemensMachine<string, string>("1", "", SiemensType.Tcp, _machineIp, SiemensMachineModel.S7_1200, null, true, 2, 0);
|
||||||
|
|
||||||
_siemensPpiMachine = new SiemensMachine<string, string>("2", SiemensType.Ppi, _machineCom, SiemensMachineModel.S7_200, null, true, 2, 0, 1, 0);
|
_siemensPpiMachine = new SiemensMachine<string, string>("2", "", SiemensType.Ppi, _machineCom, SiemensMachineModel.S7_200, null, true, 2, 0, 1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"TCP": {
|
"TCP": {
|
||||||
"ConnectionTimeout": "5000",
|
"ConnectionTimeout": "5000",
|
||||||
"FetchSleepTime": "100",
|
"FetchSleepTime": "100",
|
||||||
"FullDuplex": "False",
|
"FullDuplex": "True",
|
||||||
"Modbus": {
|
"Modbus": {
|
||||||
"ModbusPort": "502",
|
"ModbusPort": "502",
|
||||||
"IP": "192.168.1.1"
|
"IP": "192.168.1.1"
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
"UDP": {
|
"UDP": {
|
||||||
"ConnectionTimeout": "5000",
|
"ConnectionTimeout": "5000",
|
||||||
"FetchSleepTime": "100",
|
"FetchSleepTime": "100",
|
||||||
"FullDuplex": "False",
|
"FullDuplex": "True",
|
||||||
"Modbus": {
|
"Modbus": {
|
||||||
"ModbusPort": "502",
|
"ModbusPort": "502",
|
||||||
"IP": "192.168.1.1"
|
"IP": "192.168.1.1"
|
||||||
@@ -46,7 +46,8 @@
|
|||||||
"Host": "opc.tcp://localhost/test"
|
"Host": "opc.tcp://localhost/test"
|
||||||
},
|
},
|
||||||
"Controller": {
|
"Controller": {
|
||||||
"WaitingListCount": "100"
|
"WaitingListCount": "100",
|
||||||
|
"NoResponse": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user