I have been trying to read data from a modbus slave device using pymodbus. After much research I found this code on internet. It is a straight forward code. I am able to read the data on windows but RPi returns "none". There have been similar questions on this site but none of them seem to have been answered.
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
#def checkProxCounter():
count = 0
result = 0
#UART.setup("UART1")
client = ModbusClient(method='rtu',port='/dev/ttyUSB1',parity= 'E',stopbits=1,bytesize=8,baudrate=19200,timeout=3)
try:
if client.connect():
print ("Port open")
result = client.read_holding_registers(address=3909, count=2,unit=1)
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