How can I close a Serial port if its open before running python script?

Viewed 203

I was hoping to find out how to check if there are any serial ports opened before they are established and if so then close them

comlist= serial.tools.list_ports.comports()
for i in comlist:
    connected.append(i.device)
if i.device == isOpen()
    i.device.close

where i.device is the serial port being identified because if I use

    try:
        ser = serial.Serial(port = i.device)
        if ser.isOpen:
            ser.close()
    except
        pass

because then the serial I want to close cannot be accessed as ser, it fails because ser = serial.Serial(port = i.device) will raise a PermissionError. Let me know if I can explain this further.

0 Answers
Related