1

I try to read data from a device by raspberry 3 B+.

those two devices are connected via CH340G usb module (USB to Serial (TTL) adapter). Concerning the python code; here's:

from pymodbus.client.sync import ModbusSerialClient as ModbusClient    
from pymodbus.register_read_message import ReadInputRegistersResponse  
client = ModbusClient(method='rtu', port='/dev/ttyUSB0', stopbits=1, bytesize=8, parity='N', baudrate='9600', timeout=0.3)  
connection=client.connect()
print(connection)  
value=client.read_input_registers(2301, 4, unit=0x01)  
print(value.registers)

And the error I get is:

Traceback (most recent call last):
  File "/home/iot/Desktop/test.py", line 3, in <module>
    client = ModbusClient(method='rtu', port='/dev/ttyUSB0', stopbits=1, bytesize=8, parity='N', baudrate='9600', timeout=0.3)
  File "/usr/local/lib/python3.6/dist-packages/pymodbus/client/sync.py", line 442, in __init__
    if self.baudrate > 19200:
TypeError: '>' not supported between instances of 'str' and 'int'
Ghanima
  • 15,958
  • 17
  • 65
  • 125
Brahim
  • 9
  • 2

1 Answers1

1

From the error message, specifically these two lines:

    if self.baudrate > 19200:
TypeError: '>' not supported between instances of 'str' and 'int'

I take it that baudrate should be an integer number not a string... and I am not judging the library for not typechecking and/or trying to convert a well-formed string such as this one (well, I probably do).

Solution: do not use baudrate='9600' but baudrate=9600, the full line thus reads:

client = ModbusClient(method='rtu', port='/dev/ttyUSB0', stopbits=1, bytesize=8, parity='N', baudrate=9600, timeout=0.3)  
Ghanima
  • 15,958
  • 17
  • 65
  • 125