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-1] == 0x5A) //若是收到的是 0x5A 則下面就是有效數據 Uart2_Tx = Uart2_Rx-1; if((Uart2_Buffer[Uart2_Tx] == 0x5A)&&(Uart2_Buffer[Uart2_Rx-1] == 0xA5)) //檢測到頭的狀況下檢測到尾 { Uart2_Len = Uart2_Rx-1- Uart2_Tx; //長度 Uart2_Sta=1; //標誌位 } if(USART_GetFlagStatus(USART2,USART_FLAG_ORE) == SET) //溢出 { USART_ClearFlag(USART2,USART_FLAG_ORE); //讀SR USART_ReceiveData(USART2); //讀DR } } main.c################################################################### if( Uart2_Sta )//數據幀接收完畢 { for(tx2=0;tx2 <= Uart2_Len;tx2++,Uart2_Tx++) USART2_SendByte(Uart2_Buffer[Uart2_Tx]); //發送數據 Uart2_Rx = 0; //初始化 Uart2_Tx = 0; Uart2_Sta = 0; } 好比數據幀爲 5A 00 11 65 33 22 23 43 A5 此爲一幀