CRC check in Modbus protocol is a verification method used to verify the integrity of Modbus data packets, which can effectively prevent errors and data tampering during data transmission. The CRC check used in the Modbus protocol adopts polynomial division, and the specific calculation method is as follows:
- Firstly, assemble all bytes in the Modbus data packet into a byte array in order.
- Then, all bytes of the byte array are sequentially fed into the CRC calculation function to calculate the CRC check value.
- Finally, the calculated CRC check value is appended to the end of the Modbus data packet to verify the integrity of the data.
The specific steps for calculating the CRC check value are as follows:
- Set the initial value of a 16 bit register to 0xFFFF.
- Perform the following operations on each byte in the byte array:
- XOR the value of this byte with 0xFF.
- XOR the above result with the lower 8 bits of the 16 bit register to obtain a new value for the 16 bit register.
- Perform 8 cycles on the value of the new 16 bit register, with each cycle:
- If the highest bit of the register is 1, shift the register to the left by one bit and perform XOR operation with 0xA001;
- If the highest bit of the register is 0, move the register one bit to the left.
- The final value of the 16 bit register is the CRC check value.
It should be noted that when calculating the CRC check value, the order of bytes needs to follow the order specified by the Modbus protocol, generally arranged from low to high. In addition, the additional method of CRC check values also needs to follow the format specified by the Modbus protocol.
Download and organize some CRC online verification URLs:
http://www.ip33.com/crc.html
https://www.23bei.com/tool/232.html
难道不是右移吗