Micro and while (!Serial)

Hello all.....

The Micro (or Leonardo) requires the while (!Serial) {;} thing to give it time to connect. That seems to me, by my experimenting, that the serial monitor actually has to be opened; right now it sits in the while until I open it. Fair enough....

But often one can still run a sketch without needing to see the output, yet it seems the sketch will not get through the while until a connection is established.

So two questions:

  • Is my Micro actually acting as it's supposed to by hanging around until it sees the monitor?, or should it be enough that it's actually physically plugged in on the usb
  • If it's working properly, how should I jump out of the while if I'm running with no connection? I'm thinking check millis() for the time elapsed since reset and if it's over a certain threshold, break?

Make that three questions:

It seems that opening a monitor on Micro does not reset the board, as it does on a Uno.... is that correct behaviour?

TIA,

Jim

I don't have a Micro but I do have a Leonardo. It can be a bit of a PITA.

You can make it reboot by opening the serial port at 1200 baud and then closing it again. The problem is that during the reboot period it will disconnect itself from the PC and the PC will only see the serial port again after a period of time - I think 12 secs is plenty.

As far as I know the if (!Serial()) does not require a connection to the PC - it is just an internal thing. And it is not essential - it is just a precaution.

If you have the Serial Monitor open and you press the reset button it will lose its connection and (AFAIK) when it restarts it will appear as a different serial port because the Serial Monitor is still sitting on the first one.

Not sure if that will get you anywhere - I have rather lost my way :slight_smile:

...R

I must say I was actually unable to ascertain a pattern in the way it works, it's a total pita as you say Robin.

The other day I happened to have device manager open while I uploaded a sketch and it made that Windows "pidunk" usb noise a couple of times and I saw the port number change in device manager.

But it seems erratic, at best. I'll try to spend some time nailing down the circumstances under which it does "this" or "that".

Is it documented anywhere? All I can find is this empty link.

I confess I don't use the Leonardo very often so I have not figured it all out. And I am using Linux which (AFAIK) deals with serial ports much better than Windows.

If I use the Leonardo with the Serial Monitor I normally close the monitor, press reset on the Leonardo and then open the Serial Monitor again. I sometimes put delay(5000); at the top of setup to allow me time to opening the monitor.

An added refinement would be to get the LED to blink twice at the start of setup() so you would know when to open the Serial Monitor.

Thankfully the code uploading process works without any trouble (or maybe it was unwise to say that :slight_smile: ).

I haven't seem a comprehensive documentation page. I guess I just picked up bits here and there with the assistance of Google.

...R

Well on my Mac it all works just the same an a Uno. :grinning:

Robin2:
I think 12 secs is plenty.

In computing terms that's, like, forever....

You did mean 12 seconds?

JimboZA:
You did mean 12 seconds?

Yes.
How long do you think it takes a Leonardo to complete its reset process and to make the serial port available on the PC?

I have no doubt it is a bit shorter than 12 secs - that's why I chose 12 - but not much.

...R