Maybe I'm going at this the wrong way.
In the scenario described, the code is receiving single raw bytes one at a time. Even if there are more in the buffer, it only reads one at a time. The code will initially look forever for the 'start' character and if it receives any other character in the meantime then it will discard that character and carry on looking for the start character.
Once the start character has been detected the code then needs to carry on receiving single raw bytes for as long as they continue to arrive within a set time interval. If the time since the last received byte exceeds that set interval, that is how the code determines that the sender is not going to send any more.
I want to do it this way because the material being received contains no information about its length / size and no special 'end of stream' marker character - the final byte of incoming information is equally likely to have any value from 0x00 to 0XFF. The only thing which makes the last character unique is that it will not be followed by any more characters, so I want to determine that by timing the interval since the last character was received. If no more appear to be coming, then the receive phase of the operation ends
If I were going direct to the hardware on Arduino or on any other processor like a PIC I would do something like:
(Pseudocode)
uiTimeOutValue=500mS
uiAddress=0
Read serial input bytes until 'start' character is received.
Store it in RAM location 'uiAddress'
uiAddress=uiAddress+1
Load hardware timer 'HardwareTimer' with value equivalent to uiTimeOutValue // (assume it is a downcounter)
while(HardwareTimer >= 0)
{
if there is a serial byte waiting to be received
{
Receive and store it in RAM location 'uiAddress'
uiAddress=uiAddress+1
Re-load HardwareTimer with uiTimeOutValue
}
}
All data now received. Begin working through the RAM from address 0 and processing the received data
You can assume for the purposes of the Pseudocode above that the RAM storage into which the received characters is being placed is limitless. It nearly is, because it is a 32K * 8 SRAM. The input data will never get close to filling all of that up. Several Kbytes at the most.
Unless things have changed, I'm not sure that the feature set of Arduino provides for the loading, running and reading of an independent hardware timer such that you can start a timer running, go away and do a few other things and then come back and interrogate the timer to see how much time has elapsed since you started it. Am I wrong about that? Can I do this using 'pure' Arduino or will I need to go directly to the timer registers? I would rather not do that as it will make the code specific to whatever platform I write it for.
I should say that I have no control over the format of the data being received, it is what it is, so I can't change it to make it easier to handle.