当前位置: 首页 > news >正文

ESP-IDF串口中断接收

串口接收并回传,定时发送数据

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "esp_log.h"
#include "driver/gpio.h"
static const char *TAG = "uart_events";#define EX_UART_NUM UART_NUM_1
#define PATTERN_CHR_NUM    (3)         /*!< Set the number of consecutive and identical characters received by receiver which defines a UART pattern*/#define BUF_SIZE (1024)
#define RD_BUF_SIZE (BUF_SIZE)
static QueueHandle_t uart0_queue;static void uart_event_task(void *pvParameters)
{uart_event_t event;size_t buffered_size;uint8_t* dtmp = (uint8_t*) malloc(RD_BUF_SIZE);for (;;) {//Waiting for UART event.if (xQueueReceive(uart0_queue, (void *)&event, (TickType_t)portMAX_DELAY)) {bzero(dtmp, RD_BUF_SIZE);ESP_LOGI(TAG, "uart[%d] event:", EX_UART_NUM);switch (event.type) {case UART_DATA:ESP_LOGI(TAG, "[UART DATA]: %d", event.size);uart_read_bytes(EX_UART_NUM, dtmp, event.size, portMAX_DELAY);ESP_LOGI(TAG, "[DATA EVT]:");uart_write_bytes(EX_UART_NUM, (const char*) dtmp, event.size);break;//Event of HW FIFO overflow detectedcase UART_FIFO_OVF:ESP_LOGI(TAG, "hw fifo overflow");uart_flush_input(EX_UART_NUM);xQueueReset(uart0_queue);break;//Event of UART ring buffer fullcase UART_BUFFER_FULL:ESP_LOGI(TAG, "ring buffer full");uart_flush_input(EX_UART_NUM);xQueueReset(uart0_queue);break;//Event of UART RX break detectedcase UART_BREAK:ESP_LOGI(TAG, "uart rx break");break;//Event of UART parity check errorcase UART_PARITY_ERR:ESP_LOGI(TAG, "uart parity error");break;//Event of UART frame errorcase UART_FRAME_ERR:ESP_LOGI(TAG, "uart frame error");break;default:ESP_LOGI(TAG, "uart event type: %d", event.type);break;}}}free(dtmp);dtmp = NULL;vTaskDelete(NULL);
}/* 串口发送任务 */
void uart_T_task(void *p)
{uint8_t get_jsy_value[8] = {0x02, 0x03, 0x00, 0x48, 0x00, 0x05, 0x05, 0xEC};while (1){uart_write_bytes(EX_UART_NUM, get_jsy_value, 8);vTaskDelay(pdMS_TO_TICKS(1000));}
}void app_main(void)
{esp_log_level_set(TAG, ESP_LOG_INFO);/* Configure parameters of an UART driver,* communication pins and install the driver */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_DISABLE,.source_clk = UART_SCLK_DEFAULT,};//Install UART driver, and get the queue.uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0);uart_param_config(EX_UART_NUM, &uart_config);uart_set_pin(EX_UART_NUM, GPIO_NUM_17, GPIO_NUM_16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);//Create a task to handler UART event from ISRxTaskCreate(uart_event_task, "uart_event_task", 3072, NULL, 12, NULL);xTaskCreate(uart_T_task, "uart_T_task", 4046, NULL, 10, NULL);
}

http://www.xdnf.cn/news/1470727.html

相关文章:

  • 概率论第二讲——一维随机变量及其分布
  • 广告投放全链路解析
  • B.50.10.01-消息队列与电商应用
  • PyInstaller完整指南:将Python程序打包成可执行文件
  • Nacos中yaml文件新增配置项不规范导致项目启动失败
  • 在 CentOS 上完整安装 Docker 指南
  • SQLServer死锁监测方案:如何使用XE.Core解析xel文件里包含死锁扩展事件的死锁xml
  • LightDock.server liunx 双跑比较
  • 消息队列-ubutu22.04环境下安装
  • 激光雷达与IMU时间硬件同步与软件同步区分
  • 深度学习之第八课迁移学习(残差网络ResNet)
  • ChartGPT深度体验:AI图表生成工具如何高效实现数据可视化与图表美化?
  • RequestContextFilter介绍
  • 53.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--集成短信发送功能
  • 《C++变量命名与占位:深入探究》
  • SDRAM详细分析—06 存储单元架构和放大器
  • RPC内核细节(转载)
  • 软件设计模式之单例模式
  • 实战:Android 自定义菊花加载框(带超时自动消失)
  • 微型导轨如何实现智能化控制?
  • 9.5 面向对象-原型和原型链
  • 【Linux】Linux 的 cp -a 命令的作用
  • 2025高教社数学建模国赛B题 - 碳化硅外延层厚度的确定(完整参考论文)
  • Overleaf教程+Latex教程
  • Anaconda下载安装及详细配置的保姆级教程【Windows系统】
  • excel里面店铺这一列的数据结构是2C【uniteasone17】这种,我想只保留前面的2C部分,后面的【uniteasone17】不要
  • MySQL 8.0.36 主从复制完整实验
  • S32K3平台ADC 应用说明
  • 无人机RTK模块技术要点与难点
  • GEO排名优化:迈向个性化与语义化搜索时代的智能策略