Why does Serial.available() not return 0 without entering any data into the Serial Monitor when the buad rate is higher than 9600?

Hi everyone, I'm new to the forms but have been tinkering around with Arduinos for a while now. As the title mentions I'm facing an unexpected issue with Serial.available() and I can't find any information on how to resolve this issue.

The issue only started happen when I increased the baud rate from 9600 to 19200 or higher (both in the code and the Serial Monitor). The reason I did this was because sometimes the text output to the Serial Monitor would be slow in my project so I increased the baud rate but than ran into this issue.

I'm using Serial.available() to check if there's any data in the Serial buffer for Serial.parseInt() to read data but because Serial.available() is acting abnormally I can't read it without facing issues since the buffer is actually empty. For me Serial.available() always returns 2 instead of 0 when I don't enter anything into the Serial Monitor. With the exception of adding Serial.println() before I call Serial.available() but that is not an ideal work around for me as it adds new lines to the Serial Monitor every loop (also the fact that that causes it to work is very weird).

I'm at a complete loss because I can't figure out why this is happening and how to fix it. It also messes up my code since because Serial.available() doesn't return 0 the usual check if (Serial.available() > 0) will always equal true which is causing parts of my code to run that shouldn't (like to see if the value is a byte). I've tried narrowing it down and doing a basic sketch but I still face the issue even with this minimal code:

void setup() {
  Serial.begin(19200);
}

void loop() {
  int serialAvailableVal = Serial.available();
  if (serialAvailableVal > 0)
    Serial.println(serialAvailableVal);
}

I have tried a few things to resolve it to on my own but to no avail. I've tried clearing the buffer first with Serial.read() but it still returns 2. I've tried Serial.flush() before calling it as well. I also tried using this code but that only partial works; since if I enter "123" then the Serial Monitor will return something like "����123" and I checked the baud rate, it is correct.

I'm using an Arduino Uno and Windows 10 if it matters. I also choose to omit my actual project as the above code replicates the issue I'm facing and my project is very large and requires connection to my local server to work properly (due to making GET and POST requests). If anyone can help me figure out how to resolve this, so that Serial.available() returns the correct value, I'd really appreciate it! Thanks in advance!

Try printing the "int" values of the characters received.
Look out for values like 10 and 13. (Line feed and carriage return)

1 Like

That only makes sure stuff you've printed has gone to the output.

I suspect you have the Serial Monitor line ending set to "Both NL & CR". That would explain the two characters left in your buffer after .parseInt().

You could filter those out with:

  while(Serial.available() && (Serial.peek() == '\n' || Serial.peek() == '\r'))
    Serial.read() ;

Put that before your if (Serial.available()) to toss out an NL or CR characters at the start of your input buffer.

1 Like

Ok, if I do this in my loop:

int serialAvailableVal = Serial.available();
if (serialAvailableVal > 0) {
  Serial.println(Serial.read());
}

Then it prints this:

255
��j
255
��j
255
��j
255
�SSHH��j
��j
255

And if I do this:

int serialAvailableVal = Serial.available();
while (serialAvailableVal > 0) {
  Serial.println(Serial.read());
}

It prints -1 repeatively.

The robust and non-blocking methods for serial input in the serial input basics tutorial may be of interest.

1 Like

because after first reading your serialAvailableVal contains outdated value.

Try read Serial.available() inside of while() condition:

1 Like

I actually had it on new line but unfortunately that still doesn't help. I mentioned in my OP that I tried adding Serial.read() (both one time and two times before calling Serial.avaiable()) but it still returns 2.

I still tried changing to "Both NL & CR" and adding that line to my code like this:

while (Serial.available() && (Serial.peek() == '\n' || Serial.peek() == '\r'))
  Serial.read();
if (Serial.available() > 0) {
  int temp = Serial.parseInt();
  Serial.println(temp);
}

But it is still getting to the parseInt() method and returning 0 which it shouldn't do.

Unfortunately that doesn't change anything. Changing it to:

while (Serial.available() > 0) {
  Serial.println(Serial.read());
}

Just causes it to still print:
��j
255
��j
255
�SSHH��j
�5
255

If you aren't entering data into Serial Monitor then it should not be showing output so something seems to be wrong somewhere.

Yes, '-1' is the expected value when you call Serial.read() when the buffer is empty. Perhaps it is showing -1 so fast that you didn't see the one character that started the while() loop.

Shouldn't the while loop stop as soon as something is read?

If we talking about last code from post #5 - definitely not

I'm looking at post 9

Ok this is going to be dumb and I guess I should have mentioned it in my OP but I'm using the beta IDE 2.0. And it seems like the issue is related an issue in the Arduino IDE 2.0. It was working so well for me that I didn't think it would be the issue but I guess I should have tried it in the non-beta version before posting. Using the Serial Monitor in the non-beta IDE allows it to work just fine. So I guess I have a bug report to submit, and I'm not facing an issue with my code. Sorry for not trying that before everyone. Thanks everyone for your help, I really appreciate it!

I thought the 2.0 IDE had its own forum section . . . no?

I'm unsure but I didn't originally realize that was the issue so that's why I posted it here. Sorry for the confusion.

Only for problems with the IDE.

3 posts were split to a new topic: Serial.available() returning unexpected results

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.