Skip to main content

Serial RTU client

Create an RTU client channel using the create_rtu method. Like a TCP client, the channel is created disabled: it doesn't open the serial port until you call enable.

note

In Rust, you can use spawn_rtu_client_task to create a channel and spawn its task in the current runtime. Otherwise, create_rtu_client_task returns the Channel and a ClientTask; spawn task.run() when ready.

let channel = spawn_rtu_client_task(
path, // path
rodbus::SerialSettings::default(), // serial settings
MAX_QUEUED_REQUESTS, // max queued requests
default_retry_strategy(), // retry delays
DecodeLevel::new(
AppDecodeLevel::DataHeaders,
FrameDecodeLevel::Nothing,
PhysDecodeLevel::Nothing,
),
Some(Box::new(PortStateListener)),
);

Serial port​

Supply the path of the serial device, such as COM3 on Windows or /dev/ttyS3 on Linux, where your program needs permission to open it. The serial port settings are the baud rate, data bits, stop bits, parity and flow control; the API docs describe each one. Modbus RTU uses 8 data bits.

Once enabled, the channel keeps the port open. If the port can't be opened, or fails later, for example when a USB-to-serial adapter is unplugged, the channel reopens it, waiting as the RetryStrategy sets, the same way a TCP client reconnects. Requests made while the port isn't open fail with a NoConnection error.

Broadcast​

A request to unit ID 0x00 is a broadcast. Servers apply a broadcast write without replying, so the client waits the request's timeout to give them time to process it, and then completes the write successfully. Give broadcasts a short timeout, typically 100 to 200 ms. A read can't be broadcast: one sent to 0x00 fails with a response timeout.

Other settings​

The request queue and decode level work as for a TCP client.

PortStateListener​

The PortStateListener interface (Listener<PortState> in Rust) has a single callback that informs your code about the state of the serial port: disabled, waiting to retry opening it, open, and shut down.