[Python] pymodbus: ID mismatch exception

Discussão em 'Python' iniciado por Stack, Junho 2, 2025.

  1. Stack

    Stack Membro Participativo

    The device that I'm using has a command that allows the device ID to be identified. To send the command, I need to send a message to the device address 250 and the incoming message will include the device address (ID = 1 in this case).

    • Outgoing Message: FA | 03 | 00 66 | 00 01 | 71 93
    • Incoming Message: 01 | 03 | 02 | 00 01 | 79 84

    The result is as expected, but Pymodbus sees this as a ID mismatch and throws an error. Does anybody know how to create an exception for this scenario whenever I attempt to read the register?

    #Functions
    def is_unit_id_mistmatch_error(exc: ModbusIOException, expected_id: int, actual_id: int) -> bool:
    return (
    "Unit ID mismatch" in str(exc)
    and f"expected {expected_id}" in str(exc)
    and f"got {actual_id}" in str(exc)
    )

    #Reading Initial Address - Issue with pymodbus strict id checking
    try:
    read_address = client.read_holding_registers(address=66, count=1, slave=250)
    except ModbusIOException as exc:
    if is_unit_id_mistmatch_error(exc, expected_id=250, actual_id=1):
    print("Ignoring expected unit ID mismatch 250 -> 1... Continuing")

    Continue reading...

Compartilhe esta Página