Arduino OPTA Ethernet client connect timeout

Hello everyone,

I am creating an arduino OPTA program that will communicate with MODBUS TCP servers. The addresses of the MODBUS TCP serves are not known initially, so I scan my the address range my arduino is currently located and try to connect with ethernetclient.connect() method.

My question is: Is there a way to set the timeout for the connect method? Everytime I check an IP address that there is no MODBUS TCP server, the ethernetclient.connect() takes 30 seconds to reply that it was not possible to connect. Since I will be scanning aprox. 255 addresses, this could take hours, which is not acceptable.

I have tried modifying the Ethernet.h file but it did not affect the timeout. I have also tried client.setTimeout() which did not work.

Here is a part of the code I am using:

IPAddress current_IP_range(first_oct, second_oct, third_oct, 0);
  uint8_t net_iteration;
  int Max = 0;
  ethClient.setTimeout(3); // does not work
  for(net_iteration = 1; net_iteration < 255 && Max < NUMBER; net_iteration++){
    if (!ethClient.connect(current_IP_range, 502)) {
      Serial.println("Modbus TCP Client failed to connect!");
    } else {
      Serial.println("Modbus TCP Client connected");
      LOGOIPAddresses[Max] = current_IP_range;
      Max = Max + 1;
    }
    Serial.println("disconnecting.");
    ethClient.stop();
    current_IP_range[3] = current_IP_range[3] + 1;
    delay(100);
  }

for now you can use setTimeout. But It will be renamed SocketWrapper: avoid Stream setTimeout shadowing by pennam · Pull Request #769 · arduino/ArduinoCore-mbed · GitHub

Hello Juraj,

Thank you for your reply. As I mentioned in my original post, the setTimeout does not affect the connect() method timeout. I have already tried it, like:
ethClient.setTimeout(1000);

sorry. I forgot that I fond out a few months ago that it doesn't apply for connect

For anyone intrested in this topic, my solution was this:

  1. Change MBED_CONF_LWIP_TCP_CLOSE_TIMEOUT or MBED_CONF_LWIP_TCP_MAXRTX in the Arduino15\packages\arduino\hardware\mbed_opta\4.0.10\variants\OPTA\mbed_config.h file
  2. Rebuild libmbed.a using the methods described in their README

I guess it is a lot of work but I could not find other way.