1

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

2 Answers2

0

Okay so turns out that the adapter "QinHeng Electronics HL-340 usb-serial" does not work with Linux. I bought a new adapter with an original FTDI chip and all the codes worked fine.

0

I had this problem too. I found, that HL-340 with pymodbus on Pi sent data with parity NONE, even with EVEN configuration. When I changed parity to NONE, it worked.

#UART.setup("UART1")
client = ModbusClient(method='rtu',port='/dev/ttyUSB1',parity= 'N',stopbits=1,bytesize=8,baudrate=19200,timeout=3)

Bug in HL-340 driver?