STM32 HAL库串口不定长数据

添加中断回调函数

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
		
	if(huart->Instance == USART3)	// 判断是由哪个串口触发的中断
	{
		
		
		if(ulart3_counts == 0)
		{
				uart_data_caler(3);
		}
		ulart3_read_bufs[ulart3_counts++] = read_ulart3;
		if(read_ulart3 == 0x0d)
		{
			ulart3_counts = 0;
		}
		if(ulart3_counts>30)
		{
			ulart3_counts = 0;
		}
		HAL_UART_Receive_IT(&huart3,&read_ulart3,1);		// 重新使能串口1接收中断
		
	}
	if(huart->Instance == USART2)	// 判断是由哪个串口触发的中断
	{
		
		
		if(ulart2_counts == 0)
		{
				uart_data_caler(2);
		}
		ulart2_read_bufs[ulart2_counts++] = read_ulart2;
		if(read_ulart2 == 0x0d)
		{
			ulart2_counts = 0;
		}
		if(ulart2_counts>30)
		{
			ulart2_counts = 0;
		}
		HAL_UART_Receive_IT(&huart2,&read_ulart2,1);		// 重新使能串口1接收中断
		
	}
	if(huart->Instance == USART1)	// 判断是由哪个串口触发的中断
	{
		
		
		if(ulart1_counts == 0)
		{
				uart_data_caler(1);
		}
		ulart1_read_bufs[ulart1_counts++] = read_ulart1;
		if(read_ulart1 == 0x0d)
		{
			ulart1_counts = 0;
		}
		if(ulart1_counts>30)
		{
			ulart1_counts = 0;
		}
		HAL_UART_Receive_IT(&huart1,&read_ulart1,1);		// 重新使能串口1接收中断
		
	}

}
void uart_data_caler(char i)
{
	if(i ==1)
	{
			memset(ulart1_read_bufs, 0, sizeof(ulart1_read_bufs));
			ulart1_counts = 0;
	}
	else if(i==2)
	{
			memset(ulart2_read_bufs, 0, sizeof(ulart2_read_bufs));
			ulart2_counts = 0;
	}
	else if(i==3)
	{
			memset(ulart3_read_bufs, 0, sizeof(ulart3_read_bufs));
			ulart3_counts = 0;
	}
}

使能中断接收

	HAL_UART_Receive_IT(&huart3,&read_ulart3,1);	
	HAL_UART_Receive_IT(&huart1,&read_ulart1,1);
	HAL_UART_Receive_IT(&huart2,&read_ulart2,1);
	
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页