I have been trying to use pymodbus to read registers from a power meter and searching around here I found a useful code from the question I attach in the end of this post. So I wrote/copied the next code:
import pymodbus
import serial
from pymodbus.pdu import ModbusRequest
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
#initialize a serial RTU client instance
from pymodbus.transaction import ModbusRtuFramer
#def checkProxCounter():
count = 0
result = 0
#UART.setup("UART1")
client = ModbusClient(method='rtu',port='/dev/ttyUSB0',parity='N',stopbits=1,bytesize=8,baudrate=9600,timeout=3)
try:
if client.connect():
print ("Port open")
result = client.read_holding_registers(address=0x00, count=2,unit=0x01)
print ("Result : ")
print (result)
blah = client.read_discrete_inputs(1,8)
if blah != None:
print("{}: {}".format("Blah", blah.bits[0]))
if result != None:
count = int(str(result.registers[0]),16) + int(str(result.registers[1]),16)
print("{}: {}".format("Count", count))
else:
print("results were none")
client.close()
else:
print("Port failed to open")
count = -2
except:
print("Unknown Exception")
raise
print count
When I try to run it the next Output appears:
Port Open
Result:
ReadRegisterResponse(2)
Unknown Exception
Traceback(most recent call last):
File"prueba2.py", line 30, in <module>
count = int(str(result.registers[0]),16) + int(str(result.registers[1]),16)
AttributeError:'ReadHoldingRegistersResponse' object has no attribute 'register'
Obviously there is something wrong or missing here but I am still a beginner programming and I don't see it. I understand that the variable "count" is changing the register to an integer. And to be honest I don't know what is exactly doing the variable "blah" nor the code printed as "{}: {}".format( ) .
Any advice or way to fix this problem?