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
ModbusTcpClient - Second Request always fails #612
Comments
To add more information for debugging. The issue is not present in 2.4.0 version. Code: try:
client = ModbusClient(self.args['modbus_host'], port=self.args['modbus_port'], timeout= 20)
client.connect()
self.log('connected')
self.log('read input')
input_registers = client.read_input_registers(address=0, count=5)
self.log(input_registers.registers)
# self.log(input_registers.registers)
# client.close()
# self.log('reset')
# rq = ClearCountersRequest()
# rr = client.execute(rq)
# self.log(rr)
self.log('read holding')
# client = ModbusClient(self.args['modbus_host'], port=self.args['modbus_port'])
# client.connect()
holding_registers = client.read_holding_registers(address=0, count=3)
self.log(holding_registers.registers)
client.close()
# self.mqtt_publish("homeassistant/bedroom/light", "ON")
except Exception as inst:
self.error('Modbus error {}'.format(inst))
rq = ReadExceptionStatusRequest(unit=255)
rr = client.execute(rq)
self.log(rr)
self.log(rr.isError()) Logs:
So this is something within 2.5.0 version. Here's the log for the same code in 2.5.0:
|
@vpetrovych Can you call the requests in a loop and share the logs please? I am not able to reproduce this on my local setup. |
Just for your reference
|
Same problem here reported on stackoverflow. Code: import pymodbus
from pymodbus.client.sync import ModbusTcpClient
from time import sleep
import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s '
'%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)
connection = False
data = {}
while True:
if not connection:
client = ModbusTcpClient(host="176.219.185.16", port="502")
connection = client.connect()
if connection:
print("connected")
else:
if not client.is_socket_open():
print("connection is broken")
connection = False
data = client.read_holding_registers(address=150,count=10,unit=1).registers
sleep(2)
print(data) pymodbus 2.5.0 LOG
pymodbus 2.4.0 LOG
|
@aetasoul and @vpetrovych Refer #614 . This is a basically because |
Has you can see in the code there is a sleep of two seconds: while True:
if not connection:
client = ModbusTcpClient(host="176.219.185.16", port="502")
connection = client.connect()
if connection:
print("connected")
else:
if not client.is_socket_open():
print("connection is broken")
connection = False
data = client.read_holding_registers(address=150,count=10,unit=1).registers
sleep(2)
print(data) Just to be sure that the problem is not here, I add infinite sleep in the code: import pymodbus
from pymodbus.client.sync import ModbusTcpClient
from time import sleep
import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s '
'%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)
connection = False
data = {}
while True:
sleep(1)
if not connection:
sleep(1)
client = ModbusTcpClient(host="176.219.185.16", port="502")
sleep(1)
connection = client.connect()
sleep(1)
if connection:
sleep(1)
print("connected")
sleep(1)
else:
sleep(1)
if not client.is_socket_open():
sleep(1)
print("connection is broken")
sleep(1)
connection = False
sleep(2)
data = client.read_holding_registers(address=150,count=10,unit=1).registers
sleep(2)
print(data) LOG:
|
@aetasoul can you share the specs for your modbus slave? Also with pymodbus 2.4.0, if you explictly close the connection after each read what is the behaviour ? can you share the logs please ?
|
Specs: let me check if I can find it With pymodbus 2.4.0 if I close the connection after the read the problem is the same as the 2.5.0. But it seems to not be a delay issue, I set the sleep to 10 seconds data = client.read_holding_registers(address=150,count=10,unit=1).registers
client.close()
sleep(10) LOG:
|
@aetasoul Thanks for the experiments, looks like the slave doesn't like the disconnections. Let me make the disconnects optional after a succesfull read. You can expect 2.5.1rc1 in sometime with a patch. |
@aetasoul @vpetrovych |
Works like a charm 👍 I tried to remove the sleep and works also in this case 👍 Thank you for your help @dhoomakethu !! |
Versions
Pymodbus Specific
Description
What were you trying, what has happened, what went wrong, and what did you expect?
When trying to send 2nd request, an error is returned
[Connection] ModbusTcpClient(192.168.0.15:502): Connection unexpectedly closed 0.000886 seconds into read of 8 bytes without response from unit before it closed connection
It doesn't matter if I read first input and second holding, or vice versa. It always fails the second request.
Removing the ClearCountersRequest also doesn't help
This worked correctly in Node.JS using jsmodbus package, so I have a feeling there could be a bug here.
Code and Logs
logs
The text was updated successfully, but these errors were encountered: