|
|
@@ -56,7 +56,7 @@ namespace ModbusDemo.Device |
|
|
|
byte[] sendByte = sendByteList.ToArray(); |
|
|
|
int maxRetries = 3; // 最大重试次数 |
|
|
|
int attempt = 0; |
|
|
|
|
|
|
|
|
|
|
|
while (attempt < maxRetries) |
|
|
|
{ |
|
|
|
try |
|
|
@@ -83,7 +83,7 @@ namespace ModbusDemo.Device |
|
|
|
} |
|
|
|
//将操作失败的指令,插入数据库 |
|
|
|
string RequestStr = ByteArrayToString(sendByte); |
|
|
|
|
|
|
|
|
|
|
|
ModbusLog m = new ModbusLog(); |
|
|
|
m.OperationType = "读线圈"; |
|
|
|
m.ResponseData = ""; |
|
|
@@ -101,53 +101,53 @@ namespace ModbusDemo.Device |
|
|
|
string responseStr = ByteArrayToString(response); |
|
|
|
ModbusLog m = new ModbusLog(); |
|
|
|
m.OperationType = "读线圈"; |
|
|
|
m.ResponseData = RequestStr; |
|
|
|
m.RequestData = responseStr; |
|
|
|
m.ResponseData = responseStr; |
|
|
|
m.RequestData = RequestStr; |
|
|
|
_modbusDbContext.Add(m); |
|
|
|
_modbusDbContext.SaveChanges(); |
|
|
|
|
|
|
|
|
|
|
|
//判断发送回来的数据是否正确 |
|
|
|
if(CheckData.CheckResponse(response)) |
|
|
|
{ |
|
|
|
if (!CRCUitl.ValidateCRC(response)) |
|
|
|
{ |
|
|
|
MessageBox.Show("0x14:CRC校验错误"); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
List<byte> responseList = new List<byte>(response); |
|
|
|
//移除前两位 |
|
|
|
responseList.RemoveRange(0, 3); |
|
|
|
//移除后两位校验码 |
|
|
|
responseList.RemoveRange(responseList.Count - 2, 2); |
|
|
|
responseList.Reverse(); |
|
|
|
//数组反转之后,转为二进制字符 |
|
|
|
List<string> StringList = responseList.Select(m => Convert.ToString(m, 2)).ToList(); |
|
|
|
|
|
|
|
var result = ""; |
|
|
|
|
|
|
|
foreach (var item in StringList) |
|
|
|
{ |
|
|
|
result += item.PadLeft(8, '0'); |
|
|
|
} |
|
|
|
//先将字符串转为数组,再反转,再转为字节数组 |
|
|
|
char[] chars = result.ToArray().Reverse<char>().ToArray(); |
|
|
|
bool[] ultimately = new bool[numberOfPoints]; |
|
|
|
for (int i = 0; i < numberOfPoints; i++) |
|
|
|
if (CheckData.CheckResponse(response)) |
|
|
|
{ |
|
|
|
if (chars[i] == '1') |
|
|
|
List<byte> responseList = new List<byte>(response); |
|
|
|
//移除前两位 |
|
|
|
responseList.RemoveRange(0, 3); |
|
|
|
//移除后两位校验码 |
|
|
|
responseList.RemoveRange(responseList.Count - 2, 2); |
|
|
|
responseList.Reverse(); |
|
|
|
//数组反转之后,转为二进制字符 |
|
|
|
List<string> StringList = responseList.Select(m => Convert.ToString(m, 2)).ToList(); |
|
|
|
|
|
|
|
var result = ""; |
|
|
|
|
|
|
|
foreach (var item in StringList) |
|
|
|
{ |
|
|
|
ultimately[i] = true; |
|
|
|
result += item.PadLeft(8, '0'); |
|
|
|
} |
|
|
|
else |
|
|
|
//先将字符串转为数组,再反转,再转为字节数组 |
|
|
|
char[] chars = result.ToArray().Reverse<char>().ToArray(); |
|
|
|
bool[] ultimately = new bool[numberOfPoints]; |
|
|
|
for (int i = 0; i < numberOfPoints; i++) |
|
|
|
{ |
|
|
|
ultimately[i] = false; |
|
|
|
if (chars[i] == '1') |
|
|
|
{ |
|
|
|
ultimately[i] = true; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
ultimately[i] = false; |
|
|
|
} |
|
|
|
} |
|
|
|
return ultimately; |
|
|
|
} |
|
|
|
return ultimately; |
|
|
|
else |
|
|
|
{ |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
/// <summary> |
|
|
|
/// 写入多个线圈操作 |
|
|
@@ -155,12 +155,12 @@ namespace ModbusDemo.Device |
|
|
|
/// <param name="slaveAddress">从站地址</param> |
|
|
|
/// <param name="startAddress">起始地址</param> |
|
|
|
/// <param name="data">写入的数据</param> |
|
|
|
public bool WriteCoil(byte slaveAddress, ushort startAddress, bool[] data) |
|
|
|
public void WriteCoil(byte slaveAddress, ushort startAddress, bool[] data) |
|
|
|
{ |
|
|
|
if (!_serialPort.IsOpen) |
|
|
|
{ |
|
|
|
MessageBox.Show("串口没有链接,请先链接"); |
|
|
|
return false; |
|
|
|
return ; |
|
|
|
} |
|
|
|
List<byte> sendByteList = new List<byte>(); |
|
|
|
//设置从站地址 |
|
|
@@ -244,10 +244,10 @@ namespace ModbusDemo.Device |
|
|
|
if (!CRCUitl.ValidateCRC(response)) |
|
|
|
{ |
|
|
|
MessageBox.Show("0x14:CRC校验错误"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public ushort[] ReadRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints) |
|
|
|