Deterministic CRC-16 (IBM/ANSI) & LRC Checksum Algorithms for Industrial Fieldbuses
On noisy industrial RS-485 serial buses routed near high-voltage variable frequency drives (VFDs), servo amplifiers, and arc welders, electromagnetic interference (EMI) can easily flip individual bits in transit. Because UART parity bits only detect odd numbers of bit errors within a single byte, serial fieldbus protocols rely on frame-level error-detecting codes: CRC-16/MODBUS for binary RTU frames and Longitudinal Redundancy Check (LRC) for hexadecimal ASCII frames.
1. Mathematical Specification of CRC-16/MODBUS
The Modbus RTU standard specifies a 16-bit Cyclic Redundancy Check based on the generator polynomial $G(x) = x^{16} + x^{15} + x^2 + 1$ (normal hexadecimal representation 0x8005). Because UART hardware transmits bytes Least Significant Bit (LSB) first, the algorithm uses a bit-reflected (right-shifting) implementation with the reversed polynomial 0xA001:
| CRC Parameter | Standard Specification | Implementation Detail |
|---|---|---|
| Algorithm Name | CRC-16/MODBUS |
Reflected CRC-16-IBM / CRC-16-ANSI with non-zero seed. |
| Normal / Reflected Poly | 0x8005 / 0xA001 |
Bit-reversal of 1000 0000 0000 0101 yields 1010 0000 0000 0001 (0xA001). |
| Initial Seed Value | 0xFFFF |
All 16 register bits initialized to 1 so leading zero bytes in the frame affect the remainder. |
| Input / Output Reflection | RefIn = True, RefOut = True |
Bytes are XORed into the low byte of the 16-bit accumulator, and shifted right 8 times. |
| Final XOR Value | 0x0000 |
No inversion is applied after processing the final payload byte. |
| Wire Byte Order | Little-Endian (Low Byte First) | Unlike the rest of a Modbus frame (which is Big-Endian), the CRC-16 is appended [CRC_Lo, CRC_Hi]. |
2. Step-by-Step Bitwise CRC-16 Algorithm
The canonical bitwise computation processes every byte from the Slave ID through the last data byte of the PDU:
function computeModbusCRC16(bytes) {
let crc = 0xFFFF;
for (let i = 0; i < bytes.length; i++) {
crc ^= (bytes[i] & 0xFF);
for (let bit = 0; bit < 8; bit++) {
if ((crc & 0x0001) !== 0) {
crc = (crc >>> 1) ^ 0xA001;
} else {
crc = (crc >>> 1);
}
}
}
// Wire transmission order: Low byte first, High byte second
const crcLow = crc & 0xFF;
const crcHigh = (crc >>> 8) & 0xFF;
return { crc16: crc, wireBytes: [crcLow, crcHigh] };
}
3. The Zero-Remainder Property for Receiver Verification
A powerful mathematical property of reflected Little-Endian CRC-16 is the Zero-Remainder Theorem: if a receiver runs the exact same computeModbusCRC16() function over the entire received frame including the 2 appended CRC bytes, the final accumulator value is guaranteed to be exactly 0x0000 if and only if no transmission errors occurred. This eliminates the need to slice off the last two bytes and compare them separately.
4. Worked Example: Verifying Frame 01 03 00 00 00 0A
- Initialize
CRC = 0xFFFF. - After byte
0x01(Slave ID):CRC = 0x807E. - After byte
0x03(Function Code):CRC = 0x40FE. - After byte
0x00(Addr Hi):CRC = 0xA001. - After byte
0x00(Addr Lo):CRC = 0x51C0. - After byte
0x00(Count Hi):CRC = 0xF051. - After byte
0x0A(Count Lo = 10 registers):CRC = 0xCDC5. - Split into Little-Endian wire order: Low Byte =
C5, High Byte =CD. Complete transmitted frame:01 03 00 00 00 0A C5 CD.
5. Modbus ASCII LRC (Longitudinal Redundancy Check)
In Modbus ASCII mode (where each byte is transmitted as two ASCII hex characters framed by a leading colon : and trailing CR LF), error checking uses an 8-bit Two's Complement LRC instead of CRC-16:
function computeModbusLRC(bytes) {
let sum = 0;
for (let i = 0; i < bytes.length; i++) {
sum = (sum + (bytes[i] & 0xFF)) & 0xFF;
}
// Two's complement negation modulo 256
return ((sum ^ 0xFF) + 1) & 0xFF;
}
For the payload 01 03 00 00 00 0A, the byte sum is $1 + 3 + 0 + 0 + 0 + 10 = 14$ (0x0E). Taking the two's complement yields $256 - 14 = 242$ (0xF2), so the ASCII frame on the wire is :01030000000AF2\r\n.
Frequently Asked Questions (FAQ)
Q. Why does my calculated CRC-16 match the bytes in reverse order (e.g., CD C5 instead of C5 CD)?
While Modbus addresses and register values are transmitted Big-Endian (MSB first), the 16-bit CRC field at the end of a Modbus RTU frame is transmitted Little-Endian (Low Byte first, High Byte second).
Q. What error detection coverage does CRC-16/MODBUS provide?
CRC-16 detects 100% of all single-bit and double-bit errors, 100% of all errors with an odd number of bits, 100% of burst errors up to 16 bits in length, and 99.998% of longer burst errors.
Q. Does Modbus ASCII include the leading ':' colon or trailing CR/LF in the LRC calculation?
No. LRC is computed strictly over the decoded binary byte values of the Slave ID and PDU before hex-ASCII encoding, excluding the framing ':' and 'CR LF' characters.
Q. How can a high-speed gateway compute CRC-16 without an 8-iteration inner bit loop?
By precomputing a 256-entry 16-bit lookup table for all possible byte values (0x00..0xFF), each payload byte is processed in a single table lookup and XOR operation.