Yun Serial Port Behavior : while (!Serial)

I've noticed with my Yun that if I don't have the serial console opened, my Arduino sketch will not run.
Once I open the conlsole, it then proceeds.

I'm using the "while ( !Serial )" construct as described in this page: if(Serial) - Arduino Reference

void setup() { 
 //Initialize serial and wait for port to open:
  Serial.begin(9600); 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

The page also states that:
"On the Leonardo, if (Serial) indicates whether or not the USB CDC serial connection is open. For all other instances, including if (Serial1) on the Leonardo, this will always returns true."

So, I believe that this is related to my problem. Is the Yun actually behaving like a Leonardo?

If this is what is actually happening, how can I run my sketch without the serial console being opened? I've tried commenting out the "while (!Serial)" statement, but this doesn't seem to have helped. I have several "Serial.println("....");" statements in my code that I use for debugging. I'd really prefer to not have to add and remove comments just to enable and disable the debugging output.

Any help would be greatly appreciated.

-roejack

Roejack:
So, I believe that this is related to my problem. Is the Yun actually behaving like a Leonardo?

Yes, that is exactly your problem. The Yun is a Leonardo with a Linux WiFi/Ethernet router attached to the Serial1 pins (DO and D1). The "Arduino" side of it behaves exactly like a Leonardo.

I've tried commenting out the "while (!Serial)" statement, but this doesn't seem to have helped. I have several "Serial.println("....");" statements in my code that I use for debugging. I'd really prefer to not have to add and remove comments just to enable and disable the debugging output.

Yep, you've hit the conundrum. Keep the "while (!Serial)" in your sketch, and it won't run without the serial port being opened. Take it out, and you lose any initial debug messages that occur before you can make the connection.

This is further complicated by the fact that the Yun/Leonardo doesn't use a serial to USB converter chip, it uses the USB interface that is built into the processor. This has advantages in that it can act like other devices, such as a keyboard or mouse, but it has the disadvantage that the USB connection goes away each time you reset the processor, and is not available until the host computer re-enumerates the USB port and establishes a new serial connection. So you can never see the initial debug output without the "while (!Serial)" statement.

It's not a problem to be sending things out the serial port before you have a connection, so you don't have to disable all of your debug output. But you do have to either disable the while statement and miss your initial debug output, or work around it.

If you have free I/O pins, you could use one of them as a debug indicator: set the pin as input with pullup. After that, check the pin, and do the "while (!Serial)" only if the pin is low. With the pin disconnected, the pullup will make the input high, and the sketch won't wait. But if you want the debug output, put in a jumper wire to pull that pin to ground, and then the sketch will wait until you make a connection.

A trick I've done is take out the wait, but put a "if (Serial)" test in loop(). If this is the first time it was seen as true (I use a boolean flag to determine that) I print out a sign-on banner and initial sketch information (which was saved in local variables if necessary.) That way, the sketch starts and runs, but when I connect to the serial port, I still get that initial saved output. The downside is that this only works once: there is no way to know that the USB serial port has disconnected, so the initial information only prints out once, not every time a connection is established.

Thanks ShapeShifter, this is very helpful. I took your idea and put it into a function that does the checking. I then removed my while(!Serial) statement and replaced all of my "Serial.Println("xx") statements with this function: Msg("xxx"). And that works. Now if I don't have the serial console opened, my Yun sketch continues as it should, when I open the console, I then begin receiving the output.

void Msg(String msg)
{
	if( Serial ) {		
		Serial.println(msg);
	}	
}

Thanks again,
-roejack

Printing to the Serial port when not connected should not be causing problems. Anything you do print while not connected will simply be lost.

You really only have to check if Serial is true if you want to wait for a connection so you don't miss any of the early output. If you want to see that initial output before you connect, you have to jump through hoops. If you just want to see the output when you're connected, and ignore/lose it when not connected, then don't do anything special, just print to the Serial object.

The caveat is that if you are looking at the Tx/Rx LEDs to tell you if the sketch is running, that won't work: the LEDs won't start blinking until you have actually connected at least once. Before that first connection, the sketch is still running and printing to the serial port, but the Tx LED won't blink.

If you want a reliable LED to tell you the sketch is really running, use the LED on D13.