Checksums and CRC
On this page
A cashier counts the notes in a bundle and writes the total on the band. The next person counts again, and if the two totals differ, something happened to the bundle on the way. A checksum is that total for a message: a short number worked out from the data and sent with it. A CRC (cyclic redundancy check) is a stronger kind that notices almost every change.
See it
A gateway sends a request of 6 bytes to a meter, followed by 2 bytes of CRC. On the cable, one of the 48 data bits changes. What does the meter do?
The upper row is what the sender puts on the cable: six bytes of a request to a meter, then the check value. Each byte is written as two hexadecimal digits. The lower row is what arrives. The receiver calculates the check value again from the six bytes it received and compares.
The idea
A long cable in a plant sometimes changes a bit. If the bit is inside a reading, the value is wrong and nothing in the value shows it. So the sender calculates a check value from the bytes of the message and adds it at the end. The receiver calculates it again from the bytes that arrived. If the two differ, the receiver discards the message.
The simplest check value is a checksum: add all the bytes and send the total. It notices one changed bit. It is blind to changes that cancel, such as two bytes that change places, because the total stays the same.
A CRC treats the whole message as one long number, divides it by a fixed number and sends the remainder. Each bit affects the remainder according to its position, so changes do not cancel. The 16-bit CRC of Modbus notices every message with one or two wrong bits.
A 16-bit value has only 65,536 possible values, so of heavier damage about 1 case in 65,536 passes by chance. The lesson on Modbus RTU frames and the CRC calculates one step by step.
One worked number
A message has six bytes with the values 5, 3, 0, 0, 0 and 9. Its check value is the plain sum of the bytes. In one case the last byte changes from 9 to 11 on the cable (one bit). In another case the last two bytes change places. What does the receiver decide each time?
Show the worked solution, 3 steps
- Find the check value that was sent
5 + 3 + 0 + 0 + 0 + 9 = 17.
- One bit changed
The receiver adds 5 + 3 + 0 + 0 + 0 + 11 = 19. That is not 17, so it discards the message. This is the right decision.
- Two bytes changed places
The receiver adds 5 + 3 + 0 + 0 + 9 + 0 = 17. The sums agree, so it accepts a wrong message. Check it in the figure: set the slider to none, then press both buttons.
The mistake to avoid
Check yourself
A check value of 8 bits has 256 possible values. A CRC of 16 bits has 65,536. A message is so badly damaged that its check value is as good as random. About how often does such a message pass each check by chance?
A receiver finds that the CRC of a frame does not match. What can it do with the frame?
Every frame from a meter passes its CRC. The gateway shows 8.40 kW, and the display on the meter shows 24.15 kW. Where is the fault?
Where you meet it
- Meters and drives on a plant busEvery Modbus message ends with a 16-bit CRC.
- New firmware sent to a remote deviceThe device checks the whole file and refuses a damaged one.
- A data logger with a memory cardEach block is stored with a check value, so a block that went bad is not read as data.
- A check value is worked out from the data and sent with it.
- A plain sum misses changes that cancel. A CRC depends on the position of every bit.
- It detects and does not correct.
- A good CRC proves that the bytes arrived unchanged, not that the value is right.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.