[Python] How to read from Registers with pymodbus

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    how can I read from Register with pymodbus?

    I'm trying this piece of code:

    import pymodbus
    from pymodbus.pdu import ModbusRequest
    from pymodbus.client.sync import ModbusSerialClient as ModbusClient
    #initialize a serial RTU client instance
    from pymodbus.transaction import ModbusRtuFramer

    #count= the number of registers to read
    #unit= the slave unit this request is targeting
    #address= the starting address to read from

    client = ModbusClient(method = 'rtu', port='/dev/extcomm/0/0', stopbits = 1, bytesize = 8, parity = 'N' , baudrate= 38400)

    #Connect to the serial modbus server
    connection = client.connect()
    print(connection)

    #Starting add, num of reg to read, slave unit.
    coil = client.read_holding_registers(0x01,1,unit=1)# address, count,
    slave address
    print(coil)

    #Closes the underlying socket connection
    client.close()


    This gave me this error:

    Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)


    My slave id is number 1 and the register I want to read is also the number 1, but i don't know why is still doesn't want to work.

    Continue reading...

Compartilhe esta Página