I am trying to communicate a Modbus slave device that uses Modbus RTU protocol, with RPi having pymodbus lib in python 3.8. Here's a link for this library http://riptideio.github.io/pymodbus/
My concern is when printing holding registers responses. I have read other posts regarding it and people have suggested using '.registers' with the print command.
How to read from Registers with pymodbus
At times, I get the value printed out but other times I see this error
Traceback (most recent call last):
File "/home/user123/VsCode/Modbus_Rtu_sample.py", line 65, in <module>
value = result.registers
AttributeError: 'ModbusIOException' object has no attribute 'registers'
Firstly, I want to know why does this works sometimes and not other times. as I can see in the pymodbus documentation, there's no attribute called registers then how does it work the first time? this is my code:
with ModbusClient(method='rtu', port='/dev/ttyUSB0', stopbits=1, bytesize=8 , parity='N', baudrate=9600, timeout=2) as client:
if not client.connect():
connection = client.connect()
connection = client.connect()
print("Your connection is set to",connection)
if client.connect():
read=client.read_holding_registers(address = 0x1000,count =4,unit=1)
value = read.registers
print(value)
#print(read.registers)
else:
print("Cannot connect to modbus slave")
client.close()
Is there any better lib/method/way to overcome this issue? all I want is to have stable read / write outputs for registers. later I want to call a class made from this so don't want to have a situation where sometimes I do get output and other times an error is thrown.