Why are logs lost after Serial.isConnected() toggles true?

A while back, I created a program that wrote logs to the Serial console and I noticed that the first few log entries were not being displayed. I fixed that by adding a blocking delay before the first logs were written. Later, I added another line of code to setup(), and I immediately noticed that the original problem had returned. An increased delay solved the issue, but puzzled me because the added line came after the blocking delay and after the first few lines were generated. Testing proved that removing the added line allowed me to reduce the delay, and adding the line forced me to increase the delay.

After finishing that code with the increased delay, I decided to go back to basics … to see if I could figure out was going on. I took the following example directly from Particle documentation … and “Hello there!” does not print.

void setup()
{
  Serial.begin();   // open serial over USB
  while(!Serial.isConnected()) // wait for Host to open serial port
    Particle.process();

  Serial.println("Hello there!");
}

The example works when I insert a delay(2000) … I tested smaller delays in 100ms increments … nothing smaller worked, including delay(1900).

void setup()
{
  Serial.begin();   // open serial over USB
  while(!Serial.isConnected()) // wait for Host to open serial port
    Particle.process();
  delay(2000);
  Serial.println("Hello there!");
}

The example also works if I insert a Serial.println(); and a delay(500) … which seems odd since printing an empty line clearly does not take 1.5 seconds and a full 2-second delay is required when I don’t print it.

void setup()
{
  Serial.begin();   // open serial over USB
  while(!Serial.isConnected()) // wait for Host to open serial port
    Particle.process();
  Serial.println();
  delay(500);
  Serial.println("Hello there!");
}

While the above scenario is not exactly like what I saw the first time around, I find it equally puzzling so I decided to reach out before I disappear down the rabbit hole.

  • Why is a delay required after Serial.isConencted() toggles true?
  • Why does the required delay vary based on code changes like the above?
  • Could it have anything to do with my using TeraTerm VT running on Windows as my Serial console?

I’d think the most likely answer is that the OS needs some time to fully register the USB device.
The device knows it’s connected as soon the initial USB packet is received, but at that time the USB enumeration is not finished and then any terminal program may need some incentive to effectively open the port too.

1 Like

That makes sense for the case I was able to document. I’ve decided to create a personal library to aid in prototyping and for testing things in multiple scenarios. The library’s first method will be:

void openSerialConsole(char* title) {
    Serial.begin(115200);
    while (!Serial.isConnected()) Particle.process();
    Serial.println(); // send a wake-up call to the serial console
    delay(500);       // give  the serial console time to wake up
    Serial.print("\033[0H\033[0J"); // clear the (vt100 compatible) serial console
    Serial.printf("%s ---------- %s\r\n\n", title, (const char*)Time.format(Time.now(), TIME_FORMAT_ISO8601_FULL));
}
2 Likes