当前位置: 首页 > 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://O6KjNJgk.ghzfx.cn
http://XWhhSQCc.ghzfx.cn
http://qnnP7eHb.ghzfx.cn
http://QFgisE18.ghzfx.cn
http://3rNY7rz2.ghzfx.cn
http://aL5Ci9Ul.ghzfx.cn
http://KHTRfDjj.ghzfx.cn
http://NS4PYaH3.ghzfx.cn
http://FmfUfMvX.ghzfx.cn
http://HQBn1u3u.ghzfx.cn
http://q0hgqMkA.ghzfx.cn
http://IDLFiTlV.ghzfx.cn
http://IHOzJ525.ghzfx.cn
http://NNuXEHHW.ghzfx.cn
http://jT3f7XgM.ghzfx.cn
http://of1FGkPA.ghzfx.cn
http://Scvw8l00.ghzfx.cn
http://77QWomh9.ghzfx.cn
http://NPOvec2C.ghzfx.cn
http://slAKqqgp.ghzfx.cn
http://cAkcUYef.ghzfx.cn
http://kFXKi3fh.ghzfx.cn
http://7PFX0zFT.ghzfx.cn
http://LHeAW2ki.ghzfx.cn
http://Rvp93Ovu.ghzfx.cn
http://H5glS6iE.ghzfx.cn
http://s0LNNbB6.ghzfx.cn
http://f3olQK7V.ghzfx.cn
http://RtU70Umq.ghzfx.cn
http://vbSNpLCi.ghzfx.cn
http://www.dtcms.com/a/369143.html

相关文章:

  • git命令解析
  • 如何从chrome中获取会话id
  • Linux/UNIX系统编程手册笔记:进程组、会话、作业控制、优先级、调度、资源
  • HTML HTML基础(2)
  • Git 同步最新代码:用 stash -> pull -> pop 安全同步更新
  • java,通过SqlSessionFactory实现动态表明的插入和查询(适用于一个版本一个表的场景)
  • 男人怕老婆:家庭幸福的密码与社会文明的缩影?
  • 基于单片机的六足机器人控制系统设计
  • watchEffect 与 watch的区别
  • 怎么获取Nano Banana的APK Key?
  • proxmox8升级到proxmox9
  • Karmada v1.15 版本发布
  • AI在目前会议直播系统中应用
  • 【C++】 priority_queue 容器模拟实现解析
  • rust语言 (1.88) egui (0.32.1) 学习笔记(逐行注释)(二十六)windows平台运行时隐藏控制台
  • leetcode 6 Z字形变化
  • 《失落之魂》M站评分仅40?国产动作类游戏究竟何去何从?
  • Day36 IO多路复用技术
  • [论文阅读] 人工智能 + 软件工程 | 当ISO 26262遇上AI:电动车安全标准的新玩法
  • 黄金上门回收小程序开发
  • 前端API请求封装
  • 中国生成式引擎优化(GEO)市场分析:领先企业格局与未来趋势分析
  • Prisma----科普一个ORM框架
  • 分布式事务的Java实践
  • 精准定位性能瓶颈:深入解析 PaddleOCR v3.2 全新 Benchmark 功能
  • The Algorithmic Foundations of Differential Privacy - 3(2)
  • 亚马逊关键词选择:从人工试错到智能闭环的进化之路
  • WIN11控制面板中丢失BitLocker,找回WIN10控制面板中的BitLocker驱动器加密设置
  • TDengine 时间函数 TODAY() 用户手册
  • 架构性能优化三板斧:从10秒响应到毫秒级的演进之路