Can't understand how Serial.available() works

In the reference page of Serial.available() one reads:

Get the number of bytes (characters) available for reading from the serial port. This is data that's already arrived and stored in the serial receive buffer (which holds 64 bytes).

I have the following code:

void readSerialInput() {
  static int tempVal, pin, pinVal;
  byte inByte;

  if (Serial.available()) {
    Serial.print("buffer size: ");
    Serial.println(Serial.available());
    inByte = Serial.read();
    if (isDigit(inByte))
      tempVal = tempVal * 10 + inByte - '0';
    else {
      if (inByte == 'p') {
        pin = tempVal;
        tempVal = 0;
      }
      else if (inByte == 'v') {
        pinVal = tempVal;
        digitalWrite(pin, pinVal);
        tempVal = 0;
      }
    }
  }
}

void setup() {
  pinMode(13, OUTPUT);
  
  Serial.begin(9600);
}

void loop() {
  readSerialInput();
}

And in Arduino's Serial monitor I type this:

13p1v

to turn on the built-in LED on the board. The LED reacts properly, but I get this print in the monitor:

buffer size: 1
buffer size: 1
buffer size: 1
buffer size: 1
buffer size: 2
buffer size: 1

I'm sending 5 bytes but I get the number 1 four times, then the number 2 once, and then the number 1 once again (I guess the Carriage Return and the New Line characters are there too). Shouldn't the serial receive buffer get 5 bytes (or 7 with the CR and NL)?

What I want to do is get notified of when the buffer is emptied, but this way, if I include an 'else' after ' if (Serial.available())', I get alternating 1s and 0s for every character of the string sent to the Arduino.
Can someone explain why Serial.available() returns these values and the buffer is never filled with all the bytes of the string sent?

Thanks

The serial connection is really slow compared to the Arduino's processor.
What happens is: One byte arrives, Serial.available == 1, you read the byte, it is removed from the buffer, so Serial.available == 0, then the Arduino just keeps repeating the loop, checking Serial.available, until you receive the next byte, Serial.available == 1, you read it, Serial.available == 0, etc ...

If the code that handles the read byte (your if/else statements etc.) takes a long time to complete, it is possible that you receive more than one byte while this code is running. So when the code finally finishes, and you repeat the loop again, it checks Serial.available, and it will be more than one.
Receiving bytes into the RX buffer happens in the background, independent of what the CPU is doing.

Pieter

Ok, that's a good explanation. Makes things clear, thanks a lot!

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

See, especially, how it gathers all the characters before reporting that the newData is ready

...R

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

See, especially, how it gathers all the characters before reporting that the newData is ready

...R

Start and end characters seems like a good idea. Changed the code to this, which now works fine:

bool inputStarted = false;
bool endNotificationSent = true;

void readSerialInput() {
  static int tempVal, pin, pinVal;
  byte inByte;

  if (Serial.available()) {
    inByte = Serial.read();
    if (inByte == '<') {
      inputStarted = true;
      endNotificationSent = false;
    }
    if (inputStarted) {
      if (isDigit(inByte))
        tempVal = tempVal * 10 + inByte - '0';
      else {
        if (inByte == 'p') {
          pin = tempVal;
          tempVal = 0;
        }
        else if (inByte == 'v') {
          pinVal = tempVal;
          digitalWrite(pin, pinVal);
          tempVal = 0;
        }
        else if (inByte == '>') {
          inputStarted = false;
          if (!endNotificationSent) {
            Serial.println("buffer emptied");
            endNotificationSent = true;
          }
        }
      }
    }
  }
}

void setup() {
  pinMode(13, OUTPUT);
  
  Serial.begin(9600);
}

void loop() {
  readSerialInput();
}

Thanks a lot!