Receiving number as string (uart)

2020-03-30 01:25发布

问题:

I'm trying to receive a number through uart which is packed as a string. I'm sending number 1000, so I get 4 bytes + null character. But when I convert the array to number with atoi() and compare the integer with 1000 I don't always get a correct number. This is my interrupt handler function for receiving the number. What could be wrong?

void USART1_IRQHandler(void)
{
    if( USART_GetITStatus(USART1, USART_IT_RXNE) )
    {
        char t = USART1->RDR;
        if( (t != '\n' && t!='\0') && (cnt < 4) )
        {
            received_string[cnt] = t;
            cnt++;
        }
        else
        {
            cnt = 0;
        }

        t = 0;
        received_string[4] = 0;
    }

    if(cnt==4)
    {
        data = atoi(received_string);
    }
}

回答1:

Try this code instead. Here I check the max number of received bytes to avoid buffer overflow (and possible hardware fault). I created a specific function to clear the reception buffer. You can find also a definition for the string length because the code is more flexible. I suggest also to check the reception errors (after read the incoming byte) because in case of errors the reception is blocked.

//Define the max lenght for the string
#define MAX_LEN 5

//Received byte counter
unsigned char cnt=0;

//Clear reception buffer (you can use also memset)
void clearRXBuffer(void);

//Define the string with the max lenght
char received_string[MAX_LEN];

void USART1_IRQHandler(void)
{
    if( USART_GetITStatus(USART1, USART_IT_RXNE) )
    {
        //Read incoming data. This clear USART_IT_RXNE
        char t = USART1->RDR;

        //Normally here you should check serial error!
        //[...]

        //Put the received char in the buffer
        received_string[cnt++] = t;     

        //Check for buffer overflow : this is important to avoid
        //possible hardware fault!!!
        if(cnt > MAX_LEN)
        {
            //Clear received buffer and counter
            clearRXBuffer();                
            return;
        }

        //Check for string length (4 char + '\0')
        if(cnt == MAX_LEN)
        {
            //Check if the received string has the terminator in the correct position
            if(received_string[4]== '\0'){

                //Do something with your buffer
                int data = atoi(received_string);
            }

            //Clear received buffer and counter
            clearRXBuffer();                
        }
    }
}

//Clear reception buffer (you can use also memset)
void clearRXBuffer(void){
    int i;
    for(i=0;i<MAX_LEN;i++) received_string[i]=0;
    cnt=0;
}