Understanding Serial.available() function

Hi,

I am a bit stuck with the small details of the Serial.available() function. From the online documentation it's pretty clear to me that the function will return the number of bytes available in the serial input buffer. To test this I made a very simple sketch:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(Serial.available());
  delay(200);
}

Then in the serial monitor the value 0 is returned which is correct. Then I type 5 characters and send them which changes the value to 5 which is still correct.
Then I type another 5 characters and send them, now expecting the returned value to be 10 but it's not, it stays at 5 and if I try to send additional characters the serial monitor seems to stop working until I reset the MCU. I have tried sending various combinations for the number of characters, it never seems to register the second set of characters sent.

Since I never read any bytes from the input buffer the bytes available should just keep increasing (as I send more characters) up to the buffer limit right...

Could anybody shed some light on this behaviour?

I am running a feather board which uses the SAMD21 MCU (same as the Arduino Zero).

For me increments from 5 to 10 as you expected, on a Uno.

I tried your program on my Mega and it works properly. The number keeps incrementing as I add characters until it reaches 63

…R

Here's a pic...

serial stuff.GIF

serial stuff.GIF

Thanks for the help guys, that sure is strange, so it must then be something in my feather serial library not working correctly… will work through the uart related code then to see if I can find something obvious.

A direct USB connection might behave differently.

swanepoeljan:
that sure is strange, so it must then be something in my feather serial library not working correctly...

Or it might be a hardware thing about the way a feather works?- like somehow automatically clearing the buffer when new stuff arrives? Who knows?

Must be something with the native USB CDC code, I wired the second UART and used Serial1 and then it behaves as expected.