Search code examples
c++connection-timeoutmodbus

modbus communication decrease wait time to timeout


I'm writing a C++ app that will need to connect to various PLCs over modbus, the IPs of these PLCs is given by user input. Currently when the user puts in an IP that cannot be connected to, my program hangs for around 2 minutes in an attempt to connect and having my app hang for 2 minutes is not an option.

An example program illustrates the issue and my attempted fixes:

#include <modbus/modbus.h>
#include <string>
#include <errno.h>

#include <iostream>

#define PRINT_TIMEVAL(timeval) std::cout << "timeval sec: " << timeval.tv_sec << " usec: " << timeval.tv_usec << std::endl;

int main()
{
    std::string ip = "192.168.2.5";
    int port = 502;
    int slaveNum = 1;
    int address = 1;
    int nb = 1;

    struct timeval currentTimeout;

    struct timeval responseTimeout;
    responseTimeout.tv_sec = 1;
    responseTimeout.tv_usec = 0;

    struct timeval byteTimeout;
    byteTimeout.tv_sec = 1;
    byteTimeout.tv_usec = 0;

    modbus_t *mb = modbus_new_tcp(ip.c_str(), port);
    modbus_set_debug(mb, true);
    modbus_set_error_recovery(mb, MODBUS_ERROR_RECOVERY_NONE);
    modbus_flush(mb);
    modbus_set_slave(mb, slaveNum);

    modbus_get_response_timeout(mb, &currentTimeout);
    PRINT_TIMEVAL(currentTimeout);

    modbus_set_response_timeout(mb, &responseTimeout);

    modbus_get_response_timeout(mb, &currentTimeout);
    PRINT_TIMEVAL(currentTimeout);

    modbus_get_byte_timeout(mb, &currentTimeout);
    PRINT_TIMEVAL(currentTimeout);

    modbus_set_byte_timeout(mb, &byteTimeout);

    modbus_get_byte_timeout(mb, &currentTimeout);
    PRINT_TIMEVAL(currentTimeout);

    std::cout << "About to connect to " << ip << std::endl;
    int errno;
    if((errno = modbus_connect(mb)))
    {
        std::cout << "Error when connecting: " << modbus_strerror(errno) << std::endl;
    }
    std::cout << "Done connecting to " << ip << std::endl;

    modbus_close(mb);
    modbus_free(mb);

    return 0;
}

As you can see I've tried setting both the response and byte timeout variables to 1 second (I've also tried 500 and 5000 microseconds). When I read the timeout values they have been set properly so I'm assuming that they don't have anything to do with the initial connection attempt. I've also tried explicitly setting the error recovery mode to none in case it was trying to reconnect on its own.

I would like something that will either stop modbus_connect after x amount of time or another command that will allow me to check to see if the IP is valid before attempting to connect through modbus, this would also need to timeout after a short amount of time.

I'm using libmodbus version 3.0.1-2


Solution

  • The issue was with my version of libmodbus (3.0.1), which is the current release version. In that version they were using the linux connect command but they weren't passing a NONBLOCKING flag, thus connect would become blocked for 2m7s. We resolved this issue by upgrading to libmodbus v3.1.1 which is marked as unstable but is not under active development (they're developing on v3.1.2). Unfortunately that version of libmodbus does not work for windows.