This is a follow up question to Pyserial: How to know if a serial port is free before open it .
The post correctly says that:
try:
ser = serial.Serial(port=portName)
except SerialException:
print 'port already open'
will catch an exception generated when a script attempts to open a COM port that is open in another application (ie PuTTY).
My question is how, given the output, should I know that I need to use SerialException. I initially was trying except PermissionError
and that did not catch the exception. What makes it obvious that I needed to catch SerialException
instead of PermissionError
?
File "C:\ProgramData\Anaconda3\lib\site-packages\serial\serialwin32.py", line 62, in open
raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM6': PermissionError(13, 'Access is denied.', None, 5)