I'd like to try 10 immediate modbus connections. However, everytime I fail to connect, and I have to wait for 2 minutes for the next connection because the previous modbus_connect call is still actively listening. So, if I fail to connect 10 times, I have to wait for 20 minutes.
int max_tries = 10;
int retries = 0;
while ((modbus_connect(ctx) == -1) && retries < max_retries){
retries++;
// wait 2 mins
// I need to remove this waiting time
}
Can someone help me to reduce the time for timeout? I'm using Libmodbus v3.1.6
If you are talking about TCP connections, the behavior of your program may be correct.
There are several things involved here because you say you establish the connection and "is actively listening." Can't be both.
If you establish the connection, the only thing I can think of is that normally the
connect
(low level, not modbus) will try several times (after connecting) to sendSYN
packets (more or less two minutes) and if there is no response drop the connection.That may be one problem.
If you are listening, you have to set the SO_REUSEADDR socket option.
In any case, you should verify
errno
and get the error description to know what is happening to your connection.