ESP32 外设-串口接收

好记性不如烂笔头

使用

代码来自于网络或者其他博主

#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"//sdk配置
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"//任务
#include "freertos/message_buffer.h" //使用消息缓冲区必须要有这个头文件
#include "esp_chip_info.h" //芯片_信息
#include "esp_flash.h"  //内存
#include "esp_system.h" //系统
#include "esp_spi_flash.h"
#include "driver/uart.h" //串口
#include "esp_log.h"  //日志
#include "gatts_table_creat_demo.h"//ble蓝牙
#include "driver/gpio.h" //GPIO
#include "driver/ledc.h" //LEDPWM
#include "driver/mcpwm.h"//电机LEDPWM

static const char *TAG = "example1";
/*串口*/
#define UART1_TX_BUF_SIZE 132           /* 256个字节 */
#define UART1_RX_BUF_SIZE 132           /* 256个字节 */
#define UART1_RX GPIO_NUM_16            /* 256个字节 */
#define UART1_TX GPIO_NUM_17            /* 256个字节 */
static char msg_test[] = "hello,world"; /* 测试使用的字符串 */
static char buffer[UART1_RX_BUF_SIZE];  /* 暂时存储从串口接收到的字符串 */

/*串口初始化*/
void uartInit()
{
    /* 串口1的配置 */
    const uart_config_t uart1_config =
        {
            .baud_rate = 115200,                   /* 通讯波特率 */
            .data_bits = UART_DATA_8_BITS,         /* 每一个数据是8位 */
            .parity = UART_PARITY_DISABLE,         /* 关闭奇偶校验 */
            .stop_bits = UART_STOP_BITS_1,         /* 停止位是1位 */
            .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, /* 软件控流 */
            .source_clk = UART_SCLK_APB,           /* APB时钟 */
        };
    /* 设置串口1的参数 */
    ESP_ERROR_CHECK(uart_param_config(UART_NUM_1,      /*UART 端口号,最大端口号为(UART_NUM_MAX -1)。*/
                                      &uart1_config)); /*UART 参数设置*/

    /* 设置串口的gpio口,esp32支持gpio口动态设置,这一次先使用默认的串口gpio */
    ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1,           /*UART 端口号*/
                                 UART1_TX,             /*UART TX 引脚 GPIO 编号*/
                                 UART1_RX,             /*UART RX 引脚 GPIO 编号*/
                                 UART_PIN_NO_CHANGE,   /*UART RTS 引脚 GPIO 编号*/
                                 UART_PIN_NO_CHANGE)); /*CTS 引脚 GPIO 编号*/

    /* 启动串口1 */
    ESP_ERROR_CHECK(uart_driver_install(UART_NUM_1,        /* 串口1 */
                                        UART1_TX_BUF_SIZE, /* 发送FIFO的大小 */
                                        UART1_RX_BUF_SIZE, /* 接受FIFO的大小 */
                                        0,                 /* 不使用queue */
                                        NULL,              /* 因为不使用queue,所以NULL */
                                        0)                 /* 不分配中断标志 */
    );
}

/*接收数据*/
void uartH()
{
    const uart_port_t uart_num = UART_NUM_1;
    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
        .rx_flow_ctrl_thresh = 122,
    };
    // 配置串口参数
    ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
    int len = 0;
    while (1)
    {
        // UART 从 UART 缓冲区读取字节,从串口1的RX_FIFO获取字符串
        len = uart_read_bytes(UART_NUM_1,               // UART端口号
                              buffer,                   // 指向缓冲区的指针
                              (UART1_RX_BUF_SIZE - 1),  // 数据长度
                              20 / portTICK_PERIOD_MS); // 等待时间
        // 如果读到包的话
        if (len)
        {
            printf("len  =======   %d\n", len);
            buffer[len] = '\0';                                                             // 在结尾加入字符'\0',
            ESP_LOGI(TAG, "Recv str -> %s , and the length is:%d", buffer, strlen(buffer)); // 打印logo
            // uart_write_bytes(UART_NUM_1, buffer, strlen(buffer));//发送数据到串口1
            memset(buffer, 0, sizeof(buffer)); // 清空内存,等待下一次的串口报文。
        }
    }
}

void app_main(void)
{
    uartInit();
    void uartH();
}

额,头文件没有整理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值