本帖最后由 cnyg 于 2013-5-17 11:08 编辑
程序出错源码:
问题用PC发送N段数据(中间延时不定),第一次接收正确,以后每次只能接收到0xFD其余为0X00,
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
temp = USART_ReceiveData(USART2);
Usart2_Receive_Counter++;
if( Usart2_Receive_mode == FLAG_DATA )
{
if ( Usart2_Work_mode == STAT_IDLE )
{
if (temp == 0xfd)
{
Usart2_numlength = 0;
Usart2_RX_DATA[Usart2_Amount][Usart2_numlength++] = temp;
Usart2_Work_mode = STAT_RECEIVE; //正确接收到返回的起始字 0xFD
}
} else if (Usart2_Work_mode == STAT_RECEIVE)
{
if( Usart2_numlength == DATA_SIZE )
{
memset(&Usart2_RX_DATA[Usart2_Amount],0,DATA_SIZE);
Usart2_Work_mode = STAT_IDLE;
}else
{
Usart2_RX_DATA[Usart2_Amount][Usart2_numlength++] = temp;
if (Usart2_numlength == Usart2_RX_DATA[Usart2_Amount][1] + 7)
{
Usart2_Work_mode = STAT_IDLE;
Usart2_Receive_OK = STAT_OK;
Usart2_RX_DATA[Usart2_Amount][39]=STAT_OK;
Usart2_Amount++;
if(Usart2_Amount == DATA_AMOUNT)
Usart2_Amount=0;
}
}
}
}else Usart2_RX_CMD[Usart2_numlength++] = temp;
但是如果代码改为这个接收就正常了????????
难道程序能够影响串口接收?????????
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
temp = USART_ReceiveData(USART2);
Usart2_Receive_Counter++;
Usart2_RX_CMD[Usart2_numlength++] = temp;
// if( Usart2_Receive_mode == FLAG_DATA )
// {
// if ( Usart2_Work_mode == STAT_IDLE )
// {
// if (temp == 0xfd)
// {
// Usart2_numlength = 0;
// //memset(Usart1_RX_DATA[Usart1_Amount][],0,40);
// Usart2_RX_DATA[Usart2_Amount][Usart2_numlength++] = temp;
// Usart2_Work_mode = STAT_RECEIVE; //正确接收到返回的起始字 0xFD
// }
// } else if (Usart2_Work_mode == STAT_RECEIVE)
// {
// if( Usart2_numlength == DATA_SIZE )
// {
// memset(&Usart2_RX_DATA[Usart2_Amount],0,DATA_SIZE);
// Usart2_Work_mode = STAT_IDLE;
// }else
// {
// Usart2_RX_DATA[Usart2_Amount][Usart2_numlength++] = temp;
// if (Usart2_numlength == Usart2_RX_DATA[Usart2_Amount][1] + 7)
// {
// Usart2_Work_mode = STAT_IDLE;
// Usart2_Receive_OK = STAT_OK;
// Usart2_RX_DATA[Usart2_Amount][39]=STAT_OK;
// Usart2_Amount++;
// if(Usart2_Amount == DATA_AMOUNT)
// Usart2_Amount=0;
// }
// }
// }
// }else Usart2_RX_CMD[Usart2_numlength++] = temp;
|