2013-07-18 39 views
0

我正在使用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有什么区别?

回答

1

Modbus16 a CRC16。 CRC计算有几个参数:

  • 比特宽度,在这种情况下16
  • 多项式,在这种情况下0xA001异
  • 的初始值,在此情况下为0xFFFF
  • 比特顺序
  • 最终的CRC是否与异或相反。

定义了很多CRC16s,这些参数的值不同,这看起来就是其中之一。有关更多信息,请参阅Wikipedia article on Cyclic Redundancy Checks

-2

类Obliczenia {

short POLYNOM = (short) 0x0A001; 
short[] TAB = {2,3,8,0x13,0x88,1,0x90,0,0x3c,2,0}; 
short crc = (short) 0xffff; 
short CRC_LByte,CRC_HByte; 
    public Obliczenia() { 
    for (short dana : TAB) { 
     crc= CRC16(crc, dana); 
    } 
    System.out.println("KOD CRC="+Integer.toHexString(crc&0xffff)); 
    CRC_LByte=(short)(crc & 0x00ff); 
    CRC_HByte=(short)((crc & 0xff00)/256); 
    System.out.println(" W ramce CRC_LByte="+Integer.toHexString(CRC_LByte)+ " CRC_HByte "+Integer.toHexString(CRC_HByte)); 

} 
short CRC16(short crct, short data) { 
    crct = (short) (((crct^data) | 0xff00) & (crct | 0x00ff)); 
     for (int i = 0; i < 8; i++) { 
     boolean LSB = ((short) (crct & 1)) == 1; 
     crct=(short) ((crct >>> 1)&0x7fff); 
     if (LSB) { 
      crct = (short) (crct^POLYNOM); 
     } 
    } 
    return crct; 
} 

}