Using the Arduino library ArduinoModbus.h I am able to read the Holding registers of a Hiking DDS238-2ZN/S power meter with a MKR1010 via TCP but somewhat slowly compared to using a direct Modbus RTU connection to the MKR
The MKR is connected by wifi (-53dbm) to my local router and running TCP Client code based on the example in
https://github.com/arduino-libraries/ArduinoModbus/blob/master/examples/TCP/WiFiModbusClientToggle/WiFiModbusClientToggle.ino
The Hiking power meter is connected by a RTU wire pair to a Max RS485 interface then via Serial1 to a Mega 2560 which has a Arduino Ethernet Shield. This is connected to my local router by ethernet cable.
The Mega is running Modbus gateway RTU to TCP code from
https://github.com/budulinek/Arduino-Modbus-RTU---Modbus-TCP-UDP-Gateway/blob/master/Modbus_Gateway.ino
Before the next read I get the following error message from the MKR
read fail lastError =Connection timed out.
I have tried varying the default TCP timeout in the Client MKR from 30sec to 1sec
modbusTCPClient.setTimeout(mytimeout);
The 'last Error' message occurs regardless of the reading frequency ie 2 sec reads, 5 sec reads, so a new attempt to make a TCP connection is made after nearly every read which is usually successful.
The data being read is correct but the timing of the reads are not reliable due to the connection timeout delays.
What might be causing the Connection timeout?
Can I make the TCP connection persistent?
Is the problem in the Client MKR