Ian,
When you lost synchronisation (at startup or when any received character was lost or damaged), there's no change to determine, what character might be the next. So you need a mechanism to find complete messages. Either search for the token or start a new message with the character following CRLF (btw, this is 0x0d0a).
So what I would do is something like this pseudocode:
Open serial port
Clear a string shift register SR
While not finished do
wait some time
N=Bytes at serial port
Read N bytes
append them to whatever is in SR
delete all characters in SR before the first set of CRLF
find the position of CRLF in SR starting at pos 2
If found, remove the substring from pos 0 to foundposition-2
process the removed substring
put the remainder back into SR
WhileEnd
Close serial port
This way you'll have some data in SR that always start with CRLF. You remove and process complete messages, but those do start now with CRLF instaed of ending with. But that's not a problem.
If processing the data is complicated or time consuming, it is better to separate data reading and processing. Put the processing into a parallel independent while loop. Stop it with a local variable of the stop for the first while loop. Use a named queue to transfer those messages from the reader to the processor. Don't forget a wait in both loops.
Greetings from Germany!
--
Uwe