carlok — zsh — 88×30

cat _posts/2026-10-31-contributed-to-pymodbus-and-libmodbus-two-modbus-servers-disagreeing-on-the-wire-four-bugs.md

Contributed to pymodbus and libmodbus: two Modbus servers disagreeing on the wire, four bugs

Four protocol-conformance bugs went upstream to the two Modbus implementations on 30 October, each found by differential testing one against the other: the same raw request put on the wire, the two answers compared byte for byte. pymodbus-dev/pymodbus#3074 was merged on 31 October by janiversen (three files, +39/−2): a Write Single Coil request (0x05) with a value other than 0xFF00 or 0x0000 used to be carried out — any non-zero value turned the coil on — and answered with an echoed 0xFF00 the client never sent, where the specification’s state diagram answers exception 03. WriteSingleCoilRequest now keeps the decoded value and datastore_update answers ILLEGAL_VALUE before touching the datastore.

Three remain open. pymodbus#3075 takes the function codes 0x81–0xFF, reserved by the spec for exception responses: the server decoded them as exception responses, hit NotImplementedException and answered SLAVE_FAILURE (81 00 00 → 81 04) instead of the ILLEGAL_FUNCTION an unknown code gets, so DecodePDU.decode now raises ModbusIOException for them and two tests that used the server decoder on a response moved to the client side. In libmodbus#888, response_exception() and modbus_reply_exception() build the exception’s function byte as sft->function + 0x80 in a byte, so 81 00 00 is answered 01 01 — a Read Coils response with a byte count and no data, and 0x80 wraps to 0x00; the fix is | 0x80, with a raw-request test in unit-test-client.c.

libmodbus#889 is the same shape of hole in Write Multiple Coils (0x0F): a byte count too small for the quantity was refused, a byte count too large was accepted and the write carried out from the first data byte, where the state diagram wants nb_bits != (nb / 8) + ((nb % 8) ? 1 : 0) and exception 03 — the expression modbus.c already uses elsewhere, and what Write Multiple Registers already checks in both directions. Each pull request carries a wire-level test that fails on the parent commit and passes on the branch, plus the repositories’ own lint, shellcheck and distcheck runs.