本文章主要以ESP32开发环境为例记录,C语言映射表在嵌入式串口解析中的应用
【ESP32】C语言映射表在嵌入式串口解析中的应用
- 一、C语言映射表在串口数据解析中的应用
- 1、数据结构
- 2、指令、函数映射表
- 3、串口解析函数实现
- 二、实验现象
- 三、实验代码
一、C语言映射表在串口数据解析中的应用
1、数据结构
typedef struct
{
char CMD[CMDLen];
unsigned char (*cmd_operate)(char *data);
}Usart_Tab;
2、指令、函数映射表
static const Usart_Tab InstructionList[CMDMax]=
{
{"PWON",PowOn},
{"PWOFF",PowOff},
};
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;
}
二、实验现象
三、实验代码
#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);
}
}