为什么ESP32上的UART通信返回随机数据?

问题描述 投票:0回答:1

我正在使用 UART echo exmaple 来测试 ESP32 和树莓派 4 之间的 UART 通信。

修改后的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'

我记得以前可以用,但现在就这样卡住了。

首先我尝试使用 ESP CAM 与 UART 进行通信,然后尝试使用 ESP32-WROOM。他们俩都做了同样的事情。我也尝试过使用 UART_NUM_1。

更新:

经过一些调试,我发现当我发送 16 个字节时,ESP32 收到了 40 个字节。是不是 UART 坏了?

embedded esp32 uart
1个回答
0
投票

我将两块板插在一起并测试了 uart,它工作了。问题出在 rpi 上

© www.soinside.com 2019 - 2024. All rights reserved.