定义
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); //再开启接收中断
}
你写的不太规范啊,接收完成回调函数,传入的串口句柄参数,你怎么使用UNUSED?需要用传入的参数判断是不是当前的串口。再一个就是在中断中使用发送函数也是不合理的,因为你所使用的是阻塞式的发送,会导致中断不能及时退出。至于你说的不能通讯的问题,可以使用单步调试,看看是否进入接收中断了。接收中断都不进,说明是线路的问题或者是串口配置的问题。
一周热门 更多>