From Nick Gammon:
How to process incoming serial data without blocking
You really should look at the State machine part as it directly addresses what you are doing.
snip--
State Machine
Another way of processing incoming data, without blocking, is to set up a "state machine". Effectively this means looking at each byte in the input stream, and handling it depending on the current state.
As an example, say you had this coming into the serial port:
R4500S80G3
Where Rnnn is RPM, Snnnn is speed, and Gnnnn is the gear setting.
The state machine below switches state when it gets a letter "R", "S" or "G". Otherwise it processes incoming digits by multiplying the previous result by 10, and adding in the new one.
When switching states, if first handles the previous state. So for example, after getting R4500 when the "S" arrives, we call the ProcessRPM function, passing it 4500.
This has the advantage of handling long messages without even needing any buffer, thus saving RAM. You can also process message as soon as the state changes, rather than waiting for end-of-line.
Yes, the code is there.