Examples online always show this code to read bytes from the serial interface:
while(serial.available() > 0)
{
char receivedByte = serial.read();
}
But I don't understand why it works.
The read() operation "removes" a single byte from the queue, so the while loop should consume all the bytes from the queue.
However, what guarantees that the incoming bytes are being pushed into the queue faster than the consuming process? I would imagine that any Arduino board is order of magnitude faster than a standard serial transmission, so it would starve the queue and effectively exit the while loop even though there are more incoming bytes to process. What am I missing here?
byte
is a data type in the Arduino environment, better don't use it as a variable name.if
block would make more sense than awhile
block