Description
Describe the problem
I was working on a program that requires the use of Serial.available()
and it was working fine with the baud rate set to 9600. However, I started running into an issue when increasing the baud rate to 19200 or higher (with both in the code and the Serial Monitor set to the same). The issue is that Serial.available()
returns 2 instead of 0 when called without entering anything into the Serial Monitor. This also happens regardless of whether you change "New Line" to any of the other options including "No Line Ending". I can easily replicate the issue with the following code:
void setup() {
Serial.begin(19200);
}
void loop() {
while (Serial.available() > 0) {
Serial.println(Serial.available());
}
}
Something else I noticed is that when running that snippet if you change "New Line" to any of the other options, the value returned increases from 2 to 8 to 14. Not sure if that is related.
Another oddity is that if I change the code to:
void setup() {
Serial.begin(19200);
}
void loop() {
Serial.println();
while (Serial.available() > 0) {
Serial.println(Serial.available());
}
}
Then it works correctly and the buffer will decrease (even if you enter data) if you read from it like this:
void setup() {
Serial.begin(19200);
}
void loop() {
Serial.println();
while (Serial.available() > 0) {
Serial.println(Serial.available());
Serial.read();
}
}
However removing the Serial.println()
line and running the modified code doesn't work the same way. The buffer doesn't reach 0 like it was before with the Serial.println()
line included.
To reproduce
- With a new sketch open enter this code snippet:
void setup() {
Serial.begin(19200);
}
void loop() {
while (Serial.available() > 0) {
Serial.println(Serial.available());
}
}
- Upload the code
- Open the Serial Monitor and ensure it is set to a baud rate of 19200
- Now with it running you'll see without entering anything to the Serial Monitor the issue described is occurring
You can also try changing the baud rate to 9600 in the sketch and Serial Monitor. With just that change the code now works correctly and only returns a number greater than 0 if you enter data in the Serial Monitor.
Expected behavior
I would expect that no matter what the baud rate is set to in the IDE 2's Serial Monitor that Serial.available()
would return 0 unless there was data in the buffer from you entering it.
Arduino IDE version
2.0.0-rc9.1-nightly-20220805
Operating system
Windows
Operating system version
Windows 10 Pro - OS build 19044.1826
Additional context
I tried running the same code snippet in the IDE 1.8.19 version and it works correctly without reuploading the code. Also, using another serial port monitor like PuTTy works fine too with the same code uploaded from the IDE 2. So it seems like the issue lies in the Serial Monitor at least to me.
Issue checklist
- I searched for previous reports in the issue tracker
- I verified the problem still occurs when using the latest nightly build
- My report contains all necessary details