串口接收回调函数如下:
/** * @brief Rx Transfer completed callbacks. * @param huart pointer to a UART_HandleTypeDef structure that contains * the configuration information for the specified UART module. * @retval None */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { /* Prevent unused argument(s) compilation warning */ UNUSED(huart); /* NOTE: This function Should not be modified, when the callback is needed, the HAL_UART_TxCpltCallback could be implemented in the user file */ if(huart->Instance == huart1.Instance) { if(bRx1_complete != 0) return;//接收的命令还没处理完,不再接收新数据 if(Uart1_Rx_Cnt >= 255) //溢出判断 { Uart1_Rx_Cnt = 0; memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff)); } else { Uart1_RxBuff[Uart1_Rx_Cnt] = aRx1Buffer; //接收数据转存 Uart1_Rx_Cnt++; if((Uart1_RxBuff[Uart1_Rx_Cnt-1] == 0x0A)&&(Uart1_RxBuff[Uart1_Rx_Cnt-2] == 0x0D)) //判断结束位(回车换行符) { Uart1_Rx_Cnt= 0; bRx1_complete = 1;//接收完成 } } } HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRx1Buffer, 1); //再开启接收中断 }
解析每个逗号前的字符
if(bRx1_complete == 1) { char *ptr,*retptr; int i=0; ptr = Uart1_RxBuff;//指针指向串口1接收的缓冲数组 while ((retptr=strtok(ptr, ",")) != NULL) {//逐个解析逗号前的字符 printf("substr[%d]:%s ", i++, retptr);//打印每个逗号前的字符 switch(i) { case 1:SavePara(retptr,ppara1);break;//保存第1个字符 case 2:SavePara(retptr,ppara2);break; case 3:SavePara(retptr,ppara3);break; case 4:SavePara(retptr,ppara4);break; case 5:SavePara(retptr,ppara5);break; case 6:SavePara(retptr,ppara6);break;//保存第6个字符 default: break; } ptr = NULL; }
}