Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serial.available() doesn't return 0 when no data has been entered into the Serial Monitor when the baud rate set higher than 9600 #1297

Closed
3 tasks done
Jarboer opened this issue Aug 5, 2022 · 1 comment
Assignees
Labels
conclusion: duplicate Has already been submitted topic: code Related to content of the project itself topic: serial monitor Related to the Serial Monitor type: imperfection Perceived defect in any part of project

Comments

@Jarboer
Copy link

Jarboer commented Aug 5, 2022

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

  1. With a new sketch open enter this code snippet:
void setup() {
  Serial.begin(19200);
}

void loop() {
  while (Serial.available() > 0) {
    Serial.println(Serial.available());
  }
}
  1. Upload the code
  2. Open the Serial Monitor and ensure it is set to a baud rate of 19200
  3. 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
@Jarboer Jarboer added the type: imperfection Perceived defect in any part of project label Aug 5, 2022
@kittaakos kittaakos added topic: serial monitor Related to the Serial Monitor topic: code Related to content of the project itself labels Aug 5, 2022
@per1234
Copy link
Contributor

per1234 commented Aug 5, 2022

Hi @Jarboer . Thanks for taking the time to submit an issue.

I see we have another report about this at #375.

It is best to have only a single issue per subject so we can consolidate all relevant discussion to one place, so I'll go ahead and close this in favor of the other.

If you end up with additional information to share, feel free to comment in the other thread.

@per1234 per1234 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 5, 2022
@per1234 per1234 self-assigned this Aug 5, 2022
@per1234 per1234 added the conclusion: duplicate Has already been submitted label Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: duplicate Has already been submitted topic: code Related to content of the project itself topic: serial monitor Related to the Serial Monitor type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

3 participants