Search code examples
bufferavruartc-strings

What am I missing with this routine to read uart data on AVR


I am able to read the data fine. The data is a series of records, each ending with "\r\n" as the terminator and then the Data stream ends with a "\r\n". The first record comes in perfect but the remaining records are missing the first X number of characters. X does not change either. Data is coming from a modem.

c = UART0_RxChar();
response[0]=c;
i=0;
rec=0;
while((c=UART0_RxChar()) != CR)
{
    response[i++]=c;
    while ((c=UART0_RxChar()) != CR)
    {
        response[i++]=c;

    }
    rec++;
    UART1_Printf("Record %d %s", rec, response);
    UART1_Printf("\n\n");
    memset(response,0,strlen(response));
    i=0;
    AT_ASSERT(UART0_RxChar() == LF)
}

AT_ASSERT(UART0_RxChar() == LF)
// Finished

Bad Data Received

Record 1 7854122,4,2017-04-11 00:00,2017-04-11 8:09,29342441,,,,,,,0,0,0,0,

Record 2 705-03 13:15,18958737,TRK 23564,,,,BOL 327867,,0,0,0,0,

Record 3 7,BOL 327867,,0,0,0,0,

Record 4 717-05-03 23:59,38580000,TRK 23564,,,,BOL 327867,,0,0,0,0,

Record 5 7,BOL 327867,,0,0,0,0,

Record 6 717-05-05 23:59,86340000,TRK 23564,,,,BOL 327867,,0,0,0,0,

Record 7 7,BOL 327867,,0,0,0,0,

Actual Data

7854122,4,2017-04-11 00:00,2017-04-11 08:09,29342441,,,,,,,0,0,0,0,
7854122,1,2017-04-11 08:09,,0,,,,,,,0,0,0,0,
7854122,4,2017-05-03 08:00,2017-05-03 13:15,18958737,TRK 23564,,,,BOL 327867,,0,0,0,0,
7854122,1,2017-05-03 13:15,2017-05-03 13:16,70332,TRK 23564,,,,BOL 327867,,0,0,0,0,
7854122,3,2017-05-03 13:16,2017-05-03 23:59,38580000,TRK 23564,,,,BOL 327867,,0,0,0,0,
7854122,3,2017-05-04 00:00,2017-05-04 23:59,86340000,TRK 23564,,,,BOL 327867,,0,0,0,0,

Solution

  • The UART may not be reading the data fast enough to capture it all and so is dropping data

    If the UART_RxChar function is using a buffer that came from interrupts writing to buffers, you may be able to increase the size of the buffers.