串口接收信息函數

/********************************************************************************************************
* Description: reciever message via uart
*
* Arguments  : ptr ---- a pointer used for data storage
*              nCmd---- the count of command
*
* Returns    : the length of data    
*              
* Notes      : 
********************************************************************************************************/
INT8U uart_recieve_msg(INT8U *ptr)
{
    INT8U wait_times = 0;
    run_on();
    while(g_rxcnt > 0) 
    {
        if(g_rxbuffer[g_rxhead] == CMD_START)
        {
            do{
                if(g_rxcnt > 0)
                {
                    *ptr++ = g_rxbuffer[g_rxhead];
                    g_rxhead = (g_rxhead+1)%UART_BUFFER_SIZE;
                    g_rxcnt--;
                }
                else
                {
                    wait_times++;
                    usleep(1000);
                    if(wait_times >20)
                        return (READ_CMD_TIMEOUT);
                }
            }while(g_rxbuffer[g_rxhead] != CMD_END);
            *ptr++ = g_rxbuffer[g_rxhead];
            run_off();
            return (READ_CMD_OK);
        } 
        else
        {
            g_rxhead = (g_rxhead+1)%UART_BUFFER_SIZE;
            g_rxcnt--;
        }
    }
    return (UART_NO_DATA);
}

發佈了55 篇原創文章 · 獲贊 20 · 訪問量 51萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章