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);
}