Skip to content

Wiki

UART serial communication

About 8 min read

Existing English translations were reused. 3 current source blocks remain in Simplified Chinese, without triggering paid translation.

1.Linux Serial Communication

Serial communication is one of the most common low-level communication methods in robotics and embedded projects.

In actual projects, the host computer often needs to exchange data with devices such as STM32, ESP32, Arduino, sensor modules, and debug boards through a serial port. For example:

  • The host computer sends motor speed commands to the STM32
  • The STM32 returns encoder data to the host computer
  • Read sensor data such as IMU, GPS, and laser ranging modules
  • Use a USB-to-serial module for debugging
  • In ROS 2 chassis control, communicate with the lower-level controller

Serial communication is characterized by simplicity, versatility, and low cost, making it particularly suitable for point-to-point communication between a host computer and a microcontroller.

1.1.What is serial communication?

Serial communication usually refers to UART communication. On Linux, common USB-to-serial devices are recognized as device files similar to the following:

/dev/ttyUSB0
/dev/ttyUSB1
/dev/ttyACM0

The program only needs to open the corresponding device file and configure parameters such as baud rate, data bits, stop bits, and parity bits, and then it can send and receive data.

Common parameters include:

Parameter Description
Device name For example /dev/ttyUSB0、/dev/ttyACM0
Baud rate For example 115200, 921600
Data bits Commonly 8 bits
Stop bits Commonly 1 bit
Parity bit Commonly no parity
Flow control Hardware flow control is usually disabled in general projects

In robotics projects, the serial port typically does not just send ordinary strings; instead, a custom binary communication protocol is designed, such as frame header, length, command word, data field, CRC check, etc.

1.2.Common serial port libraries under Linux

There are multiple approaches for using serial communication under Linux, common ones include:

方案 类型 特点 适合场景
termios Linux 原生接口 最底层、依赖少、通用性强,但代码相对繁琐 想深入理解 Linux 串口机制
boost::asio 通用 C++ 库 支持同步/异步,代码结构较清晰,跨平台能力较好 推荐用于 C++ 工程封装
libserial 第三方串口库 使用简单,比 termios 更易上手 普通串口收发项目
serial_driver ROS 2 生态封装 更方便接入 ROS 2,但不适合只依赖 ROS 2 以外的项目 纯 ROS 2 项目或参考实现
pyserial Python 串口库 使用非常简单,适合快速测试 Python 调试脚本

These libraries are essentially all intended to accomplish the opening, configuration, reading, and writing of the serial port; they differ only in their level of encapsulation.

1.3.Comparison of Schemes

Solution Advantages Disadvantages
termios Native to Linux, minimal dependencies, strong low-level capabilities Cumbersome configuration, beginners prone to errors
boost::asio C++ engineering-friendly, convenient asynchronous communication, suitable for encapsulating drivers Requires understanding the basic asio model
libserial Simple and easy to use, quick to get started Flexibility and engineering controllability are inferior to custom encapsulation
serial_driver Convenient integration with the ROS 2 ecosystem Poor reusability outside of ROS 2
pyserial Fast debugging, suitable for scripting Not suitable as the main line for high-performance C++ low-level drivers

1.4.This tutorial recommends selecting

This tutorial recommends prioritizing:

boost::asio

The reason is:

  • More suitable than directly using termios for C++ engineering development.
  • It can be encapsulated into a regular C++ driver for easy reuse.
  • It does not depend on ROS 2, so OpenCV, Qt, and ordinary C++ projects can all use it.
  • It can also be naturally integrated later with ros2_control hardware_interface
  • which makes it quite suitable for robot host computer and STM32 communication scenarios.

The recommended engineering structure is:

上层项目:ROS 2 / OpenCV / Qt / 普通 C++ 程序
        ↓
自己封装的 SerialDriver 类
        ↓
boost::asio
        ↓
/dev/ttyUSB0
        ↓
STM32 / ESP32 / 传感器

1.5.Relationship with ROS 2

Learning Linux serial communication does not mean that you do not need ROS 2.

在 ROS 2 项目中,可以将串口通信封装成一个普通 C++ 类,然后在 ROS 2 节点或 ros2_control hardware_interface 中调用它。

For example:

ros2_control controller
        ↓
hardware_interface
        ↓
自己封装的 SerialDriver
        ↓
boost::asio
        ↓
STM32

The advantage of doing this is that the serial communication code will not be tightly bound to ROS 2. In the future, even when writing OpenCV projects, Qt host computer applications, or ordinary C++ debugging programs, the same serial driver can continue to be reused.

1.6.Chapter Learning Objectives

After completing this chapter, you should be able to master:

  • Linux 下串口设备的基本概念
  • 如何查看和打开 /dev/ttyUSB0 等串口设备
  • 串口波特率、数据位、停止位、校验位等基本参数
  • 使用 C++ 进行串口数据收发
  • 设计简单的上位机与下位机通信协议
  • 将串口通信封装成可复用的 C++ 类
  • 为后续接入 ROS 2、OpenCV、Qt 项目打基础