| ??? 11/18/01 22:01 Read: times |
#16686 - RE: Checksum calc \\\'LRC\\\' ? |
Bert
Here is how a LRC is generated (excerpt from the Modicon Modbus Protocol) Basically you sum all the bytes, subtract FF from the total, and then add 1 to it so in the previous example 00 40 02 90 00 00+40+02+90+00 = D2 FF - D2 = 2D 2D + 1 = 2E LRC = 2E (well that is my understanding of it from reading the book anyway) The Excerpt: LRC Generation The Longitudinal Redundancy Check (LRC) field is one byte, containing an 8–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 8–bit bytes in the message, discarding any carries, and then two’s complementing the result. The LRC is an 8–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. A procedure for generating an LRC is: 1. Add all bytes in the message, excluding the starting ‘colon’ and ending CRLF. Add them into an 8–bit field, so that carries will be discarded. 2. Subtract the final field value from FF hex (all 1’s), to produce the ones–complement. 3. Add 1 to produce the twos–complement. The C Source Code: static unsigned char LRC(auchMsg, usDataLen) unsigned char *auchMsg ; /* message to calculate LRC upon */ unsigned short usDataLen ; /* quantity of bytes in message */ { 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 */ } |
| Topic | Author | Date |
| Checksum calc 'LRC' ? | 01/01/70 00:00 | |
| RE: Checksum calc 'LRC' ? | 01/01/70 00:00 | |
| RE: Checksum calc 'LRC' ? | 01/01/70 00:00 | |
| RE: Checksum calc \\\'LRC\\\' ? | 01/01/70 00:00 | |
RE: Checksum calc \\\'LRC\\\' ? | 01/01/70 00:00 |



