-
Notifications
You must be signed in to change notification settings - Fork 986
Can't reconnect with TCP client #2619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hello, I am recently facing issue with the reconnection, and i also receive "Connection reset by peer" i am using pymodbus 3.7.4 with python in 3.11.11. Running in a docker Debian |
First of all this is not the HA project....and HA is really using an old release. You do not write which version of pymodbus you use, but first part is to upgrade to the latest release. As you can see in the log pymodbus tries to reconnect, but your device do not accept the connection. You probably need to play with the timeout/retry parameters. As the log shows this is not a pymodbus problem. |
@maximeLeurent please update to the newest release and if the problem persist add a debug log, so we can see what happens. |
Yes i was making my research I saw this update #2501 |
I know this is not the HA project, but I'm trying to find where my problem is.
I do write which version of pymodbus I use: it's the version 3.8.6 and it's the current latest release.
I can connect() again immediately as after several seconds and in-between. But the automatic reconnect can't reconnect, even with testing several values for reconnect_delay, timeout or retries. |
If you can connect again after several seconds, then the reconnect will also work after several seconds....it is the same code being executed. Your example code is ok, no need to reduce that further, it clearly shows your device not responding. |
Please be aware that #2501 was wrong and is corrected in a later commit, both are in v3.8.6 |
I don't understand you here. My example of code and the logs show that reading several values in line is working, but after waiting at least 0.5 second (the time to lose the connection, I suppose) the next reading and the reconnecting are not working, but connect()ing again is working.
This device might be badly engineered, but the proprietary software works with it, as is Zabbix modbus plugin. The only thing that is not working between this device and the pymodbus library seems to be the reconnection. |
I don't understand how the test if not self.transport: can check if the connection is operational. Or what you want pymodbus to manage at its level. |
You might not understand how "if not self.transport" can check if the connection is operational, the reason however is simple, that is NOT what that "if" does, the check is done at a lower level. I have no idea what option you are looking for, if you want that you simply do a close/connect. It seems your device disconnects very rapidly normally it is 30-60 seconds, NOT 0.5 second. But the question is not when it disconnects but when it accept connections again, and for that you need to play with timeout/retry. Of course I assume we are only talking about v3.8.6 because in v3.8.3 there are known reconnect problems. |
The device always accept my connections and any read following the last request in less than 0.5 second, from my tests. After that, a new connection is mandatory.
I want to get my data from my device from Home Assistant. Home Assistant use pymodbus. pymodbus can't reconnect itself to my device before read, and Home Assistant don't either. So Home Assistant can only read one value per boot.
Yes, I'm talking about v3.8.6 and newer. And if my problem with Home Assistant was just an old version of a library, I would talk with them to update. So my question for you is: do you want pymodbus to handle the case where a device need a reconnection if no request had been made for 0.5 second? If you don't want it inside pymodbus, I will see with the Modbus integration of Home Assistant if they want to handle this at their level, and if not to write our integration with pyModbusTCP but this would be silly. |
As far as I know "do you want pymodbus to handle the case where a device need a reconnection if no request had been made for 0.5 second" is already handled....because if the device needs a reconnect it have closed the connection, which start the reconnect task. If your device do not close the connection, but silently demands a new connection, then it is something you should do in the app. "By adding an option allowing the app to ask a reconnection before each request" such an option do not make sense, the app should simply do a close/connect....in other words this is not an option that belongs in pymodbus. "option to specify the time limit between request before reconnecting", this is also something the app can handle a lot better than pymodbus. Good luck with the modbus integration, I am not sure there are any active maintainers. I maintained the integration during years, but decided to leave due to the demands from the core developers. Today I run a custom version, because 3.8.6 contains a number of important fixes. Finally pymodbusTCP is not this project, so I have no idea if that helps or not. |
just for the record, even if pymodbus got these options implemented, you would still need the modbus integration updated to the new version and have the options implemented in the yaml configuration. |
As I suspected, there are currently 40 open issues in HA mentioning modes....so at the very least it is not very actively maintained. A number of the issues seems to be because the integration was upgraded from pymodbus v3.7.4 to v3.8.3 without updating the code to use the features of the v3.8 line (at least as I remember the code). |
Closing, as this is an issue to be handled in the app. |
Hello,
Our electrical service provider deployed home-made electrical consumption monitoring devices (I don't know the hardware used). We abandoned their supervision tool and used Zabbix to get the values via modbus, without problems. Zabbix use Go code in its agent2 (https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/src/go/plugins/modbus).
We are testing Home Assistant to provide a more user-friendly interface to show the basic breakdown of power consumption. But Home Assistant only managed to get one value after each reboot.

I've tested with "KScada Modbus Doctor" and I get a similar behaviour: I only get values if I check "Reconnexion auto" to reconnect automatically before each reading.
I've seen the modbus "integration" of Home Assistant use the async clients of this pymodbus library (https://github.com/home-assistant/core/blob/dev/homeassistant/components/modbus/modbus.py).
So I've made a basic test:
modbus.py.txt
And I get this on a Debian Linux (with Python 3.11 and pymodbus 3.8.6):
and on Windows (with Python 3.13 and pymodbus 3.8.6):
I'm able to believe the modbus support of this home-made device is faulty. But why pymodbus is able to connect but not to reconnect?
If this can be resolved in pymodbus, the Home Assistant integration will be functional as-is. Otherwise, I will have to try to make them add an option to explicitly connect (and not reconnect) before each read, like Modbus Doctor.
Thank you for reading this far. :)
The text was updated successfully, but these errors were encountered: