ESP32与树莓派4 UART通信返回随机数据问题咨询
ESP32与树莓派4 UART通信异常排查
问题背景
基于UART回显示例测试ESP32与树莓派4的UART通信,此前能正常工作,现在出现数据接收异常:树莓派发送32字节随机数据,仅收到21字节且内容不符;后续调试发现发送16字节时,ESP32收到40字节。先后更换ESP CAM、ESP32-WROOM模块,切换至UART_NUM_1后问题依旧。
ESP32 修改后代码
/* UART Echo Example This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ #include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/uart.h" #include "driver/gpio.h" #include "sdkconfig.h" #include "esp_log.h" /** * This is an example which echos any data it receives on configured UART back to the sender, * with hardware flow control turned off. It does not use UART driver event queue. * * - Port: configured UART * - Receive (Rx) buffer: on * - Transmit (Tx) buffer: off * - Flow control: off * - Event queue: off * - Pin assignment: see defines below (See Kconfig) */ #define ECHO_TEST_TXD (CONFIG_EXAMPLE_UART_TXD) #define ECHO_TEST_RXD (CONFIG_EXAMPLE_UART_RXD) #define ECHO_TEST_RTS (UART_PIN_NO_CHANGE) #define ECHO_TEST_CTS (UART_PIN_NO_CHANGE) #define ECHO_UART_PORT_NUM (UART_NUM_1) #define ECHO_UART_BAUD_RATE (CONFIG_EXAMPLE_UART_BAUD_RATE) #define ECHO_TASK_STACK_SIZE (CONFIG_EXAMPLE_TASK_STACK_SIZE) static const char *TAG = "UART TEST"; #define BUF_SIZE (1024) static void echo_task(void *arg) { /* Configure parameters of an UART driver, * communication pins and install the driver */ uart_config_t uart_config = { .baud_rate = 19200, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, .source_clk = UART_SCLK_DEFAULT, }; int intr_alloc_flags = 0; #if CONFIG_UART_ISR_IN_IRAM intr_alloc_flags = ESP_INTR_FLAG_IRAM; #endif ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags)); ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config)); ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, 2, 16, ECHO_TEST_RTS, ECHO_TEST_CTS)); // Configure a temporary buffer for the incoming data uint8_t *data = (uint8_t *) malloc(BUF_SIZE); while (1) { // Read data from the UART int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, (BUF_SIZE - 1), 2000 / portTICK_PERIOD_MS); // Write data back to the UART uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) data, len); if (len) { data[len] = '\0'; ESP_LOGI(TAG, "Recv str: %s", (char *) data); } } } void app_main(void) { xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL); }
树莓派代码
import serial import time import os ser = serial.Serial("/dev/ttyS0", baudrate=19200, timeout=5) while True: time.sleep(1) by = os.urandom(32) ser.write(by) rx = ser.read(32) print(rx) print(len(rx)) print(by)
树莓派输出
#Output from UART b"RE\x14w\xd9Ac2G@1\x13V'\xccuM!\xd5w\x90" #Length of output 21 #Original data (32 bytes) b'\xaa\xc5\x89w\xcfc\x8c\xbc\x9cN\xcbGB/\xb3m\xd2\xae\xd3*\xa3\xae_\x83\xf1I\xfd\xed\xf7\x0f\xf9'
排查建议
- 物理连接检查:确认树莓派Tx接ESP32 Rx、树莓派Rx接ESP32 Tx,接线无虚焊、松动,且双方共地可靠
- 通信参数一致性:再次核对双方波特率、数据位、停止位、奇偶校验设置是否完全匹配(当前均为19200、8位数据位、1停止位、无校验)
- 树莓派串口配置:通过
raspi-config关闭Serial Console(保留Serial Port),确保/dev/ttyS0未被其他进程占用 - 干扰排查:尝试降低波特率测试,更换屏蔽线,远离电源、WiFi模块等干扰源
- 代码逻辑优化:ESP32端缩短
uart_read_bytes的超时时间,避免缓冲区累积旧数据;树莓派端改为循环读取直到收满指定字节数,避免因超时提前返回 - 单端硬件验证:用USB转TTL模块分别测试ESP32和树莓派的UART功能,排除单端硬件故障
内容的提问来源于stack exchange,提问作者Low Est
相关产品推荐
相关产品推荐

