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.
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.
- Rust
- C
- C++
- Java
- C#
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)),
);
rodbus_client_channel_t *channel = NULL;
rodbus_decode_level_t decode_level = rodbus_decode_level_build(RODBUS_APP_DECODE_LEVEL_DATA_HEADERS, RODBUS_FRAME_DECODE_LEVEL_NOTHING, RODBUS_PHYS_DECODE_LEVEL_NOTHING);
rodbus_param_error_t err = rodbus_client_channel_create_rtu(runtime,
path, // path
rodbus_serial_port_settings_init(), // serial settings
MAX_QUEUED_REQUESTS, // max queued requests
rodbus_retry_strategy_init(), // retry delays
decode_level, // decode level
get_port_state_listener(), // listener
&channel
);
if (err) {
printf("unable to create channel: %s\n", rodbus_param_error_to_string(err));
return NULL;
}
auto channel = rodbus::ClientChannel::create_rtu(
runtime,
path,
rodbus::SerialPortSettings(),
MAX_QUEUED_REQUESTS,
rodbus::RetryStrategy(),
rodbus::DecodeLevel(rodbus::AppDecodeLevel::data_headers, rodbus::FrameDecodeLevel::nothing, rodbus::PhysDecodeLevel::nothing),
create_port_state_listener()
);
ClientChannel channel = ClientChannel.createRtu(
runtime,
path, // path
new SerialPortSettings(), // serial settings
ushort(MAX_QUEUED_REQUESTS), // max queued requests
new RetryStrategy(), // retry delays
new DecodeLevel(AppDecodeLevel.DATA_HEADERS, FrameDecodeLevel.NOTHING, PhysDecodeLevel.NOTHING),
new PrintingPortStateListener()
);
var channel = ClientChannel.CreateRtu(
runtime,
path,
new SerialPortSettings(), // 9600 baud, 8 data bits, no parity, 1 stop bit
MaxQueuedRequests,
new RetryStrategy(),
new DecodeLevel(AppDecodeLevel.DataHeaders, FrameDecodeLevel.Nothing, PhysDecodeLevel.Nothing),
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.