簡體   English   中英

獲取字節數組的CRC校驗和並將其添加到該字節數組

[英]Getting the CRC checksum of a byte array and adding it to that byte array

我有這個字節數組:

static byte[] buf = new byte[] { (byte) 0x01, (byte) 0x04, (byte)0x00, (byte)0x01,(byte)0x00, (byte) 0x01};

現在,該字節數組的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;

}

並使用Integer.toHexString()轉換它們,但沒有一個結果與正確的CRC匹配。 有人可以指出我在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] & 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;  
}

但是,您可能必須反轉返回CRC以獲得正確的字節順序。 我甚至在這里測試過:

http://ideone.com/PrBXVh

使用Windows計算器或其他東西,您可以看到第一個結果(來自上面的函數調用)給出了預期值(盡管是相反的)。

CRC32會這么做,還是必須是CRC16? 如果32沒問題,您是否嘗試在java.util.zip使用CRC32

import java.util.zip.CRC32;

byte[] buf = new byte[] { (byte) 0x01, (byte) 0x04, (byte)0x00, (byte)0x01,(byte)0x00, (byte) 0x01};
CRC32 crc32 = new CRC32();
crc32.update(buf);
System.out.printf("%X\n", crc32.getValue());

輸出是:

F9DB8E67

然后你可以做任何你想要的額外計算。

我正在使用Java 1.6進行modbus,嘗試上面的代碼,它只是部分工作? 在一些CRC上達成一致意見,其他人則錯誤。 我研究了一下,看到我的符號擴展有問題。 我掩蓋了高位(見下面的FIX),現在效果很好。 注意:所有CRC計算都不一樣,MODBUS有點不同:

    public static int getCRC(byte[] buf, int len ) {
    int crc =  0xFFFF;
    int val = 0;

      for (int pos = 0; pos < len; pos++) {
        crc ^= (int)(0x00ff & buf[pos]);  // FIX HERE -- 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, crc has low and high bytes swapped, so use it accordingly (or swap bytes)
    val =  (crc & 0xff) << 8;
    val =  val + ((crc >> 8) & 0xff);
    System.out.printf("Calculated a CRC of 0x%x, swapped: 0x%x\n", crc, val);
    return val;  

}   // end GetCRC

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM