Tips for consistent serial output?

madmyers

Active member
I've found that I often need to physically power cycle my Teensy to get USB serial logs. I'm using the Arduino IDE. Has anyone run into this?

100% of the time I'll get output if I power cycle, much of the time I'll get no output if I just upload new firmware to the Teensy.

Any tips or best practices?

Only thing I'm doing is calling in setup

Code:
  Serial.begin(9600);

And then doing basic

Code:
 Serial.println("I AM ALIVE");

I've tried adding a Serial.flush() but that doesn't help.

Thanks
 
You should have a delay between Serial.begin() and the first Serial.print(), like shown below. You'll see this in most of the TeensyDuino examples. Without the delay, the println can execute before the USB serial is up and running and capable of output.

Code:
Serial.begin(9600);
while (!Serial && millis() < 3000) {}
Serial.println("I AM ALIVE");

Try not to delay solely based on Serial, as shown below, because if you don't have serial monitor open, this code will get stuck and you'll think your Teensy is broken.

Code:
Serial.begin(9600);
while (!Serial) {}
Serial.println("I AM ALIVE");
 
On some machines the Arduino IDE serial monitor can be flaky after a restart. Closing it and reopening it usually helps.
 
Thanks. I'd experimented with that delay -- didn't know why it was there. Good to know! Alas, that hasn't fixed my problem.

Does the Teensy USB code require anything to work? For example, interrupts must be enabled? Would doing a delay(99999); after some serial operations cause a problem?
 
On some machines the Arduino IDE serial monitor can be flaky after a restart. Closing it and reopening it usually helps.
Your post came in while I was writing the other. Thanks -- I was wondering if it's the IDE.

I'm running on a Mac (older Intel). Any recommendations for a solid serial read/write program?
 
Does the Teensy USB code require anything to work? For example, interrupts must be enabled? Would doing a delay(99999); after some serial operations cause a problem?
It may require interrupts, it depends how much data is currently being queued. delay() doesn't disable interrupts so it's not a concern.

I haven't used it myself but several people on the forum regularly recommend TyCommander for serial monitoring.
 
The Teensy might send stuff before the USB is ready for it. So most of my programs have something like:

Code:
while (!Serial && millis() < 5000) {}
Will wait up to 5 seconds for the Serial object to be ready. You can do it without the timeout, but have seen (and done it myself) where
you believe there is an issue as your program hangs when run on external power.

Serial monitor: Which version of Arduino IDE are you using? There are lots of things in IDE 2.x I like, however the Serial monitor is not one of them.
There are a lot of issues with it... Like you can only cut and paste from it, what currently fits on the screen...

So, for more hard-core things I second the use of TyCOmmander.
 
I've found that I often need to physically power cycle my Teensy to get USB serial logs. I'm using the Arduino IDE. Has anyone run into this?

100% of the time I'll get output if I power cycle, much of the time I'll get no output if I just upload new firmware to the Teensy.

Any tips or best practices?

Only thing I'm doing is calling in setup

Code:
  Serial.begin(9600);

And then doing basic

Code:
 Serial.println("I AM ALIVE");

I've tried adding a Serial.flush() but that doesn't help.

Thanks
Are you picking a "Teensy Serial Port" as opposed to a standard "Serial Port" in the Arduino IDE ??

Mark J Culross
KD5RXT
 
The Teensy might send stuff before the USB is ready for it. So most of my programs have something like:

Code:
while (!Serial && millis() < 5000) {}
Will wait up to 5 seconds for the Serial object to be ready. You can do it without the timeout, but have seen (and done it myself) where
you believe there is an issue as your program hangs when run on external power.

Serial monitor: Which version of Arduino IDE are you using? There are lots of things in IDE 2.x I like, however the Serial monitor is not one of them.
There are a lot of issues with it... Like you can only cut and paste from it, what currently fits on the screen...

So, for more hard-core things I second the use of TyCOmmander.
It's the 2.3.2 IDE.
I'm going to give TyCommnader a shot.
 
Yes. It's the dev/cu.usbmodemXXXXXXX (Teensy 4.1).

In the Tools > Port menu, does it appear twice? It should be "Serial Port" and also "Teensy Port". Do you get the same behavior with both?

You might also check your USB cable. When people have reported flaky USB, usually it's due to a long cable or unusual arrangement like multiple cables or adapters. Some cheap cables work well at 12 Mbit/sec but have issues with 480 Mbit/sec speed. If you have one of those, you could find usb.c (Arduino IDE installs it deep within your hidden ~/Library folder) and uncomment a line that force 12 Mbit/sec speed. It's been mentioned several times on this forum...
 
Back
Top