Hello. Is the RX buffer completely cleared when Serial.readBytes and Serial.readBytesUntil functions are used? Or is it cleared up to the byte read?
Simple answer No, not necessarily
Did you read the documentation? That explains things pretty well.
I've looked at these and my question has no answers. For example, 20 bytes of data came to the RX buffer. However, instead of reading all of this data, I read the first 10 bytes I needed using the Serial.readBytes or Serial.readBytesUntil functions. Will all data in the RX buffer be cleared at the end of this, or is it just the first 10 bytes I read?
If 20 bytes are received, and you read 10, then there will be 10 still in the buffer. Best way to confirm this sort of thing is to try it out for yourself...
The following diagrams (Fig-1, 2) may help you to get answer to your queries.

Figure-1:
After reading 10-byte data, the Serial Buffer of Fig-1 will appear as Fig-2 shown below.

Figure-2:
So, it is clear from Fig-2 that there are still 10-byte data left in the Serial Buffer to read. Note that the Serial Buffer is a FIFO-type buffer (First-in First-out) which means that the data byte that has entered at the last will come out first. Location-0 holds the data item that has arrived at the very first.
Maybe there is a more efficient way to do it, but when I have had the need to clear the RX buffer, I did something like this:
while(Serial.available()) {
Serial.read();
}
You might add a short delay to ensure the while loop does not exit while data is still being received if that is possible in your application.
I did a few tries and found that these two functions do not completely clear the rx buffer. Only the read bytes are cleared. Unread bytes still remain in the rx buffer. Thanks to everyone who helped.
Try the following sketch and see that the whole buffer gets emty.
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte n = Serial.available();
if (n == 5)
{
do
{
(void)Serial.read();
}
while (Serial.available() !=0);// -1); edit
Serial.println(Serial.read()); //shows nothing
}
}
Which core are you using? I thought Serial.available() would never return less than 0:
Well, if you have a perpetual do ... while loop then it will never execute at all. But if no data is available then it would be expected to print -1:
https://www.arduino.cc/reference/en/language/functions/communication/serial/read/#_returns
Returns
The first byte of incoming serial data available (or -1 if no data is available). Data type:
int.
It is Arduino UNO for ATmega328P MCU.
You are right!
If Serial.available() function returns 0 (indicating that there is no data byte available in the Serial Buffer to read), then Serial.read() function will return 16-bit int (0xFFFF = -1). //edit
When the receive buffer is empty, Serial.read() always returns -1. That's 0xFFFF where int is 16 bits and 0xFFFFFFFF where int is 32 bits. It's possible to receive an actual byte with the value 0xFF (which is not -1), so the function must return a value with a type that's bigger than a byte. In this way, Serial.read() is the same as Standard C's getchar() function (it also returns an int, not char)).
Yes! it returns -1 after the execution of the following sketch:
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte n = Serial.available();
if (n == 5)
{
do
{
(void)Serial.read();
}
while (Serial.available() !=0);// -1); edit
Serial.println(Serial.read()); //shows -1
}
}
-1
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.