Welcome to MicroROS V2 Robot Car repository
6. Serial Communication
Serial Communication
Experiment Objective
This section will guide you through the basic usage of UART serial communication on the MicroROS-V2 control board. Through this tutorial, you will learn how to initialize UART1 and UART2, configure parameters such as baud rate, data bits, and stop bits, use a ring buffer to cache received data, and implement serial data send/receive echo functionality.
Experiment Principle
1. Keywords
| Keyword | Description |
|---|---|
| UART | Universal Asynchronous Receiver/Transmitter, implements serial communication between devices through TX/RX lines, full-duplex operation |
| Baud Rate | The serial communication rate, representing the number of symbols transmitted per second; common values include 9600 and 115200 |
| Data Bits | The number of data bits in each data frame, usually 8 bits |
| Parity | A redundant bit used for error checking; options are none / odd / even parity |
| Stop Bits | The bit(s) that mark the end of a data frame, usually 1 bit |
| Ring Buffer | A fixed-size circular queue used to cache and read serial received data, preventing data loss |
2. Technical Architecture
The program uses the UART peripheral driver of the ESP32-S3 to initialize two serial ports: UART1 (GPIO17 TXD, GPIO18 RXD) and UART2 (GPIO36 TXD, GPIO35 RXD), each independently configured with a baud rate of 115200, 8 data bits, no parity, and 1 stop bit. Each serial port has an independent FreeRTOS task responsible for receiving data, and the data is cached through a ring buffer. The main loop polls the buffer status of both serial ports; when data is available, it reads it byte by byte and sends it back over the same line (loopback), implementing the send/receive echo function.
Main component files:
main/main.c— main program entry, initializes the serial ports and implements the echo logiccomponents/uart/uart1.c/uart1.h— UART1 driver implementation and interface declarationscomponents/uart/uart2.c/uart2.h— UART2 driver implementation and interface declarationscomponents/ring_buffer/ring_buffer.c/ring_buffer.h— ring buffer implementation
Experiment Steps
1. Hardware Preparation
| Hardware | Requirement |
|---|---|
| MicroROS-V2 control board | ✅ Required |
| USB data cable | ✅ Required (for power supply and serial monitor) |
| USB-to-TTL serial module | ✅ Required (for connecting UART1 or UART2 to test send/receive) |
| Jumper wires (female-to-female) | ✅ Required (to connect the serial module to the control board) |
2. Prerequisites
ESP32-IDF development environment set up
MicroROS-V2 control board connected to the PC via USB
Prepare a USB-to-TTL serial module (such as CH340, CP2102, etc.)
Hardware wiring
Connect the USB-to-TTL serial module to the UART1 or UART2 pins of the control board:
| USB-to-TTL | MicroROS-V2 control board |
|---|---|
| TXD | UART1_RXD (GPIO18) or UART2_RXD (GPIO35) |
| RXD | UART1_TXD (GPIO17) or UART2_TXD (GPIO36) |
| GND | GND |
3. Compile and Flash
x
cd ~/esp/base_samples/uartsource ~/esp/esp-idf/export.shidf.py build flash monitor
This example initializes both UART1 and UART2 at the same time, and the two serial ports work independently. After the program starts, you can see the receive task startup logs of UART1 and UART2 in the serial monitor. When data is sent to UART1 or UART2, the program sends the received data back unchanged.
4. How to Stop
Press Ctrl+] in the terminal to exit the serial monitor.
Experiment Result
After correctly flashing and running the program, the serial monitor outputs the following startup information:
xxxxxxxxxx