我正在使用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有什麼區別?