I'm use "pymodbus" lib to connect PLC devices. The device is used Modbus RTU over TCP that devices will return the temperature and humidity of the environment.
map address list
- 0001: temperature
- 0002: humidity
I performed once to get value and it can succeed. But I'm using while loop sometimes get error. I don't know why.
code:
from time import sleep
from pymodbus.client.sync import ModbusTcpClient
from pymodbus.framer.rtu_framer import ModbusRtuFramer
from pymodbus.register_read_message import ReadHoldingRegistersResponse
client = ModbusTcpClient(host='192.168.1.1', port=5000, framer=ModbusRtuFramer)
client.connect()
while True:
rr = client.read_holding_registers(0, 2, unit=1)
if isinstance(rr, ReadHoldingRegistersResponse):
temp = rr.registers
print(temp)
else:
print('error')
sleep(1)
client.close()
output:
> ...
> [189, 444]
> [189, 443]
> [189]
> error
> error
> ...
We can see that sometimes the result is obtained normally, sometimes the result is incomplete, and sometimes the result is not available.
What should I do to solve this problem, I want to monitor this device. Thank you.
framer=ModbusRtuFramer
so perhaps the answer lies in what that does. Certainly for me, it's either a full list or no list (with an Exception thrown), but not a partial read. – roganjoshframer = ModbusRtuFramer
will get an error. – frankAttributeError: 'ModbusIOException' object has no attribute 'registers'
– frankModbusIOException
. Enable debug logs and see what exactly is happening over the socket . I would use a higher interval between polls in any case. – Sanju