串口常用函数:
HalUARTOpen() // 打开串口 HalUARTRead() // 读取串口数据 HalUARTWrite() // 向串口发送数据
1、定义串口缓存区
// 串口数据缓存 unsigned char uartbuf[128];
2、定义串口回调函数
static void rxCB(uint8 port, uint8 event); // 串口回调函数
3、在应用初始化中进行串口初始化
void GenericApp_Init(byte task_id){
halUARTCfg_t uartConfig; // 定义串口配置
GenericApp_TaskID = task_id;
GenericApp_TransID = 0;
GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
GenericApp_epDesc.task_id = &GenericApp_TaskID;
GenericApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
GenericApp_epDesc.latencyReq = noLatencyReqs;
afRegister(&GenericApp_epDesc);
// 串口配置
uartConfig.configured = TRUE;
uartConfig.baudRate = HAL_UART_BR_115200;
uartConfig.flowControl = FALSE;
uartConfig.callBackFunc = rxCB;// 回调函数
HalUARTOpen(0, &uartConfig);
}
4、回调函数
static void rxCB(uint8 port, uint8 event){
HalUARTRead(0, uartbuf, 13);
if(osal_memcmp(uartbuf, "www.luchg.com", 13)){
HalUARTWrite(0, uartbuf, 13);
}
}
5、添加宏定义 HAL_UART=TRUE