KROHNE ALTOSONIC V Modbus EN User Manual
Page 58
ALTOSONIC V
ModBus Manual 0300 rev07 E
7.30855.35.00
Page 58 of 64
9.2
Appendix B: LRC Generation
(As taken from the website:
www.modicon.com/techpubs/crc7.html
)
The Longitudinal Redundancy Check (LRC) field is one byte, containing an eight-bit binary value. The
LRC value is calculated by the transmitting device, which appends the LRC to the message. The
receiving device recalculates an LRC during receipt of the message, and compares the calculated
value to the actual value it received in the LRC field. If the two values are not equal, an error results.
The LRC is calculated by adding together successive eight-bit bytes in the message, discarding any
carries, then two's complementing the result. The LRC is an eight-bit field, therefore each new addition
of a character that would result in a value higher than 255 decimal simply rolls over the field's value
through zero. Because there is no ninth bit, the carry is discarded automatically.
Generating an LRC
Step 1 :
Add all bytes in the message, excluding the starting colon and ending CRLF. Add them into an eight-bit
field, so that carries will be discarded.
Step 2
Subtract the final field value from FF hex (all 1's), to produce the ones-complement.
Step 3
Add 1 to produce the two's-complement.
Placing the LRC into the Message
When the the eight-bit LRC (two ASCII characters) is transmitted in the message, the high order
character will be transmitted first, followed by the low order character-e.g., if the LRC value is 61 hex
(0110 0001):
Figure 8 LRC Character Sequence
Example
An example of a C language function performing LRC generation is shown below.
The function takes two arguments:
unsigned char *auchMsg ;
A pointer to the message buffer containing binary data to be used for generating the LRC
unsigned short usDataLen ; The quantity of bytes in the message buffer. The function returns the LRC as a type unsigned
char.
LRC Generation Function
static unsigned char LRC(auchMsg, usDataLen)
unsigned char *auchMsg ;
/* message to calculate */
unsigned short usDataLen ;
/* LRC upon quantity of */
/* bytes in message */
{
unsigned char uchLRC = 0 ;
/* LRC char initialized */
while (usDataLen--)
/* pass through message */
uchLRC += *auchMsg++ ;
/* buffer add buffer byte*/
/* without carry */
return ((unsigned char)(-((char_uchLRC))) ;
/* return twos complemen */
}