modbus-rtu
This crate provides helpers for building and decoding standard Modbus RTU request and response packets.
It now ships with a synchronous Master that can talk to a serial port directly, while still
exposing the lower-level building blocks for applications that prefer to manage framing themselves.
Usage
High-level master (auto write/read)
use ;
The master enforces the Modbus RTU silent interval (T3.5) before/after each transmission, flushes the TX buffer, reads until the slave stops talking, and automatically decodes the reply.
Opting out of the master to shrink binaries
The synchronous master and its serialport dependency are enabled by default. If you only need the
packet-building utilities, disable default features in your Cargo.toml:
[]
= { = "1.1", = false }
You can always re-enable the high-level API with features = ["master"] when needed.
Manual packet construction
First, construct the function you want to issue.
The following example reads four input registers starting at address 0x1234.
use Function;
let starting_address: u16 = 0x1234;
let quantity: usize = 4;
let function = ReadInputRegisters ;
Next, build the request with the target device information and timeout.
use ;
...
let modbus_id: u8 = 1;
let timeout: Duration = from_millis;
let request = new;
Finally, convert the request into a Modbus RTU frame.
...
let packet: = request.to_bytes.expect;
You can now write packet through any transport of your choice (UART, TCP tunnel, etc.).
Receiving
With the original request available, attempt to decode the response bytes as shown below.
use Response;
...
let bytes: & = ... ; // user-implemented receive logic
let response = from_bytes.expect;
match response