Operation Manual

Appendix C LRC and CRC calculation
LRC generation function
static unsigned char LRC(unsigned char *auchMsg, unsigned short usDataLen)
{
unsigned char uchLRC = 0 ; /* LRC char initialized */
while (usDataLen––) /* pass through message buffer */
uchLRC += *auchMsg++ ; /* add buffer byte without carry */
return ((unsigned char)(–((char)uchLRC))) ; /* return twos complement */
}
CRC generation
The Cyclical Redundancy Checking (CRC) field is two bytes, containing a
16-bit binary value. The CRC value is first generated by the transmitting
device, which appends the CRC to the message. The device that receives
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.
There are many ways of calculating a CRC checksum. To ensure correct
calculation, please refer to [Reference 1] Modbus over serial line, where
detailed descriptions and programming examples are available. Even
more information and programming examples in different programming
languages can be found on: www.modbus.org searching for CRC.
Below is a short text description of how the CRC is calculated. This
description is then followed by a C programming example.
1. Load a 16-bit register with FFFF hex (all 1’s). Call this the CRC
register.
2. Exclusive OR the first 8-bit byte of the message with the low-order
byte of the 16-bit CRC register, putting the result in the CRC
register.
3. Shift the CRC register one bit to the right (toward the LSB), zero-
filling the MSB. Extract and examine the LSB.
4. (If the LSB was 0): Repeat step 3 (another shift). (If the LSB was
1): Exclusive OR the CRC register with the polynomial value
0xA001 (1010 0000 0000 0001).
5. Repeat steps 3 and 4 until 8 shifts have been performed. When
this is done, a complete 8-bit byte will have been processed.
6. Repeat steps 2 through 5 for the next 8-bit byte of the message.
Continue doing this until all bytes have been processed.
7. The final content of the CRC register is the CRC value.
34 S 421