libmodbus - monitor and restart connection

1.2k views Asked by At

I am developing a Modbus TCP client on Linux to communicate with different Modbus devices using libmodbus.

To initialize the TCP connection, the function int modbus_connect(modbus_t *ctx); is called and returns 0 if the connection was successful.

I need to have one separate thread that continuously monitors this TCP connection to the device to see if it's still connected, and if not, attempt to connect again.

Continuously calling modbus_connectto check if the connection was successful will create multiple connections which is not what I want.

I could attempt to continuously try and read from the device and if there is an error, check the error code and try to connect again. However, The writing and reading to / from the Modbus device happens from a different class and thread which should be extracted from the communication control.

There doesn't seem to be any function in libmodbus to support this.

Any suggestions are appreciated.

0

There are 0 answers