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
4
  • What modbus adapter are you using with your Pi? Jan 2, 2016 at 5:02
  • @jwygralak67 I am using "qinheng electronics hl-340 usb-serial adapter" Jan 2, 2016 at 5:23
  • Is this the same adapter you used successfully with windows? Did you run the same python program on windows, or did you use a different software? Jan 2, 2016 at 5:41
  • @jwygralak67 The adapter is the same that I used on Windows but, I used visual studio and nmodbus library there Jan 2, 2016 at 6:23

2 Answers 2

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?

1
  • I think parity and stop bits need to match with the settings of the client device. It seems that the original FTDI chip driven modbus converters work well with Linux. I couldn't find any driver for HL-340 that worked on Linux. Sep 17, 2016 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.