How to see first Serial.print outputs?

Hello!

When the Photon starts up, I have some serial outputs I’d really like to see in the setup() section, but it takes a while (5-10 seconds) until the device can be read…
I try to read the Serial output with this:

#!/bin/bash
DEVICE=/dev/ttyACM1
stty -F $DEVICE 9600
cat $DEVICE

But get this for several seconds until it is available:

stty: /dev/ttyACM1: Device or resource busy
cat: /dev/ttyACM1: Device or resource busy

Is there any way to connect to the serial device as soon as it becomes available and so see all of the serial output? (I’m running on Linux)

Thank you for your help! :slight_smile:
-Robin-

You could put a delay before the first prints, so you have time to connect. Alternatively, you could have it wait until you input something over serial before continuing. Either should work.

Hmm… I guess, but both of those options require me to flash a “debug” version and then flash again after - not a great workflow.

If it’s for debugging purposes, it does make sense somehow. If not, then what exactly do you need the serial output for that soon? Any reason the device should start immediately, since a three second delay is often more than enough to connect with serial. The setup is progressed through incredibly fast, and you connecting before it finishes is rather rare. I haven’t got enough experience with Linux to know if there are options to do so automatically, but other than that, the delay works.

There is a similar Arduino method that will let you call to test if Serial is connected like this:

Serial.begin(MY_BAUD_RATE);
do
  {
    Particle.process();
  } while(!Serial);

it compiles on my Particle IDE, but I am not sure that the method works… you could try it.

If you have a free IO pin, a technique like this can be handy if you want to see your Serial.prints during setup during development. If D6 is left unconnected, the code runs through setup normally. If you tie D6 low, then setup either waits until you hit a key over serial, or for 10 seconds, whichever comes first.

void setup() {
	Serial.begin(9600);
	pinMode(D6, INPUT_PULLUP);
	if (digitalRead(D6) == LOW) {
		// Only check for waiting for serial when D6 is pulled low
		unsigned long waitStart = millis();

		// Wait for 10 seconds or until a key is pressed
		while(!Serial.available() && ((millis() - waitStart) < 10000)) {
			Particle.process();
		}
	}
	Serial.println("exiting setup");
}

Thank you - this looks like a really good solution for me! :smile: