Introduction
Serial port communication remains essential in industrial, IoT, robotics, and microcontroller systems. Controlling devices via COM (RS-232/RS-485/USB-Serial) requires a stable, cross-platform, and flexible tool. In Python, the go-to solution is the PySerial library.
PySerial provides a simple yet powerful interface for connecting, configuring, and transferring data over serial lines. It's widely used to communicate with Arduino, Raspberry Pi, modems, GPS receivers, controllers, sensors, and many other devices.
About PySerial
PySerial is a cross-platform Python library for working with serial ports. Created by Chris Liechti, it has become the de facto standard for serial communication in the Python ecosystem. The library supports Windows, Linux, macOS, and many other operating systems.
Key Features of PySerial
The library provides a unified API for working with various types of serial connections. It automatically handles platform-specific port operations and gives developers a clean, straightforward interface.
PySerial supports not only physical COM ports but also virtual connections, including USB-to-Serial adapters, Bluetooth Serial Port Profile, and even network connections via the RFC2217 protocol.
Installation and Setup
Install the library using the pip package manager:
pip install pyserial
Import the library in your Python script with a standard import statement:
import serial
Basic Workflow Architecture
Connection Lifecycle
Typical PySerial usage follows a simple pattern:
- Create a Serial object — initialize with port parameters
- Open the connection — establish communication with the device
- Exchange data — send commands and read responses
- Close the connection — properly terminate the session
ser = serial.Serial('COM3', baudrate=9600, timeout=1)
ser.write(b'command')
data = ser.readline()
ser.close()
Key Configuration Parameters
When creating a Serial object, you can set many parameters:
- port — device name (COM1, /dev/ttyUSB0, /dev/ttyAMA0)
- baudrate — data transfer speed (typically 9600, 38400, 115200)
- bytesize — character size in bits (5, 6, 7, 8)
- parity — parity checking (PARITY_NONE, PARITY_EVEN, PARITY_ODD)
- stopbits — number of stop bits (1, 1.5, 2)
- timeout — read operation timeout in seconds
Reading Data Methods
Basic Reading Methods
PySerial offers several ways to read data from the port:
Serial.read(size=1) — reads exactly the specified number of bytes. If not enough data is available, the method blocks until all bytes are received or the timeout expires.
data = ser.read(10) # Read 10 bytes
Serial.readline() — reads data until a newline character (\n). This is the most popular method for text-based protocols.
line = ser.readline().decode('utf-8').strip()
Serial.read_all() — reads all available data from the input buffer immediately.
all_data = ser.read_all()
Advanced Reading Methods
Serial.read_until(expected) — reads data until a specified delimiter is encountered.
Also in library
Rate Limiting in TypeScript: Libraries, Tools, and Implementation Examples
HTTP Client for C# (.NET): Top Libraries for Working with Web Requests