/* code begin define */#define USART_2_RECV_DATA_BUF_ROW_NUM 2 // 串口 2 数据接收缓存行数。#define USART_2_RECV_DATA_BUF_COL_NUM 10 // 串口 2 数据接收缓存列数。/* code end define *//* code begin data type */typedefstruct{
UART_HandleTypeDef* huart;// 串口句柄
uint8_t indexForRecvDataBufRow;// 数据接收缓存的行索引。
uint8_t indexForRecvDataBufCol;// 数据接收缓存的列索引。
uint8_t recvDataBuf[USART_2_RECV_DATA_BUF_ROW_NUM][USART_2_RECV_DATA_BUF_COL_NUM];// 数据接收缓存。void(*sendData)(uint8_t* data, uint16_t data_len);// 发送数据。void(*recvDataInIntrrupt)(void);// 在中断中接收数据。该方法需要放在 USART2_IRQHandler 函数中。void(*processData)(void);// 处理数据、需要自己填写如何解析,处理数据。该方法可放在 while 循环或 recvDataInIntrrupt 中。 }objUsart2;// 串口 2 控制类。/* code end data type */
2. 在 .c 中创建串口控制类对象,声明需要实现的相关方法
/* code begin variable definition */
objUsart2 userUsart2;/* code end variable definition *//* code begin function statement */voidinitUserUsart2(void);staticvoiduserUsart2_sendData(uint8_t* data, uint16_t data_len);staticvoiduserUsart2_recvDataInIntrrupt(void);staticvoiduserUsart2_processData(void);/* code end function statement */
/* code begin variable statement */extern objUsart2 userUsart2;// 串口 2 控制类对象。/* code end variable statement *//* code begin function statement */voidinitUserUsart2(void);// 初始串口 2./* code end function statement */