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!
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.
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.
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!