• 【ESP32】C语言映射表在嵌入式串口解析中的应用


    本文章主要以ESP32开发环境为例记录,C语言映射表在嵌入式串口解析中的应用

    一、C语言映射表在串口数据解析中的应用

    1、数据结构

    typedef struct 
    {
        char CMD[CMDLen];
        unsigned char (*cmd_operate)(char *data);
    }Usart_Tab;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、指令、函数映射表

    static const Usart_Tab InstructionList[CMDMax]=
    {
        {"PWON",PowOn},
        {"PWOFF",PowOff},
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3、串口解析函数实现

    /* 串口解析函数实现 */
    unsigned char DataAnalysis(char *buf)
    {
      unsigned char i,Result = 0;
      int NEXT=0;
      for(i=0;i<CMDMax;i++)
      {
        NEXT=strcmp((char *)buf,(char*)InstructionList[i].CMD);
        if(NEXT==0)
        {
           Result = (InstructionList[i].cmd_operate)(buf) ;
           break;
        }
      }
      return Result;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    二、实验现象

    请添加图片描述

    三、实验代码

    #include "freertos/FreeRTOS.h"
    #include "freertos/task.h"
    #include "esp_system.h"
    #include "esp_log.h"
    #include "driver/uart.h"
    #include "string.h"
    #include "driver/gpio.h"
    
    #include "ring_buffer.h"
    
    static const int RX_BUF_SIZE = 1024;
    
    
    #define TXD_PIN (GPIO_NUM_4)
    #define RXD_PIN (GPIO_NUM_5)
    
    
    #define CMDMax 2
    #define CMDLen 24
    
    
    static unsigned char PowOn(char *data);
    static unsigned char PowOff(char *data);
    
    
    /* 数据结构定义 */
    typedef struct 
    {
        char CMD[CMDLen];
        unsigned char (*cmd_operate)(char *data);
    }Usart_Tab;
    
    typedef unsigned char (*usartfuncp)(char *data);
    
    
    /* 指令、函数映射表 */
    static const Usart_Tab InstructionList[CMDMax]=
    {
        {"PWON",PowOn},
        {"PWOFF",PowOff},
    };
    
    
    
    /* 串口解析函数实现 */
    unsigned char DataAnalysis(char *buf)
    {
      unsigned char i,Result = 0;
      int NEXT=0;
      for(i=0;i<CMDMax;i++)
      {
        NEXT=strcmp((char *)buf,(char*)InstructionList[i].CMD);
        if(NEXT==0)
        {
           Result = (InstructionList[i].cmd_operate)(buf) ;
           break;
        }
      }
      return Result;
    }
    
    static unsigned char PowOn(char *data)
    {
        printf("%s \r\n",data);
        return 0;
    }
    
    static unsigned char PowOff(char *data)
    {
        printf("%s \r\n",data);
        return 0;
    }
    
    
    
    
    
    
    
    
    
    void uart_init(void) {
        const 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_APB,
        };
        // We won't use a buffer for sending data.
        uart_driver_install(UART_NUM_1, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
        uart_param_config(UART_NUM_1, &uart_config);
        uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
        
    static void rx_task(void *arg)
    {
        static const char *RX_TASK_TAG = "RX_TASK";
        esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
        uint8_t* data = (uint8_t*) malloc(RX_BUF_SIZE+1);
        while (1) {
            const int rxBytes = uart_read_bytes(UART_NUM_1, data, RX_BUF_SIZE, 100 / portTICK_RATE_MS);
            if (rxBytes > 0) {
                
                data[rxBytes] = '\0';
                ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data);
                //ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO);
                DataAnalysis((char *)data);
                
                //ring_buffer_writeArry(&ring_buffer,(const char*)data,rxBytes);
            }
        }
        free(data);
    }
    
    
    
    void app_main(void)
    {
        uart_init();
        ring_buffer_init(&ring_buffer, buf_arr, sizeof(buf_arr));
        xTaskCreate(rx_task, "uart_rx_task", 1024*2, NULL, configMAX_PRIORITIES, NULL);
        //xTaskCreate(tx_task, "uart_tx_task", 1024*2, NULL, configMAX_PRIORITIES-1, NULL);
    }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
  • 相关阅读:
    【Vue原理系列】(一)简要总结Vue3运行时+编译时原理
    C/C++轻量级并发TCP服务器框架Zinx-游戏服务器开发002:框架学习-按照三层结构模式重构测试代码+Tcp数据适配+时间轮定时器
    ServiceComb场景及其原理
    开发Chrome插件,实现网站自动登录
    基于开源项目OCR做一个探究(chineseocr_lite)
    Java学习笔记(十九)
    JVM学习----垃圾回收调优
    pdf添加水印
    【Day-30慢就是快】代码随想录-二叉树-找树左下角的值
    设计模式之美——里式替换原则 和 接口隔离原则
  • 原文地址:https://blog.csdn.net/qq_39217004/article/details/133877446