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'
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)  
  • @BrahimAssad please take the tour and visit the helpcenter to see how things work here. Feel free to express your thanks by upvoting and/or accepting the answer in due time if it solves the issue. – Ghanima May 1 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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