1

I am trying to build an automatic recovery if USB is disconnected or changed after reboot, and tried to built error handling, and successed here in startup

But then if the connection break during operation I cannot get over this cycle of exceptions. Why am I doing this? If connection lost it gets into loop reading databases and trying to send send values to Pico loading CPU over 80% and heating up heavily.

Runtime error

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 603, in write
    n = os.write(self.fd, d)
OSError: [Errno 5] Input/output error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/IsmoCodes/USBwrite_to_PicoTeho_v8.2.8_ACM1.py", line 155, in main
    ser.write(kwh_teho_str.encode('utf-8'))
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 637, in write
    raise SerialException('write failed: {}'.format(e))
serial.serialutil.SerialException: write failed: [Errno 5] Input/output error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/IsmoCodes/USBwrite_to_PicoTeho_v8.2.8_ACM1.py", line 294, in <module>
    main()
  File "/home/pi/IsmoCodes/USBwrite_to_PicoTeho_v8.2.8_ACM1.py", line 222, in main
    except 'Input/output error':
TypeError: catching classes that do not inherit from BaseException is not allowed

Code sample

except 'Input/output error':
    try:
        print("connection break")
        time.sleep(6)
        continue
    except serial.serialutil.SerialException:
        try:
            print("serial error")

        except 'Input/output error':
            print("nested2 error")
            print("connection2 break")
            time.sleep(6)
            continue

OK I managed to get over, rather than caring what specific error there comes, I did a simple single handling for any error. If connection breaks, it tries to toggle between portsm and once connected back it runs smoothly

except Exception as e:
    print("Exception: " + str(e))
    print("Try to switch port, old was : ",port)
    if port ==  '/dev/ttyACM1':
        port = '/dev/ttyACM2'
    else:
        port = '/dev/ttyACM1'
    print("New port ", port)
    Globaali_paalla = 0
    USB_on = 0
    
    time.sleep(60)
    continue

0 Answers0