budulinek / arduino-modbus-rtu-tcp-gateway

Arduino-based Modbus RTU to Modbus TCP/UDP gateway with web interface. Allows you to connect Modbus RTU slaves (such as sensors, energy meters, HVAC devices) to Modbus TCP/UDP masters (such as home automation systems). You can adjust settings through web interface.
GNU General Public License v3.0
172 stars 51 forks source link

Modbus rtuovertcp ModbusTcpClient: Connection unexpectedly closed x seconds into read of 2 bytes without response from unit before it closed connection #8

Closed ericvb closed 2 years ago

ericvb commented 2 years ago

I'm using HomeAssistant Modbus plugin (mode rtuovertcp) to get data from a serial modbus device (power meter SDM630) In between I have this arduino modbus rtu tcp gateway, build with a Arduino Uno and a W5500 shield.

It is working, I get the values from the power meter, but HA is reporting regularly that the item is Unavailable. In the logfile of HA, I find the error mentioned in the title. I opened a ticket in HA, but they are redirecting me to a problem in the gateway. https://github.com/home-assistant/core/issues/57426

They propose to debug the thing in the gateway. If I understand correctly, the diagnostic define cannot be uncommented in a Arduino Uno? I've done tests in HA with a configuration to get only one input register with a scan interval of 20 seconds, thinking that getting all needed registers is too much for the gateway. But still the same error.

What do you think? How to proceed to debug this? Is the Arduino not quick enough to do this and should I switch to a nano or a mega?

budulinek commented 2 years ago

Hi, thanks for the report.

First, make sure that you are running the latest version of the program because I have dealt with a very similar bug couple of months ago here.

You can not enable EXTRA_DIAG, because the program would not fit on Uno. https://github.com/budulinek/arduino-modbus-rtu-tcp-gateway/blob/5b5d36d70ffeb232e081cdf5cf481a931e53fcbe/arduino-modbus-rtu-tcp-gateway/arduino-modbus-rtu-tcp-gateway.ino#L60

The DEBUG mode prints some info to serial. Would not help you, because your problem is on lower level. https://github.com/budulinek/arduino-modbus-rtu-tcp-gateway/blob/5b5d36d70ffeb232e081cdf5cf481a931e53fcbe/arduino-modbus-rtu-tcp-gateway/arduino-modbus-rtu-tcp-gateway.ino#L54

According to the debug info from Home Assistant, it seems that Arduino speed is not an issue. It seems that Arduino unexpectedly closes TCP socket.

In the latest version of the program, I leave it up to Ethernet.h library to close the socket. To be more precise, i use server.available() function which is supposed to close the socket only if the other side closes connection - see https://github.com/arduino-libraries/Ethernet/blob/5cdcdbcc65925e93492d96e54f1520ec61c90038/src/EthernetServer.cpp#L60

ericvb commented 2 years ago

Ok, I have the version before your latest changes, so I will install the latest version and test with this version. I'll report back.

ericvb commented 2 years ago

Installed yesterday evening the latest code on the Arduino UNO. And the problem is gone, no more errors in the Home Assistant logfile and no more unavailable message on screen.

Maybe something you could add to the code: a version of the sketch software visible in the html pages. So users can directly see if there version is up to date or not.

Thanks for this open source code!

budulinek commented 2 years ago

Maybe something you could add to the code: a version of the sketch software visible in the html pages.

Good idea. Implemented in the latest commit. For version history see arduino-modbus-rtu-tcp-gateway.ino