Every Modbus RTU frame ends with a 16-bit CRC. Get it wrong and the slave discards the frame silently — no exception, no reply. That single failure mode makes CRC the most common reason a hand-built RTU frame never gets an answer.
Here is the exact specification, a hand-worked example, and code that produces byte-identical output.
| Parameter | Value |
|---|---|
| Width | 16 bits |
| Polynomial | 0x8005 (normal), used as 0xA001 (reflected) |
| Initial value | 0xFFFF |
| Input reflection | Yes |
| Output reflection | Yes |
| Final XOR | 0x0000 |
| Byte order on the wire | Low byte first, then high byte |
Because input and output are both reflected, implementations use the reversed polynomial 0xA001 and shift right instead of left. That is why almost every code sample you see uses 0xA001.
For each byte of the frame (excluding the CRC itself):
crc ^= bytecrc = (crc >> 1) ^ 0xA001crc = crc >> 1After all bytes, the register holds the CRC. Transmit low byte first.
Note the crucial detail: the initial value is 0xFFFF, not 0x0000. Using 0x0000 is the classic bug — the CRC looks plausible but every slave rejects it.
Request: read 10 holding registers from slave 0x01, starting at address 0x0000, function 0x03.
Frame bytes before CRC:
01 03 00 00 00 0A
Running the algorithm over those six bytes yields 0xCDC5. Since the low byte goes first, the wire order is C5 CD:
01 03 00 00 00 0A C5 CD
Cross-check it the fast way — the CRC of a complete valid frame, CRC bytes included, is always 0x0000:
crc16(bytes.fromhex("0103000000 0AC5CD".replace(" ", ""))) # -> 0
That self-check is the cheapest assertion you can put in a test suite. If a captured frame does not reduce to zero, either it is corrupt or your CRC implementation is wrong — and the two failures look identical from the slave's perspective.
def crc16(data: bytes, crc: int = 0xFFFF) -> int:
for b in data:
crc ^= b
for _ in range(8):
if crc & 0x0001:
crc = (crc >> 1) ^ 0xA001
else:
crc >>= 1
return crc & 0xFFFF
def append_crc(frame: bytes) -> bytes:
c = crc16(frame)
# low byte first
return frame + bytes([c & 0xFF, (c >> 8) & 0xFF])
assert crc16(bytes.fromhex("0103000000 0A".replace(" ", ""))) == 0xCDC5
assert append_crc(bytes.fromhex("0103000000 0A".replace(" ", ""))).hex() == "01030000000ac5cd"
uint16_t modbus_crc16(const uint8_t *buf, size_t len) {
uint16_t crc = 0xFFFF;
for (size_t i = 0; i < len; i++) {
crc ^= buf[i];
for (int bit = 0; bit < 8; bit++) {
if (crc & 0x0001) crc = (crc >> 1) ^ 0xA001;
else crc >>= 1;
}
}
return crc;
}
On a microcontroller the table-driven version is roughly 8× faster, but for frames under 256 bytes the loop above costs microseconds. Do not optimise it before you measure it.
0x0000 instead of 0xFFFF. Produces a stable, wrong CRC. Very common in code copied from CRC-16/ARC examples, which differ only in the initial value."01030000", the bytes 0x30 0x31 … are what you hashed. Parse to bytes first.Modbus TCP replaces the CRC with the 7-byte MBAP header (transaction ID, protocol ID 0x0000, length, unit ID). There is no checksum — TCP already guarantees integrity.
So a frame that looks like this on RTU:
01 03 00 00 00 0A C5 CD
looks like this on TCP:
00 01 00 00 00 06 01 03 00 00 00 0A
^^^^^ ^^^^^ ^^^^^ ^^ ^^^^^^^^^^^^^^^^^
txn proto len unit PDU
If you are debugging a gateway and the RTU side fails while TCP works, CRC or timing is the culprit. If both fail, the problem is in the PDU itself.
You can paste any RTU or TCP frame into the ModbusLab message lab to see the CRC recomputed and verified byte by byte, or send frames to the live TCP slave and watch which ones get answered.