Guides › What Is CRC? How CRC-16 MODBUS, CCITT and CRC-32 Work
What Is CRC? How CRC-16 MODBUS, CCITT and CRC-32 Work
CRC (Cyclic Redundancy Check) is an error-detecting code that tells you whether data was corrupted in transit or storage. The sender appends a CRC value to the data; the receiver computes it again, and if the two values differ the data is treated as damaged. Modbus RTU, CAN, USB, Ethernet, ZIP and PNG all rely on CRCs.
Why CRC is needed
Industrial cabling such as RS-485 runs next to motors, drives and welders, so electrical noise flipping a bit is common. If one bit of the temperature value 0x00FA (25.0 °C) flips to give 0x80FA, the value is completely wrong. With a CRC the receiver discards the frame and asks again instead of acting on bad data.
CRC vs simple checksums
| Method | How it works | Weakness |
|---|---|---|
| Checksum (sum) | Adds all bytes | Misses reordered bytes, or one byte +1 and another −1 |
| LRC (Modbus ASCII) | Two's complement of the byte sum | Similar limits |
| CRC | Remainder of a polynomial division | Very strong against random errors, but not against deliberate tampering |
| Hash (SHA-256) | Cryptographic hash | Too heavy for most microcontroller links |
A CRC always detects: every single-bit error, every double-bit error within a bounded frame length, any odd number of bit errors (when the polynomial has a factor of (x+1)), and any burst error no longer than the CRC width. A CRC is not a security feature: anyone who changes the data can simply recompute and append a new CRC.
How it works: binary polynomial division
A CRC treats the data bits as one large binary polynomial and divides it by a fixed generator polynomial; the remainder is the CRC. Addition and subtraction are XOR without carries. The common CRC-16 polynomial x16 + x15 + x2 + 1, written without its top term, is 0x8005.
- Load the CRC register with the initial value (Init).
- XOR the next data byte into the register.
- Shift one bit at a time; whenever the bit shifted out is 1, XOR the polynomial in. Repeat 8 times.
- After the last byte, apply the final XOR (XorOut). The result is the CRC.
Reading CRC parameters
"CRC-16" alone does not define a result. Two 16-bit CRCs with different parameters give completely different values, so check these in the device manual first.
| Parameter | Meaning |
|---|---|
| Width | Number of CRC bits (8, 16, 32...) |
| Poly | Generator polynomial (top term omitted) |
| Init | Register value before processing |
| RefIn | Whether each input byte is bit-reflected (LSB first) |
| RefOut | Whether the final result is bit-reflected |
| XorOut | Value XORed into the final result |
| Check | Result for the ASCII string "123456789"; use it to verify an implementation |
Common CRC variants
| Name | Poly | Init | RefIn/Out | XorOut | Check | Used in |
|---|---|---|---|---|---|---|
| CRC-16/MODBUS | 0x8005 | 0xFFFF | True/True | 0x0000 | 0x4B37 | Modbus RTU |
| CRC-16/CCITT-FALSE (IBM-3740) | 0x1021 | 0xFFFF | False/False | 0x0000 | 0x29B1 | Serial protocols, firmware images |
| CRC-16/XMODEM | 0x1021 | 0x0000 | False/False | 0x0000 | 0x31C3 | XMODEM file transfer |
| CRC-16/KERMIT | 0x1021 | 0x0000 | True/True | 0x0000 | 0x2189 | KERMIT, some Bluetooth |
| CRC-32 (ISO-HDLC) | 0x04C11DB7 | 0xFFFFFFFF | True/True | 0xFFFFFFFF | 0xCBF43926 | Ethernet, ZIP, PNG, gzip |
"CCITT" is used loosely for several variants with different Init values. If a manual only says CCITT, use the check values above to find out which one it means. This site's calculator supports MODBUS, CCITT-FALSE and CRC-32.
CRC-16/MODBUS example
The request 01 03 00 00 00 0A (read 10 holding registers from slave 1) has CRC 0xCDC5. Modbus RTU sends the CRC low byte first, so the frame on the wire is:
01 03 00 00 00 0A C5 CD
Enter 01 03 00 00 00 0A in HEX mode in the calculator to confirm. Appending the two CRC bytes in the wrong order is the most common reason a hand-built frame gets no reply.
C code (bitwise)
uint16_t crc16_modbus(const uint8_t *buf, size_t len) {
uint16_t crc = 0xFFFF;
for (size_t i = 0; i < len; i++) {
crc ^= buf[i];
for (int b = 0; b < 8; b++)
crc = (crc & 1) ? (crc >> 1) ^ 0xA001 : crc >> 1;
}
return crc; /* send crc & 0xFF first, then crc >> 8 */
}
0xA001 is 0x8005 bit-reversed. For reflected CRCs (RefIn/RefOut true), shifting right with the reversed polynomial avoids reflecting every byte.
CRC-16/CCITT-FALSE
uint16_t crc16_ccitt_false(const uint8_t *buf, size_t len) {
uint16_t crc = 0xFFFF;
for (size_t i = 0; i < len; i++) {
crc ^= (uint16_t)buf[i] << 8;
for (int b = 0; b < 8; b++)
crc = (crc & 0x8000) ? (crc << 1) ^ 0x1021 : crc << 1;
}
return crc;
}
Faster: table-driven CRC
The bitwise version loops 8 times per byte. When throughput matters, precompute a 256-entry table (512 bytes for a 16-bit CRC) and do one lookup per byte. Use the bitwise version on very small MCUs and the table version when memory allows.
ASCII input vs HEX input
Entering 01 03 as ASCII computes the CRC over the characters 0, 1, space, 0, 3 (bytes 30 31 20 30 33). In HEX mode it uses the bytes 01 03. Use HEX mode to verify communication frames.
Common mistakes
- Appending the CRC bytes in the wrong order (Modbus RTU is low byte first)
- Leaving the address byte out of the calculation, or including an already-appended CRC
- Confusing Init values 0xFFFF and 0x0000
- Adding a CRC to Modbus TCP frames (TCP frames have no CRC)
The full Modbus frame layout is covered in the Modbus guide.
Try it now: CRC Calculator (runs in your browser, no data is sent anywhere)