Skip to main content

Crate mbrtu_ffi

Crate mbrtu_ffi 

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

MbrtuExceptionResponse
An exception response: the function code that failed (high bit already cleared) and why.
MbrtuFfiRequest
Opaque handle to a decoded Modbus RTU request, owned by the caller until freed with mbrtu_request_free.
MbrtuFfiResponse
Opaque handle to a decoded Modbus RTU response, owned by the caller until freed with mbrtu_response_free.
MbrtuReadCoilsRequest
Read Coils (function code 1) request: which coils to read.
MbrtuReadCoilsResponse
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.
MbrtuReadHoldingRegistersRequest
Read Holding Registers (function code 3) request: which registers to read.
MbrtuReadHoldingRegistersResponse
Read Holding Registers (function code 3) response: the register values read. registers[..count] are meaningful; the rest are unused padding.
MbrtuWriteSingleRegister
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§

MbrtuFfiError
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 (len bytes long) as a request. buf must 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 (len bytes long) as a response. See mbrtu_decode_request for the framing/error contract, which is otherwise identical - except that a frame with the broadcast address (0) is rejected with MbrtuFfiError::Frame, since Modbus RTU never sends a response to a broadcast request.
mbrtu_request_address⚠
The request’s unit/slave address, or 0 for a NULL handle.
mbrtu_request_as_read_coils⚠
Reads req’s request as Read Coils into out, returning true on a match. Returns false without writing to out if req is NULL or holds a different function code.
mbrtu_request_as_read_holding_registers⚠
Reads req’s request as Read Holding Registers into out, returning true on a match. Returns false without writing to out if req is NULL or holds a different function code.
mbrtu_request_as_write_single_register⚠
Reads req’s request as Write Single Register into out, returning true on a match. Returns false without writing to out if req is 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 0xFF for a NULL handle.
mbrtu_response_address⚠
The response’s unit/slave address, or 0 for a NULL handle.
mbrtu_response_as_exception⚠
Reads resp’s response as an exception into out, returning true on a match. Returns false without writing to out if resp is NULL or holds a normal (non-exception) response.
mbrtu_response_as_read_coils⚠
Reads resp’s response as Read Coils into out, returning true on a match. Returns false without writing to out if resp is NULL or holds a different function code.
mbrtu_response_as_read_holding_registers⚠
Reads resp’s response as Read Holding Registers into out, returning true on a match. Returns false without writing to out if resp is NULL or holds a different function code.
mbrtu_response_as_write_single_register⚠
Reads resp’s response as Write Single Register into out, returning true on a match. Returns false without writing to out if resp is 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 0xFF for a NULL handle.
mbrtu_version
This crate’s version string, as a NUL-terminated C string with static lifetime.