"While" Command

On a series of the
"while (!Serial.available()) {
}"
command to introduce parameters on the serial, the command does not stop when asked. It seems to jump a while command to the next. The day before the same program was working perfectly. Any hints. Thanks

post the full code or a sample code demonstrating what you mean.

is that really Arduino Command line tool related ???

Please post your sketch :wink: Please do not forget to use code tags as described in https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code-and-common-code-problems.

Your topic does not indicate a problem with the Arduino CLI and therefore has been moved.

Assuming you're using the CLI, which terminal program do you use? What are the settings related to <CR> and <LF>?

You can use the Serial.peek() function to see what character is available when you are expecting no characters to be available. The .peek() function is like .read() except that it does not remove the character from the buffer, so using it will not affect the rest of your code.
You could add a function like this:

void checkSerial() {
  if (Serial.available()) {
    char c = Serial.peek();

    Serial.print("Char waiting to be read: ");
    if (isPrintable(c)) {
      Serial.print("'");
      Serial.print(c);
      Serial.print("'");
      }
    else {
      Serial.print("code ");
      Serial.print(byte(c));
    }
    Serial.println();
  }
}

Then use it in each place in your code where the while getting jumped unexpectedly:

checkSerial();
while (!Serial.available()) {
}

1 Like

How did you asked? Did you ask polite? :slight_smile:

What's the program? Is the code secret?

@ardan
Because you are new in the forum, you definitely need to start with reading a guide:

Did you mean isprint()?

Note that the Serial monitor supports UTF8 so typing one glyph might result in multiple bytes in the Serial buffer and they would not be caught by the simple (useful) code you shared

https://docs.arduino.cc/language-reference/en/functions/characters/isPrintable/

They would, one character at a time. Individually those characters might not look like the original glyph that was typed, but it would be helpful when writing code to deal with such glyphs, if that was required.

1 Like

Did not know this one, it’s a wrapper to return a bool

1 Like

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