Python/Serial port/pySerial

= Objective = This tutorial will guide you to work with the serial port on your PC by using pySerial library. This tutorial is specifically for anyone working with devices, i.e microcontrollers, PLC,... to communicate with PC via USB port, and intend to use a third party library to handle those connection. The Python programming skills required for this tutorial is very basic.

Hobbyist, Python starter and inexperienced developers who wish to communicate their PC with microcontroller boards, like Arduino Uno R3, STM32 dev-kit, Tiva C dev-kit,... via USB port may find this course useful.

This tutorial provides you the basic knowledge of simple operations on serial port, include:


 * View serial port information
 * Open and close the serial ports
 * Read operation: Receive data sent to the serial port
 * Write operation: Send data to the serial port

= Introduction = pySerial is a third party Python library to support working with the Serial port. PySerial is compatible with Windows, macOS, Linux, BSD and IronPython.

pySerial is a famous library, whose official Github named pyserial has more than 3.1k stars till July 2024.

To install pyserial: = View serial port information = In your PC, plug in your micro-controller board or dev-kit which supports the UART-to-USB converter, e.g an Arduino Uno R3, to your USB port. On your Windows PC, if all drivers are well setup, it will appear as COM3. In Linux PC, like Ubuntun, it will appear as /dev/ttyUSB0.

Print out the information of that serial device
The result will be like:

In the program above, the serial port at  is opened with the baudrate of 9600 bit/s. Also note that this program only works if the serial port at  is plugged in at the time the program starts. Please change the serial port to your port name.

List of serial ports
With multiple serial ports plugged into your PC, to get that list: The result will be like: To get their specific information: The result will be like: To get the serial port name only: The result will be like:

= Open and close the serial ports =

Open the serial port Close the serial port immediately

= Receive data sent to the serial port = PC, as the receiver, receives the data sent from a device as the transmitter, e.g an Arduino Uno R3, to the PC's serial port: Suppose that the data sent from the device is string "Hello, World !" for an interval of time inside an infinite loop, the received data will be: The "b" appears as pySerial receives those value of bytes data type. To "parse" the received string, i.e to remove the "b": The result will then be:

= Send data to the serial port = PC, as the transmitter, sends data to a device as a receiver, e.g an Arduino Uno R3, via the serial port: From the receiver side which has setup its baudrate to be 9600, it will receive the string "Hello, World !" sent from the PC.

= Assignment =

Two-way communication between master and slave
The following master program (which runs on your PC) will wait until the slave sends a character "R" to it. After receiving the "R" character, it will start sending a string with a count up value to the slave Your assignment is to write the slave program in your slave device to communicate with the master. Your slave device can be microcontroller like Arduino - ATmega family, STM family, Tiva C,... or single board computer like Raspberry Pi, Beagle Bone,...

Python serial port communication without pySerial library
A follow up question for developers and students who want to deep into serial communication is to write your own program using only the Python built-in API for serial port communication. This will include all the operations we have gone through above, especially the serial port communication between the PC and the external devices via USB port.

Those who don't want to go deep into low-level programming with Python can skip this assignment.

= Conclusion = Congratulation ! You have reached the end of the tutorial. The tutorial is very basic for hobbyist and inexperience developers who will gain enough basic knowledge with pyserial to work on their hobbyist projects. Hobbyist will need to investigate more on the pyserial API based on their project features. Anyone who works on the serial communication between PC and microcontrollers or single board computer are required to complete the Two-way communication between master and slave assignment as this is an important problem met in almost every communication system.

= External links =
 * pyserial Github repository
 * pySerial document
 * pyserial on PyPI