Serial Port - Buffering questions - "sliding" / ring buffer performance

Hello everyone,

I digged further into it and put together a runnable sketch. In "real life" this is a triple programmalble timer for laboratory use. Don't bother about some functionality that is not strictly required or just dropped here - I just replaced the loop() with something very minimal, and left out parts that are of no interest for time being.

SEE ATTACHMENT!

Main point of interest is still the doSerialPort() part. I have added some diagnostic output in the end of it.

Now, start the sketch, and throw the following string at it IN ONE CHUNK, i.e. copy and paste it into e.g. PuTTY, or fire it from the Serial Monitor in Arduino IDE:

12345678901234567890123456789012345678901234567890123456789012345678901234567890

I see the following output from that:

12Chars read per turn: 2
345678Chars read per turn: 6
901234Chars read per turn: 6
567890123456789012345678901234567890123456789012345678901234567890
 PARAMETER STRING ERROR

Chars read per turn: 68

That looks surprising, and not deterministic... any explanations?
Maybe the timer interrupt is coming in our way? I use the "MsTimer2" librar for a 1 millisecond interval.

As for the buffer shifting:
Morgan, you're not quite right... what I do is to first fill the buffer up to 20 characters, check the complete buffer after every new character if my "expected_string" is now contained, and after reaching 20 chars, shift the buffer 1 char to the left and append the new char at the end.

So, using an example size of 5 chars for demonstration, reading in "abcdefghij..." you would see this:
a
ab
abc
abcd
abcde
bcdef
cdefg
...

Let's say I'm expecting "def" to be contained, this will be the case after the 6th step.

Best regards

stargar

serial_testing_1.ino (16.2 KB)