Expand description
C FFI bindings for the mbrtu Modbus RTU decoder.
Exposes a decode-only subset of mbrtu’s API: decode a single frame
from a raw byte buffer into an opaque handle, then read its fields via
an mbrtu_request_as_<type>/mbrtu_response_as_<type> function that
fills a plain C struct. Request and response decoding are separate
entry points because Modbus can’t tell the two apart from the bytes
alone (see mbrtu’s own message module docs) - the caller must
already know which one it’s looking at. See include/mbrtu.h for the
generated C declarations.
This crate is a plain (non-no_std) library: it targets host
environments linking a shared or static C library, not the embedded
no_alloc use case mbrtu itself supports. Embedded consumers should
use the mbrtu Rust crate directly.
Structs§
- Mbrtu
Exception Response - An exception response: the function code that failed (high bit already cleared) and why.
- Mbrtu
FfiRequest - Opaque handle to a decoded Modbus RTU request, owned by the caller
until freed with
mbrtu_request_free. - Mbrtu
FfiResponse - Opaque handle to a decoded Modbus RTU response, owned by the caller
until freed with
mbrtu_response_free. - Mbrtu
Read Coils Request - Read Coils (function code 1) request: which coils to read.
- Mbrtu
Read Coils Response - Read Coils (function code 1) response: the coil values read, packed
one bit per coil (LSB of
bits[0]is the first requested coil).bits[..byte_count]are meaningful. - Mbrtu
Read Holding Registers Request - Read Holding Registers (function code 3) request: which registers to read.
- Mbrtu
Read Holding Registers Response - Read Holding Registers (function code 3) response: the register
values read.
registers[..count]are meaningful; the rest are unused padding. - Mbrtu
Write Single Register - Write Single Register (function code 6) request or response - the two share an identical wire shape (the response echoes the confirmed address and value).
Enums§
- Mbrtu
FfiError - Error codes returned by
mbrtu_decode_request/mbrtu_decode_response.
Constants§
- MBRTU_
FFI_ MAX_ COIL_ BYTES - Bytes needed to hold the largest possible Read Coils response’s packed bits, per the Modbus spec (2000 coils / 8 bits per byte).
- MBRTU_
FFI_ MAX_ REGISTERS - Largest number of registers a single Read Holding Registers response can carry, per the Modbus spec.
Functions§
- mbrtu_
decode_ ⚠request - Decodes a single Modbus RTU frame from
buf(lenbytes long) as a request.bufmust contain exactly one frame - Modbus RTU has no length field of its own, so there’s no trailing-bytes case to skip past the way a preamble-based protocol’s decoder would. - mbrtu_
decode_ ⚠response - Decodes a single Modbus RTU frame from
buf(lenbytes long) as a response. Seembrtu_decode_requestfor the framing/error contract, which is otherwise identical - except that a frame with the broadcast address (0) is rejected withMbrtuFfiError::Frame, since Modbus RTU never sends a response to a broadcast request. - mbrtu_
request_ ⚠address - The request’s unit/slave address, or
0for a NULL handle. - mbrtu_
request_ ⚠as_ read_ coils - Reads
req’s request as Read Coils intoout, returningtrueon a match. Returnsfalsewithout writing tooutifreqis NULL or holds a different function code. - mbrtu_
request_ ⚠as_ read_ holding_ registers - Reads
req’s request as Read Holding Registers intoout, returningtrueon a match. Returnsfalsewithout writing tooutifreqis NULL or holds a different function code. - mbrtu_
request_ ⚠as_ write_ single_ register - Reads
req’s request as Write Single Register intoout, returningtrueon a match. Returnsfalsewithout writing tooutifreqis NULL or holds a different function code. - mbrtu_
request_ ⚠free - Frees a handle returned by
mbrtu_decode_request. Passing NULL is a no-op. - mbrtu_
request_ ⚠function_ code - The request’s function code, or
0xFFfor a NULL handle. - mbrtu_
response_ ⚠address - The response’s unit/slave address, or
0for a NULL handle. - mbrtu_
response_ ⚠as_ exception - Reads
resp’s response as an exception intoout, returningtrueon a match. Returnsfalsewithout writing tooutifrespis NULL or holds a normal (non-exception) response. - mbrtu_
response_ ⚠as_ read_ coils - Reads
resp’s response as Read Coils intoout, returningtrueon a match. Returnsfalsewithout writing tooutifrespis NULL or holds a different function code. - mbrtu_
response_ ⚠as_ read_ holding_ registers - Reads
resp’s response as Read Holding Registers intoout, returningtrueon a match. Returnsfalsewithout writing tooutifrespis NULL or holds a different function code. - mbrtu_
response_ ⚠as_ write_ single_ register - Reads
resp’s response as Write Single Register intoout, returningtrueon a match. Returnsfalsewithout writing tooutifrespis NULL or holds a different function code. - mbrtu_
response_ ⚠free - Frees a handle returned by
mbrtu_decode_response. Passing NULL is a no-op. - mbrtu_
response_ ⚠function_ code - The response’s function code (with the high bit set for an exception
response), or
0xFFfor a NULL handle. - mbrtu_
version - This crate’s version string, as a NUL-terminated C string with static lifetime.