0

I'm using ModbusSerialClient of pymodbus library. I've multiple registers that I want to read.

I'm able to read one register at a time, but not able to figure out how to read all the registers at a single time for better performance.

registers = [3900,3902,3904,3906,3926,3860]
client = ModbusSerialClient(method='rtu',port='/dev/ttyUSB0', timeout=1, parity='N', baudrate=19200, unit=1)
client.connect()

for register in registers:
    response = client.read_holding_registers(register, 2, unit=0x01)

I've tried something like this

add_min, add_max = min(registers), max(registers)
no_bytes = add_max - add_min
response = client.read_holding_registers(add_min, no_bytes, unit=0x01)

But it's throwing "Illegal Address" error.

3
  • 1
    You can read at most 125 registers (as per modbus specs. Note 1 register == 2 bytes) If you are trying to read more than 125 registers pymodbus would throw InvalidAddress exception. Make sure 1< no_bytes<=125.
    – Sanju
    Commented May 22, 2018 at 15:42
  • You are assuming that all registers between 3860 and 3926 exists. If at least one of them is not mapped in the device an invalid address exception is returned.
    – Ronaldo
    Commented May 23, 2018 at 16:21
  • Try this: no_bytes = (add_max - add_min) + 2 instead of no_bytes = add_max - add_min, because in the maximum registers you need also to read two count. And consider maximum count of reading that not be more that 125 in each request. Commented Oct 12, 2018 at 22:45

0

Browse other questions tagged or ask your own question.