Expand description
Modbus TCP wire format: MBAP header + PDU.
Reference: MODBUS Application Protocol Specification V1.1b3 and MODBUS Messaging on TCP/IP Implementation Guide V1.0b.
This module is deliberately free of any I/O, LLM or state: it turns bytes into
ModbusRequest values and turns answers back into bytes. Everything that the
specification determines mechanically (frame legality, quantity limits, the shape of
a write echo) lives here; everything a device would decide (what a register reads
as, whether a write is accepted) lives in actions.rs and is answered by the model.
There is no register file here, by design — see src/server/modbus/CLAUDE.md.
Structs§
- Adu
- A complete Modbus/TCP Application Data Unit.
Enums§
- Encode
Error - A value the encoder refuses to put on the wire.
- Frame
Error - Why a byte stream could not be turned into a Modbus ADU.
- Modbus
Request - A decoded, spec-legal Modbus request.
- Modbus
Response - A decoded response, checked against the request it answers.
Constants§
- EXCEPTION_
FLAG - Bit OR-ed into the function code of an exception response.
- EXC_
ACKNOWLEDGE - EXC_
GATEWAY_ PATH_ UNAVAILABLE - EXC_
GATEWAY_ TARGET_ FAILED - EXC_
ILLEGAL_ DATA_ ADDRESS - EXC_
ILLEGAL_ DATA_ VALUE - EXC_
ILLEGAL_ FUNCTION - EXC_
MEMORY_ PARITY_ ERROR - EXC_
SERVER_ DEVICE_ BUSY - EXC_
SERVER_ DEVICE_ FAILURE - FC_
READ_ COILS - FC_
READ_ DISCRETE_ INPUTS - FC_
READ_ HOLDING_ REGISTERS - FC_
READ_ INPUT_ REGISTERS - FC_
WRITE_ MULTIPLE_ COILS - FC_
WRITE_ MULTIPLE_ REGISTERS - FC_
WRITE_ SINGLE_ COIL - FC_
WRITE_ SINGLE_ REGISTER - MAX_
ADU_ LEN - Largest legal ADU on TCP: MBAP header + PDU.
- MAX_
PDU_ LEN - Largest legal PDU (function code + data), per the Modbus spec.
- MAX_
READ_ RESPONSE_ DATA - Most data octets a read response can carry: the PDU ceiling less the function code and the
byte-count octet. The byte count is one octet, and
MAX_PDU_LEN - 2is 251, so the PDU bound is the tighter of the two and the only one that has to be stated. - MBAP_
HEADER_ LEN - Length of the MBAP header: transaction id (2), protocol id (2), length (2), unit id (1).
- MODBUS_
PROTOCOL_ ID - Protocol identifier reserved for Modbus in the MBAP header.
- UNKNOWN_
EXCEPTION - What
exception_namereturns for a code the specification does not define.
Functions§
- encode_
adu - Wrap a PDU in an MBAP header addressed back to the requester.
- encode_
bits_ response - PDU for a FC 1/2 read response: byte count then packed bits, least-significant bit of the first byte carrying the first requested coil.
- encode_
exception - PDU for an exception response:
function_code | 0x80, then the exception code. - encode_
registers_ response - PDU for a FC 3/4 read response: byte count then big-endian registers.
- encode_
request - Encode a request’s PDU.
- encode_
write_ ack - PDU acknowledging a write.
- exception_
code_ from_ name - Map a canonical exception name back to its code.
- exception_
name - Canonical name for an exception code, for logs and for the model’s vocabulary.
- parse_
request - Decode a PDU.
- parse_
response - Decode a response PDU against the request it answers.
- try_
parse_ adu - Try to pull one complete ADU off the front of
buf.