Learn How to handle PySerial Exceptions in Python while communicating with an Arduino from PC

2 min readOct 17, 2022

Tutorial on connecting an Arduino or Microcontroller with a Linux/Windows PC using serial port (VCP). and communicating with it using Python and PySerial library.

This tutorial will concentrate on Python 3.x.x language and will use Pyserial 3.4 Library.

Original article -> Serial Port Programming using Python 3.0 and Pyserial

Some times opening the serial port may fail due to various conditions like port in use ,port not found etc .

PySerial comes with the exception serial.SerialException which can be used to identify the error and fail gracefully.

The below partial code shows how to use exceptions

SerialObj = serial.Serial(‘COM11’,9600) # open the Serial Port

except serial.SerialException as var : # var contains details of issue
print(‘An Exception Occured’)
print(‘Exception Details-> ‘, var)

print(‘Serial Port Opened’)

Here is an example of the error message you will get

when some other process is using the port you are trying to access.

When Port number is wrong.