Modbus CRC-16 (0xA001) C99 & Python 3 Implementation & Test Vector

Copy verified zero-dependency C99 and Python 3 CRC-16/MODBUS functions with known test vectors, wire-order packing and bit-shift explanation.

Jump to results

Inputs

Frame mode & addressing
1–247 unicast, 0 broadcast.
Zero-based protocol PDU address (0–65535).
Count for reads, 0 or 1 for FC05, or a 16-bit value for FC06.
Raw validation & RS-485 bus

Results

Estimates
Modbus RTU Frame (Hex)01 03 00 00 00 0A C5 CD
Computed CRC-16
0xCDC5
Wire CRC order (Lo Hi)
C5 CD
Slave ID & Function
ID 1 · Read Holding Registers (0x03)
Frame length / air time
8 bytes · 0.76 ms
Inter-char timeout (t1.5)
750 µs
Frame silent gap (t3.5)
1,750 µs
RTU frame byte map · 8 bytes
B0 01 B1 03 B2 00 B3 00 B4 00 B5 0A B6 C5 B7 CD CRC-16/MODBUS: 0xCDC5 · Wire order: C5 CD (Lo → Hi)
  • Slave ID
  • Function Code
  • Address / Payload
  • CRC-16 Lo / Hi
Frame field breakdown
FieldBytes (Hex)Byte count
Slave ID011
Func 0x03031
Address00 002
Quantity00 0A2
CRC LoC51
CRC HiCD1
Copyable C99 & Python 3 CRC-16/MODBUS functions
#include <stddef.h>
#include <stdint.h>

/* CRC-16/MODBUS: init 0xFFFF, reflected polynomial 0xA001, wire order Lo then Hi */
uint16_t modbus_crc16(const uint8_t *buf, size_t len) {
  uint16_t crc = 0xFFFFU;
  for (size_t i = 0; i < len; ++i) {
    crc ^= (uint16_t)buf[i];
    for (int bit = 0; bit < 8; ++bit) {
      if (crc & 0x0001U) crc = (uint16_t)((crc >> 1) ^ 0xA001U);
      else crc >>= 1;
    }
  }
  return crc;
}

/* Test vector: 01 03 00 00 00 0A C5 CD (CRC=0xCDC5, wire: 0xC5 0xCD) */
static const uint8_t frame_pdu[6] = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x0A };
def modbus_crc16(data: bytes) -> int:
    crc = 0xFFFF
    for b in data:
        crc ^= b
        for _ in range(8):
            crc = ((crc >> 1) ^ 0xA001) if (crc & 1) else (crc >> 1)
    return crc & 0xFFFF

pdu = bytes.fromhex("01 03 00 00 00 0A")
crc = modbus_crc16(pdu)
rtu_frame = pdu + crc.to_bytes(2, "little")
assert rtu_frame.hex(" ").upper() == "01 03 00 00 00 0A C5 CD"
print(rtu_frame.hex(" ").upper())

Modbus RTU transmits the CRC low byte first, followed by the high byte, whereas multi-byte register addresses and values are big-endian.

Drop-in C99 and Python 3 CRC-16/MODBUS with exact test vectors

CRC-16/MODBUS uses initial value `0xFFFF`, no final XOR (`0x0000`), and reflected polynomial `0xA001` (the bit-reversal of `0x8005`) processed LSB-first. Confusing it with CRC-16/CCITT (`0x1021`) or XMODEM produces completely incompatible checksums.

Open the code panel below the results to copy the self-contained C99 function (zero lookup-table flash overhead) or Python 3 script with an assertion against your active frame.

Formulas

  • Init = 0xFFFF; Poly_reflected = 0xA001; RefIn = True; RefOut = True; XorOut = 0x0000
  • Check("123456789") = 0x4B37
  • Wire append: buf[len] = crc & 0xFF; buf[len+1] = crc >> 8

Worked example

Running the bit-shift C99 or Python loop over `[0x01, 0x03, 0x00, 0x00, 0x00, 0x0A]` returns `0xCDC5`, appending `0xC5, 0xCD` on the wire.

Frequently asked questions

When should I use a 256-entry CRC lookup table vs the bit-shift loop?

The bit-shift loop uses ~40 bytes of flash and processes an 8-byte frame in microseconds at 16+ MHz; use a 512-byte LUT only on high-speed DMA links where CPU cycles are scarce.

Why does CRC-16/MODBUS use 0xA001 instead of 0x8005?

Because Modbus shifts bits out LSB-first, reversing the normal polynomial 0x8005 yields 0xA001 and eliminates per-byte bit-reflection operations.

What is the result of computing CRC-16/MODBUS over a complete valid frame including its CRC?

Passing the PDU plus its Lo/Hi CRC bytes into `modbus_crc16()` always evaluates to exactly `0x0000` for any valid RTU frame.