|
|
@ -1,5 +1,6 @@
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections;
|
|
|
|
using HslCommunication;
|
|
|
|
using HslCommunication;
|
|
|
|
|
|
|
|
using HslCommunication.Core;
|
|
|
|
using HslCommunication.ModBus;
|
|
|
|
using HslCommunication.ModBus;
|
|
|
|
using HybirdFrameworkCore.Utils;
|
|
|
|
using HybirdFrameworkCore.Utils;
|
|
|
|
using log4net;
|
|
|
|
using log4net;
|
|
|
@ -14,6 +15,8 @@ public class ModbusTcpMaster
|
|
|
|
public string Ip { get; set; } = "192.168.1.5";
|
|
|
|
public string Ip { get; set; } = "192.168.1.5";
|
|
|
|
public int Port { get; set; } = 502;
|
|
|
|
public int Port { get; set; } = 502;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DataFormat DataFormat { get; set; } = DataFormat.ABCD;
|
|
|
|
|
|
|
|
|
|
|
|
public int Duration { get; set; } = 1000;
|
|
|
|
public int Duration { get; set; } = 1000;
|
|
|
|
public bool Connected { get; set; } = false;
|
|
|
|
public bool Connected { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
|
@ -40,6 +43,7 @@ public class ModbusTcpMaster
|
|
|
|
if (ModbusTcpNet == null)
|
|
|
|
if (ModbusTcpNet == null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ModbusTcpNet = new ModbusTcpNet(Ip, Port);
|
|
|
|
ModbusTcpNet = new ModbusTcpNet(Ip, Port);
|
|
|
|
|
|
|
|
ModbusTcpNet.DataFormat = DataFormat;
|
|
|
|
OperateResult result = ModbusTcpNet.ConnectServer();
|
|
|
|
OperateResult result = ModbusTcpNet.ConnectServer();
|
|
|
|
if (result.IsSuccess)
|
|
|
|
if (result.IsSuccess)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -83,14 +87,11 @@ public class ModbusTcpMaster
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (ReadAction != null)
|
|
|
|
if (ReadAction != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// ReadAction(this);
|
|
|
|
|
|
|
|
while (!StopFlag)
|
|
|
|
while (!StopFlag)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
ReadAction(this);
|
|
|
|
ReadAction(this);
|
|
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(Duration);
|
|
|
|
Thread.Sleep(Duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
catch (Exception e)
|
|
|
@ -99,6 +100,7 @@ public class ModbusTcpMaster
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GetLog().Info("stop listen");
|
|
|
|
GetLog().Info("stop listen");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -140,25 +142,27 @@ public class ModbusTcpMaster
|
|
|
|
ModbusDataType dataType = property.Type;
|
|
|
|
ModbusDataType dataType = property.Type;
|
|
|
|
int start = property.Start;
|
|
|
|
int start = property.Start;
|
|
|
|
int length = property.Length;
|
|
|
|
int length = property.Length;
|
|
|
|
|
|
|
|
int registerNo = property.RegisterNo - 40000;
|
|
|
|
byte[] setValue = BitUtls.Value2Bytes(value, property.Scale, property.Offset);
|
|
|
|
byte[] setValue = BitUtls.Value2Bytes(value, property.Scale, property.Offset);
|
|
|
|
OperateResult operateResult;
|
|
|
|
OperateResult operateResult;
|
|
|
|
|
|
|
|
|
|
|
|
switch (dataType)
|
|
|
|
switch (dataType)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
case ModbusDataType.Byte:
|
|
|
|
case ModbusDataType.Byte:
|
|
|
|
|
|
|
|
|
|
|
|
start = start % 2 == 0 ? start / 2 : start / 2 + 1;
|
|
|
|
start = start % 2 == 0 ? start / 2 : start / 2 + 1;
|
|
|
|
operateResult = ModbusTcpNet.Write("x=6;" + (property.RegisterNo - 40000 + start), setValue);
|
|
|
|
operateResult = ModbusTcpNet.Write("x=6;" + (registerNo + start), setValue);
|
|
|
|
result = operateResult.IsSuccess;
|
|
|
|
result = operateResult.IsSuccess;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case ModbusDataType.Register:
|
|
|
|
case ModbusDataType.Register:
|
|
|
|
operateResult = ModbusTcpNet.Write("x=16;" + (property.RegisterNo - 40000 + start), setValue);
|
|
|
|
operateResult = ModbusTcpNet.Write("x=16;" + (registerNo + start), setValue);
|
|
|
|
result = operateResult.IsSuccess;
|
|
|
|
result = operateResult.IsSuccess;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case ModbusDataType.Bit:
|
|
|
|
case ModbusDataType.Bit:
|
|
|
|
|
|
|
|
|
|
|
|
start = start % 16 == 0 ? start / 16 : start / 16 + 1;
|
|
|
|
start = start % 16 == 0 ? start / 16 : start / 16 + 1;
|
|
|
|
length = length % 8 == 0 ? length / 8 : length / 8 + 1;
|
|
|
|
length = length % 8 == 0 ? length / 8 : length / 8 + 1;
|
|
|
|
OperateResult<byte[]> readResult = ModbusTcpNet.Read("x=3;" + (property.RegisterNo - 40000 + start), (ushort)length);
|
|
|
|
OperateResult<byte[]> readResult =
|
|
|
|
|
|
|
|
ModbusTcpNet.Read("x=3;" + (registerNo + start), (ushort)length);
|
|
|
|
|
|
|
|
|
|
|
|
if (readResult.IsSuccess)
|
|
|
|
if (readResult.IsSuccess)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -173,7 +177,7 @@ public class ModbusTcpMaster
|
|
|
|
|
|
|
|
|
|
|
|
bitArray.CopyTo(bytes, 0);
|
|
|
|
bitArray.CopyTo(bytes, 0);
|
|
|
|
|
|
|
|
|
|
|
|
operateResult = ModbusTcpNet.Write("x=6;" + (property.RegisterNo - 40000 + start), bytes);
|
|
|
|
operateResult = ModbusTcpNet.Write("x=6;" + (registerNo + start), bytes);
|
|
|
|
result = operateResult.IsSuccess;
|
|
|
|
result = operateResult.IsSuccess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|