It reports the difference between the head and the tail of the circular buffer the incoming characters are stored in.
The way serial reception on the Arduino works is:
- A character is received into the internal hardware RX buffer (
- An interrupt is triggered
- An interrupt service routine (ISR) is executed
- The character in
UDRx is read and, if there is room, stored in a circular buffer (
head pointer of
_rx_buffer is incremented and wrapped if needed.
When you actually read a character with
Serial.read() the following happens:
- If the
rx_buffer are equal, return
- Get the character at
tail and wrap if needed
- Return the character
So when you use
Serial.available() it just returns
head - tail (taking into account the wrapping, so the sum is slightly more complex than a simple subtraction).
You should examine the files
HardwareSerial.cpp in the Ardino AVR core software.