1

I have a device connected to the computer's COM port and communicating with it via the Modbus protocol. Up to 40 of the same slave devices can be connected to this unit. How to find all slave devices using Pymodbus? I know that in order to understand whether I found the right type of device at a certain address, I need to make a request to register 976 and if it answers with the number 60, then everything is correct.

I do it like this:

from pymodbus.client.sync import ModbusSerialClient as ModbusClient


def get_slaves(port):
    client = ModbusClient(method='rtu', port=port, stopbits=2, bytesize=8, parity='N', baudrate=19200)
    client.connect()
    slaves = []
    for i in range(1, 250):
        try:
            response  = client.read_holding_registers(address=976, count=1, unit=i)
            registers_values = response.registers
            print(registers_values)
            # if registers_values[0] == 60:
            slaves.append(i)
            print(slaves)
        except:
            print('ошибка')
            continue
    print(slaves)

That is, because a master device can have up to 250 slave devices, I go through all possible addresses using the unit parameter of the read_holding_registers function. Such a survey takes quite a lot of time, because. you have to wait about 1 second if there is no device at all at the address. Is it possible to somehow find out in advance at what addresses the slave devices are connected?

Etozeigor
  • 47
  • 5

0 Answers0