How to prevent the Serial Monitor from closing upon re-uploading sketch? I'll be teaching a year 10 class on the Intel Galileo gen2 and there'll be a wide (but high-level) programming curriculum, which includes introduction to console printing, using Serial.

I'd like the students to be able to write their printing code in setup() so that it's only printed once and easily readable by them. However, it's impossible to be fast enough to upload a sketch and open the Serial Monitor (which flat out refuses to open before the sketch has started), and the Serial Monitor closes itself upon re-uploading code.

Without unnecessary (and potentially for the students, confusing) use of the delay function in setup(), it's impossible to read any output originating from setup(); only things printed in loop() (which are constantly re-printed) are ever discoverable.

So; is there a way to somehow keep the Serial Monitor open, or make it capture output from before it's opened, or delay the entire running of the sketch (rather than chucking in delays)?

We'll be using the standard Arduino IDE.

EDIT

This script was run in one of two conditions...

void setup() {
  Serial.begin(9600);

  // ensure Serial is ready
  delay(1000);

  for (int i=0; i &lt; 10000; i++) {
    Serial.println(i);
  }
}

void loop() {      
}


When the Serial Monitor was opened immediately after upload, all numbers from and including 0 to 9999 were recorded in the monitor.  

When the Serial Monitor was opened around 5s after upload, only numbers upward of 2243 were observed. This suggests the sketch WAS being re-run (else no numbers would have been captured; the sketch runs in significantly less time than 5s) but that all numbers between 0 and 2243 were not captured by the monitor; as if opening the Serial Monitor late enough to cause a re-run causes some monitor overhead.
This strange loss of numbers is NOT time dependent: increasing the initial delay from 1s to 4s (and waiting longer after upload before opening the Serial) did not at all vary the soonest number recorded, suggesting it is some 'line' based overhead
(but of course, how could anything fail for 2243 lines?!)

With that mystery aside, this negates the automatic re-running of the sketch upon Serial Monitor opening from being actually useful.
I assume you are uploading the code over USB - the same USB that you want to monitor for output from the sketch? If so, the serial monitor has to release the port for the uploader to be able to use it, and vice-versa, as your computer's OS will only assign a USB port to one application at a time.

It may seem like the IDE is one application containing an uploader and a serial monitor, but the IDE is not much more than a "pretty face" that runs separate compiler and uploader applications on your behalf.

Several work-arounds are possible (although I think a delay() statement in setup() is the clearest of the choices):


Add a delay statement in the setup function. If you must this, you can put it in main() just before the setup() call, but you'd take away a learning opportunity.
Use a separate hardware or software serial channel for the monitor which you could leave connected and wouldn't interfere with uploading;
Use the loop() function instead, and put a forever loop at the bottom to prevent it returning, thus making it a runs-once function.