Skip to main content

Module codec

Module codec 

Source
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§

EncodeError
A value the encoder refuses to put on the wire.
FrameError
Why a byte stream could not be turned into a Modbus ADU.
ModbusRequest
A decoded, spec-legal Modbus request.
ModbusResponse
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 - 2 is 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_name returns 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.