获取字节数组的CRC校验和并将其添加到该字节数组

Gre*_*dot 9 java checksum modbus crc

我有这个字节数组:

static byte[] buf = new byte[] { (byte) 0x01, (byte) 0x04, (byte)0x00, (byte)0x01,(byte)0x00, (byte) 0x01};
Run Code Online (Sandbox Code Playgroud)

现在,该字节数组的CRC校验和应该是0x60,0x0A.我希望Java代码重新创建此校验和,但我似乎无法重新创建它.我试过crc16:

static int crc16(final byte[] buffer) {
    int crc = 0xFFFF;

    for (int j = 0; j < buffer.length ; j++) {
        crc = ((crc  >>> 8) | (crc  << 8) )& 0xffff;
        crc ^= (buffer[j] & 0xff);//byte to int, trunc sign
        crc ^= ((crc & 0xff) >> 4);
        crc ^= (crc << 12) & 0xffff;
        crc ^= ((crc & 0xFF) << 5) & 0xffff;
    }
    crc &= 0xffff;
    return crc;

}
Run Code Online (Sandbox Code Playgroud)

并使用Integer.toHexString()转换它们,但没有一个结果与正确的CRC匹配.有人可以指出我在CRC公式方面的正确方向.

For*_*cke 11

请改用以下代码:

// 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] & 0xFF;   // 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;  
}
Run Code Online (Sandbox Code Playgroud)

但是,您可能必须反转返回CRC以获得正确的字节顺序.我甚至在这里测试过:

http://ideone.com/PrBXVh

使用Windows计算器或其他东西,您可以看到第一个结果(来自上面的函数调用)给出了预期值(尽管是相反的).