Serial RTU Client
You can create a RTU client channel using the create_rtu_client
method. It will immediately try to open the serial port.
In Rust, you can use the spawn_rtu_client_task
to create a channel and spawn the async task in the context of the current runtime.
Outside the Tokio runtime, you can use create_rtu_handle_and_task
and manually spawn the returned future.
The library does not support sending broadcast requests yet. Sending a request with a broadcast unit ID
(0x00
) will generate a timeout error, because no server will respond.
- Rust
- C
- C++
- Java
- C#
let channel = spawn_rtu_client_task(
"/dev/ttySIM0", // path
rodbus::SerialSettings::default(), // serial settings
1, // max queued requests
default_retry_strategy(), // retry delays
DecodeLevel::new(
AppDecodeLevel::DataValues,
FrameDecodeLevel::Payload,
PhysDecodeLevel::Nothing,
),
Some(Box::new(LoggingListener)),
);
rodbus_client_channel_t* channel = NULL;
rodbus_decode_level_t decode_level = rodbus_decode_level_nothing();
rodbus_param_error_t err = rodbus_client_channel_create_rtu(runtime,
"/dev/ttySIM0", // path
rodbus_serial_port_settings_init(), // serial settings
1, // max queued requests
rodbus_retry_strategy_init(),
decode_level, // decode level
get_port_listener(),
&channel
);
if (err) {
printf("Unable to initialize channel: %s\n", rodbus_param_error_to_string(err));
return -1;
}
auto channel = rodbus::ClientChannel::create_rtu(
runtime,
"/dev/ttySIM0",
rodbus::SerialPortSettings(),
1,
rodbus::RetryStrategy(),
rodbus::DecodeLevel::nothing(),
std::make_unique<PrintingPortStateListener>()
);
ClientChannel channel = ClientChannel.createRtu(
runtime,
"/dev/ttySIM0", // path
new SerialPortSettings(), // serial settings
ushort(1), // max queued requests
new RetryStrategy(),
DecodeLevel.nothing(), // decode level
new PrintingPortStateListener()
);
var channel = ClientChannel.CreateRtu(
runtime, // runtime
"/dev/ttySIM0", // path
new SerialPortSettings(), // serial settings
1, // max queued requests
new RetryStrategy(),
DecodeLevel.Nothing(),
new PortStateListener()
);
Path
A path to the serial device must be supplied. On Windows, it's generally something like COM3
. On Linux, it's
generally something like /dev/ttyS3
. You need to have the adequate permissions to access these devices.
Serial Port settings
The serial port settings are the following:
- Baud rate in bit per second
- Data bits. Note that Modbus should use 8 data bits.
- Stop bits
- Parity
- Flow control
Maximum Queued Requests
Each channel sends one request at a time and has a fixed-length buffer of requests to send.
Retry Delay
A serial channel tries to open the serial port as soon as it is created. If the serial port cannot be opened, the library
automatically waits retry_delay
before retrying to open the port.
Decode Level
See logging configuration page for more details.