stm32串口接收完整的数据包

时间:2023-03-09 04:03:00
stm32串口接收完整的数据包

参考了文章:《stm32串口中断接收方式详细比较》

文章地址:http://bbs.elecfans.com/jishu_357017_1_1.html

借鉴了第四种中断方式

串口的配置这里不做说明,仅对stm32接收中断中的数据进行解析。

数据帧协议:

帧头1 帧头2 数据长度 有效数据 crc_1 crc_2
B5  5B 03 00 57 0B

帧头1+帧头2+数据长度(包含有效数据、crc_1、crc_2)+有效数据 + crc_1 + crc_2(校验为帧头到有效数据)

协议采用小端模式,低字节在前,高字节在后。

crc16校验未深入学习,代码也不是自己写的,我仅是拿来用,所以未给出,也可以选择其他校验方法。

crc16函数声明:uint16_t CRC16(uint8_t * buf, uint16_t Len); 返回值为uint16_t校验值

代码如下:

/****************************
函数名称: USART2_IRQHandler
功 能:串口2接收中断
参 数:无
返 回 值:无
作 者:Yao
****************************/ uint8_t Uart2_Buffer[]; //接收缓冲区
uint8_t Uart2_Rx = ; //Uart2_Buffer下标
uint8_t Uart2_head1; //帧头1
uint8_t Uart2_head2; //帧头2
uint8_t Uart2_Len; //数据长度(第三字节以后包含crc)
uint16_t Uart2_temp; //CRC16()返回值
uint8_t Uart2_Sta; //数据帧正确标志
uint8_t Uart2_tx2; //发送计数 uint16_t CRC16(uint8_t * buf, uint16_t Len); //crc16函数声明,定义未给出。返回uint16_t校验值 void USART2_IRQHandler()
{
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
Uart2_Buffer[Uart2_Rx] = USART_ReceiveData(USART2);
Uart2_Rx++;
Uart2_Rx &= 0xFF;
}
if(Uart2_Buffer[Uart2_Rx-] == 0xB5) //判断帧头1
Uart2_head1 = Uart2_Rx-;
else if((Uart2_Rx- == Uart2_head1+)&&(Uart2_Buffer[Uart2_Rx-] == 0x5B)) //判断帧头1数据后是否为帧头2
Uart2_head2 = Uart2_Rx-;
else if(Uart2_Rx- == Uart2_head2+) //得到数据长度
Uart2_Len = Uart2_Buffer [Uart2_Rx-]; else if(Uart2_Rx- == Uart2_head1 + Uart2_Len+) //确保接收一帧数据
{
Uart2_temp = CRC16(&Uart2_Buffer[Uart2_head1],Uart2_Len+); //计算crc
if(((Uart2_temp&0x00ff)==Uart2_Buffer[Uart2_head1+Uart2_Len+])&&(((Uart2_temp>>)&0x00ff)==Uart2_Buffer[Uart2_head1+Uart2_Len+])) //判断crc是否正确
{
Uart2_Sta = ; //标志置1
}
} if(USART_GetFlagStatus(USART2,USART_FLAG_ORE) == SET)
{
USART_ClearFlag(USART2,USART_FLAG_ORE);
USART_ReceiveData(USART2);
} if(Uart2_Sta) //检测到标志
{
for(Uart2_tx2=;Uart2_tx2 <= Uart2_Len+;Uart2_tx2++,Uart2_head1++)
USART2_SendByte(Uart2_Buffer[Uart2_head1]); //从缓冲区中第Uart2_head1字节开始,接收总共Uart2_Len+2个字节 Uart2_Rx = ; //下标清0
Uart2_Sta = ; //标志置0
}
}