How many ways to 'wait' for Serial monitor ready, is it different by architecture?

I have seen a few different ways to 'wait' for the Serial monitor to be ready, from simple delays after the begin to a couple of explicit functions. Here is my favourite, who has some other ideas?

  Serial.begin(115200);  // Remember to set Serial monitor to 115200 as well
  while (!Serial && (millis() < 5000));  // Wait up to 5 secs for Serial to be ready
  if (!Serial) {
    for (;;) {} // failed, loop forever
  }
  else
    Serial.println("Serial READY");

That construct is only useful for boards / processors whose main Serial port is native USB. If it's a UART followed by a discrete UART / USB converter, then it's pointless.

1 Like

That is why I have the && (millis() < 5000 so it 'delays' 5 secs.

Is this way longer than necessary? I guess I am an A-type when it comes to startup/reboot delays... I go so far as to remove the boot loader from a final final deployment.

It's not like you are giving a human enough time to fix anything that might need it.

Just curious. How long should it take worst case?

a7

use precompiler defines for the platform and use exactly that whats needed for the particular platform. Either none, wait, !Serial or what ever is needed.

Just remember to remove that if the project is independently powered without connection to PC USB. I can't even count how many times someone has showed up here with a (native USB Arduino) project that stopped working as soon as they used an extern power supply without a PC connection.

I have been looking for those, do you have a link?

YES, I forgot about that. Is there a way to tell programatically?

I just use a 1-2 second delay and move on with life. Never had a problem with or without PC. "Better" is the enemy of "Good Enough".

I have never heard of a definitive wait time. I stuck in 5000 knowing it was 99.99999% chance of being too long, but what is a guaranteed time?

not really. recently some of my ESP32 boards needed some time so I'm using something like

Serial.begin(115200);
#ifdef ARDUINO_ARCH_ESP32
delay(200); // just give Serial some time to start
#endif
Serial.println("\nFoo");

That technique would be a lot more useful if there was a way to test for every board.

Part of the answer was given in Arduino Nano V/S Arduino Nano Everye - #7 by sterretje and later.

I do not think that you will ever find a list of defines that are used during the compile process. The system is flexible and allows everybody to build board packages to their liking.

I think that your best bet is to analyse the verbose output of the compiler when you're compiling for the specific board. The defines depend on the core, the board and all kind of options that you have selected before compiling.

Here's an example of mine serialStr.

-jim lee

Based solely on your title and description, what method of waiting does this use?

No waiting. It just checks over and over grabbing bytes as they come available. When it sees a '\n', or whatever else the user chooses, it passes back the competed c string for the user to process.

Think about it. How happy would you be if you hired someone by the hour to do job x, and they spent 99.9999% of the time loafing about smoking cigarettes?

You have to purge the idea of stopping things from your mind.

I do set timers, so when mrs employee comes by, they can check to see if it's time to do something. Although I don't think this code uses any.

-jim lee

Ok, sorry but that has nothing to do with the Topic, this is about the setup code waitig to make sure the Serial port is ready. Your code sounds interesting for other uses though, what is the nae of the lbrary?

Oh! I missed the point entirely. Sorry about that. I'm so used to people asking to stop the processor. Oh well, it was fun while it lasted.

And yes, this waiting for Serial can be a pain. I don't know if you ever run Teensys, but their serial stuff seems to run on it's own timeline. That can make debugging a real trick.

-jim lee

1 Like

Wow, I'm going at too fast baud rate today.

The library is called LC_baseTools, you find it in the library manager.

-jim lee

Installed and Slow Servo, I will try to check them out this decade but no promises, now where did I put my list of lists said the autistic 82 yo?