Skip to content
You Start with one site
Amounts in Start with one site
IoT & Networks · Concept

Checksums and CRC

One idea Intermediate 5 min read
Found this useful?

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.

Send everything twiceIt doubles the traffic, and one disturbance can damage both copies.Costly
Ask if the value is reasonableThat catches 342.7 V. It cannot catch 240.7 V.Not enough alone
Send a check value with itTwo extra bytes, worked out from all the others.This is a CRC

See it

Predict first

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?

See it: a message and its check value

Check value that arrived
Check value the receiver calculates
The receiver

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

The question

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
  1. Find the check value that was sent

    5 + 3 + 0 + 0 + 0 + 9 = 17.

  2. 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.

  3. 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

A check value detects a change. It does not repair it, and it does not prove that the value is true. The receiver can only discard a damaged message and wait for it again. And a message that passes proves only that the bytes arrived as they were sent. If the reader asks for the wrong register, the wrong number arrives with a perfect CRC.

Check yourself

Question 1

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?

Question 2

A receiver finds that the CRC of a frame does not match. What can it do with the frame?

Question 3

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.
Key points
  • 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.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.