how to catch line termination with \r\n chars?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I got a sensor that spits out serial data lines, where the lines are terminated using "\r\n" characters....

I am using usart receive interrupt to capture each incoming char. what would be the best way to capture this line end chars? Any idea? It seems I have to remember the last char and the current char and compare both to \r and \n chars...?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This little silly thing should match your line endings. I haven't tried to compile or anything, so no guarantees that it will work out of the box.

prog_char LINE_ENDING[] = "\r\n"

uint8_t lineState = 0;

void function_parsing_input(char c)
{
    if(prog_read_char(LINE_ENDING+lineState) == c)
    {
        lineState++;
    }
    else
    {
        lineState = 0;
    }

    if(prog_read_char(LINE_ENDING+lineState) == '\0')
    {
        // there's your newline
        lineState = 0;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You could just check for either '\n' or '\r' if each line won't contain it anywhere but the end.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd just throw away the carriage returns in the ISR and call it a day.
Then you just processes the lines as "normal" lines that end with newlines.

--- bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or just let the ISR set a flag when CR/LF is encountered , the main routine then knows when to evaluate the line buffer.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You could use a state machine, assuming you processing a character at a time.

I can put a snippet if you wish on how to go about it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I got it working with a state machine....but now I am changing my code again :( because of this thread https://www.avrfreaks.net/index.p...