How to handle the error "failed to connect serial modbus client"

27 views Asked by At

I am using the pymodbus library to read the holding register. After connecting the modbusserialclient i can successfully read the registers. But problem arrises when the UART is disconnected from the port. So after reading the data t gives an error ""AttributeError: 'NoneType' object has no attribute 'inWaiting'"" for the firast time & ""pymodbus.exceptions.ConnectionException: Modbus Error: [Connection] Failed to connect[ModbusSerialClient(<pymodbus.framer.rtu_framer.ModbusRtuFramer object at 0x000001984E275E50> baud[19200])]" for the second read.

How to handle this error!

def read_string_register_uint_port():
  read_value_uint = modbus_client_obj.read_holding_registers(40036, count=1, slave=1)
  if read_value_uint.isError():
    ip_port_var.set("")
    print_to_logs("WITHOUT_TIME", "No IP Port Received", "ERROR")
  try:
    data_value = (BinaryPayloadDecoder.fromRegisters(read_value_uint.registers, Endian.BIG, Endian.BIG))
    decoded = data_value.decode_16bit_uint()
    ip_port_var.set(decoded)
    print_to_logs("WITH_TIME", "IP Port Data Received", "")
    
  except ModbusIOException as e:
    print("Modbus IO error:", e)
  except AttributeError as e:
    print("Attribute error:", e)

Tried using try except function to handle th error but didn't work. I just want to display error msg when the comport is disconnected while reading registers.

0

There are 0 answers