定义
uint8_t aRxBuffer; //接收中断缓冲
uint8_t Uart2_RxBuff[256]; //接收缓冲
uint8_t Uart2_Rx_Cnt = 0; //接收缓冲计数
uint8_t cAlmStr[] = "数据溢出(大于256)
";
回调函数
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
*/
uint8_t aRxBuffer; //接收中断缓冲
uint8_t Uart2_RxBuff[256]; //接收缓冲
uint8_t Uart2_Rx_Cnt = 0; //接收缓冲计数
uint8_t cAlmStr[] = "数据溢出(大于256) ";
回调函数
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(Uart2_Rx_Cnt >= 255) //溢出判断
{
Uart2_Rx_Cnt = 0;
memset(Uart2_RxBuff,0x00,sizeof(Uart2_RxBuff));
HAL_UART_Transmit(&huart2, (uint8_t *)&cAlmStr, sizeof(cAlmStr),0xFFFF);
}
else
{
Uart2_RxBuff[Uart2_Rx_Cnt++] = aRxBuffer; //接收数据转存
if((Uart2_RxBuff[Uart2_Rx_Cnt-1] == 0x0A)&&(Uart2_RxBuff[Uart2_Rx_Cnt-2] == 0x0D)) //判断结束位
{
printf(" 您发送的消息为: ");
HAL_UART_Transmit(&huart2, (uint8_t *)&Uart2_RxBuff, Uart2_Rx_Cnt,0xFFFF); //将收到的信息发送出去
Uart2_Rx_Cnt = 0;
memset(Uart2_RxBuff,0x00,sizeof(Uart2_RxBuff)); //清空数组
}
}
HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer, 1); //再开启接收中断
}
一周热门 更多>