我正在使用modbus协议连接到设备。我需要从机器获得3个值。所述第一值是数据格式的INT16和当我发送一个例子字节数组:使用Modbus协议计算int32数据格式的CRC
static byte[] hz = new byte[] { (byte) 0x01, (byte) 0x03, (byte) 0x00,
(byte) 0x33, (byte) 0x00, (byte) 0x01 };
并使用我从a previous question I asked on the subject获得的CRC计算方法。
// Compute the MODBUS RTU CRC
private static int ModRTU_CRC(byte[] buf, int len)
{
int crc = 0xFFFF;
for (int pos = 0; pos < len; pos++) {
crc ^= (int)buf[pos]; // XOR byte into least sig. byte of crc
for (int i = 8; i != 0; i--) { // Loop over each bit
if ((crc & 0x0001) != 0) { // If the LSB is set
crc >>= 1; // Shift right and XOR 0xA001
crc ^= 0xA001;
}
else // Else LSB is not set
crc >>= 1; // Just shift right
}
}
// Note, this number has low and high bytes swapped, so use it accordingly (or swap bytes)
return crc;
}
我可以收到回复。但是,其他两个值是int32数据格式,并且在使用此方法时不返回答复。为了帮助排除故障,我正在使用名为Realterm.的程序来启动命令。我用它将一个Modbus 16 CRC附加到字节流的末尾并发送,这对所有三个都有效,并返回所需的回复。这是数据格式不适用于此特定计算公式的情况吗? CRC16和Modbus16有什么区别?