#connection #automatic #failure #modbus #robust #layer #reconnect

modbus-robust

Layer over tokio-modbus to automatically reconnect on failure

2 unstable releases

0.2.0 Dec 6, 2024
0.1.0 Dec 25, 2023

#1390 in Network programming

Download history 8/week @ 2024-08-26 4/week @ 2024-09-16 25/week @ 2024-09-23 4/week @ 2024-09-30 4/week @ 2024-11-25 117/week @ 2024-12-02 46/week @ 2024-12-09

167 downloads per month
Used in 2 crates

GPL-3.0-or-later

18KB
315 lines

Make tokio-modbus more robust to connection loss

This library is a layer on top of tokio-modbus. When a modbus call fails, it will automatically discard the current connection and reconnect. This makes it robust to TCP connection failures, such as can be caused by restarting mbusd.

This is not a complete retry mechanism. For example, if reconnecting fails, the call will fail rather than making further attempts to connect. If multiple retries are desired, they can be layered on top of this library, without needing to worry about re-establishing failed connections as each call will automatically attempt to do so.

It also does not implement any form of timeout; again, this should be layered on top.

Finally, note that it is possible that a call may actually be received twice by the modbus device. For example, it could be that the command was successfully received by the reply failed to be delivered. This library should thus only be used if the calls are idempotent.

Usage

For simple use cases, use either new_tcp_slave for TCP/IP or new_rtu_slave for serial. The latter is a shortcut for cases where only the device path and baud rate need to be set for the serial port. If other options (such as parity) need to be changed, use new_sync: it takes a factory function that produces a connection. It only works for synchronous connection functions.

For more advanced use cases, implement the SyncConnector trait to specify how to establish a connection (asynchronously). Then pass the implementation to RobustClient::new_context.

Dependencies

~5–14MB
~181K SLoC