1

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)
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
  • 1
    The relevant documentation? – John Coleman Dec 04 '17 at 14:42
  • 3
    The [documentation](https://pythonhosted.org/pyserial/pyserial_api.html#serial.Serial) -- "SerialException – In case the device can not be found or can not be configured." – khelwood Dec 04 '17 at 14:43
  • Why dont you just catch common exception and then check it in finally block or in catch block only? – Kishan Bheemajiyani Dec 04 '17 at 14:44
  • 1
    I understand that SerialException is covered in the documentation. This is more of a general question about interpreting output. I saw PermissionError in the output and found that it was a built-in exception type and so tried to catch that. My question should have been "Should I have been able to tell from the output that I needed to catch SerialException instead of PermissionError. Or, should I have gone straight to the documentation? – Steve Boege Dec 05 '17 at 16:19

0 Answers0