从网上的资料中看到:
In RTU mode, messages include an error–checking field that is based on a
Cyclical Redundancy Check (CRC) method. The CRC field checks the contents
of the entire message. It is applied regardless of any parity check method used for the individual characters of the message.
The CRC field is two bytes, containing a 16–bit binary value. The CRC value is calculated by the transmitting device, which appends the CRC to the message. The receiving device recalculates a CRC during receipt of the message, and compares the calculated value to the actual value it received in the CRC field. If the two values are not equal, an error results. The CRC is started by first preloading a 16–bit register to all 1’s. Then a process begins of applying successive 8–bit bytes of the message to the current contents of the register. Only the eight bits of data in each character are used for generating the CRC. Start and stop bits, and the parity bit, do not apply to the CRC. During generation of the CRC, each 8–bit character is exclusive ORed with the register contents. Then the result is shifted in the direction of the least  ignificant bit (LSB), with a zero filled into the most significant bit (MSB) position. The LSB is extracted and examined. If the LSB was a 1, the register is then exclusive ORed with a preset, fixed value. If the LSB was a 0, no exclusive OR takes place. This process is repeated until eight shifts have been performed. After the last (eighth) shift, the next 8–bit byte is exclusive ORed with the register’s current value, and the process repeats for eight more shifts as described above. The final contents of the register, after all the bytes of the message have been  pplied, is the CRC value.
这段话的中文资料:
CRC-16码由两个字节构成,在开始时CRC寄存器的每一位都预置为1,然后把CRC寄存器与8-bit的数据进行异或,之后对CRC寄存器从高到低进行移位,在最高位(MSB)的位置补零,而最低位(LSB,移位后已经被移出CRC寄存器)如果为1,则把寄存器与预定义的多项式码进行异或,否则如果LSB为零,则无需进行异或。重复上述的由高至低的移位8次,第一个8-bit数据处理完毕,用此时CRC寄存器的值与下一个8-bit数据异或并进行如前一个数据似的8次移位。所有的字符处理完成后CRC寄存器内的值即为最终的CRC值。请问:文中的a preset, fixed value(预定义的多项式码)指的是什么啊?我在网上下了个计算CRC校验码的源码程序,它计算16位的CRC,过程中的预定义多项式取的是$1021.就不知道这个值是怎么来的。

解决方案 »

  1.   

    用它的那个$1021?它是用个EDIT直接初始化在里面的,有什么根据啊。
      

  2.   

    go to http://www.playicq.com download example
      

  3.   

    那个预定义多项式是发送方和接收方事先的一个约定,并且他的最高项和最低项的系数必须为一,设M位数据块的多项式为M(X),生成多项式G(X)的阶数必须比M(X)的阶数低,按这个自己定义就可以。