How to Calculate the Modbus CRC-16 (With a Worked Example)

The exact CRC-16 algorithm Modbus RTU uses — polynomial, initial value, byte order — plus a hand-worked frame check and reference implementations in Python and C.

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.

The specification

ParameterValue
Width16 bits
Polynomial0x8005 (normal), used as 0xA001 (reflected)
Initial value0xFFFF
Input reflectionYes
Output reflectionYes
Final XOR0x0000
Byte order on the wireLow 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.

The algorithm, step by step

For each byte of the frame (excluding the CRC itself):

  1. XOR the byte into the low half of the 16-bit register: crc ^= byte
  2. Repeat 8 times:

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

A worked example

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.

Python implementation

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"

C implementation

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.

Four mistakes that account for nearly every CRC failure

  1. Initial value 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.
  2. High byte first. The low byte goes on the wire first. If you reverse it, the frame is off by exactly two bytes' worth of error and the slave stays silent.
  3. Including the slave address in the wrong place. The CRC covers address, function, and data — but not the inter-frame silence, and in Modbus TCP there is no CRC at all.
  4. Accidentally computing the CRC over ASCII hex. If your frame arrives as the string "01030000", the bytes 0x30 0x31 … are what you hashed. Parse to bytes first.

RTU versus TCP: where the CRC disappears

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.