PDA

View Full Version : Serial port/monitor problem?



el_supremo
11-07-2012, 05:00 PM
I've been having problems getting serial data printed on the serial port with a Teensy 3 - I'm now using beta7 but it also occurred in the previous version. Perhaps I just misunderstand the way Teensy works.
I have checked device manager to ensure that the correct port (COM9) is being used and it is also selected in the Tools menu. The sketch I'm working with works fine on an Arduino Nano and it prints the initial state of the sketch on the serial monitor.
But with Teensy nothing shows up. I have reduced the problem to a sketch which just prints a counter starting at zero.

This code:

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

int counter = 0;
void loop()
{
Serial.println(counter++);
delay(1000);
}

starts at 6 in the Serial monitor!
If I kill the Serial monitor and start it up again, the program is still running so it starts at, say, 11. This is a big difference between the way Teensy works compared to Arduino. On Arduino, restarting the Serial monitor also restarts the Arduino so that the sketch starts by printing 0. But with Teensy, it can take several seconds (*) before you get the Serial Monitor loaded so you miss whatever serial output occurred up to the point that the monitor started.
(*) Another thing I've noticed is that when I start a Teensy sketch and then click on the Tools menu to try to start the monitor, it can take several seconds for the menu to drop down with the options - this occurs in beta7 and previous.

Am I just not doing things properly or is this a problem with the Teensy environment? How do I run a sketch so that I see everything that it prints right from the start?

Pete

JBeale
11-07-2012, 05:51 PM
I also have trouble sometimes seeing the first few lines printed from Serial.println(). I often put a few seconds of delay in the start of the program, to enable me to get the serial monitor working. Teensy3 is different from Arduino in that device programming happens over a different USB channel (HID, not serial). Also, the virtual serial device that Serial.println() sends data to, is created via software and hardware inside the Teensy ARM chip itself, so the virtual serial port does not exist until the Teensy program starts operating (unlike the always-on, dedicated FTDI Serial-USB chip in Arduino). In addition, the Teensy USB-Serial implementation discards data after some number of milliseconds of non-response from the PC host side (eg. serial terminal not open or not connected) but I think that may be similar to what FTDI chips do also? (they can't buffer it forever).

Yes, unlike Arduino, Teensy 3 does not reboot when the serial port is opened from the PC side. Paul mentioned that he was considering emulating that behavior in one of his Kickstarter updates, but it hasn't been done yet. I have some of the "Seeeduino" boards (Arduino clone) from Seeed Studio which has a physical switch to select Auto-reboot or manual reboot, and I like having the ability to choose.

el_supremo
11-07-2012, 08:03 PM
Thanks for the info. Now I understand :-)
What I will do with my programs which use/need the serial monitor is I've added a button to the breadboard and those sketches will wait until I push the button before they start up.
BTW the PushbuttonPullup sketch in the Teensy examples doesn't set the pullup. It has "pinMode(8, INPUT);" which should be "pinMode(8, INPUT_PULLUP);"

Thanks again.
Pete

Paul
11-07-2012, 08:48 PM
starts at 6 in the Serial monitor!
If I kill the Serial monitor and start it up again, the program is still running so it starts at, say, 11. This is a big difference between the way Teensy works compared to Arduino.


Yes, this is a known problem and it's definitely on my to-do list.

There's more info about this on Update #19.

http://www.kickstarter.com/projects/paulstoffregen/teensy-30-32-bit-arm-cortex-m4-usable-in-arduino-a/posts/331757

Scroll down to the "Missing Features, Coming Soon..." section and look for the part about Arduino reset emulation. Teensy 2.0 has emulated the 2-chip Arduino reset for a couple years, but it was quite difficult to implement. I simply haven't done it yet for 3.0... but I will.

Incidentally, Arduino Leonardo and Due (when used on the native port) also suffer exactly this same problem. Arduino's workaround, which I don't really like, is to add while (!Serial) /*wait */; at the top of setup(). In the short-term, that might be a possible workaround.

I absolutely will implement the reset emulation. But please understand it is quite difficult to do with a single chip board like Teensy. Getting it right on Teensy 2.0 took quite a lot of work. Teensy 3.0 has a completely new USB stack which is based on DMA, so it's going to take some serious work to do this properly.

JBeale
11-07-2012, 09:41 PM
Good to hear Paul- if it isn't too much additional work, it would be nice to have the option to selectively turn the auto-reset function on or off on a per-program basis.

Frollard
11-07-2012, 09:54 PM
I use the code from arduino that makes sure the soft serial is actually started for Leonardo;



//in setup
Serial.begin(57600);
while (!Serial) {} //wait for serial


It has the effect of delaying the whole program until the serial monitor is connected if usb is connected.
Edit: I see Paul already beat me to it :)

Paul
11-07-2012, 10:14 PM
would be nice to have the option to selectively turn the auto-reset function on or off on a per-program basis.

On Teensy 2.0, the auto-reset only works with the Arduino Serial Monitor. Opening the port from a regular terminal emulator doesn't cause the auto-reset. At least that's the simple version....

When Teensy 2.0 is a serial device, the auto-reset is done by setting the baud rate to 150 baud. Teensyduino adds a tiny patch to the serial monitor so it sets the baud rate to 150 momentarily while opening the windows, if the board is Teensy. Normal programs don't do this, but if you really wanted to auto-reset behavior while opening a new session, all you'd need to do is set the baud rate to 150 for a moment (with USB virtual serial, the communication is always full USB speed... the baud rate is just a parameter sent to the device, in case it is a USB-serial converter that needs to know the desired baud rate).

When Teensy 2.0 is a non-serial device, a serial channel is emulated using HID and the teensy_gateway program. Teensyduino patches the serial monitor to talk to the teensy_gateway program. The auto-reset is sent as a HID feature request. There's some complex logic to figure out if the gateway is running or a separate utility needs to be launched to do this (java doesn't support any of this native USB stuff). There's quite a lot of work behind-the-scenes to try making Teensy 2.0 work just like you'd expect from an Arduino Uno, even though it's a single chip design running something completely different than serial.

I'll do the same on Teensy 3.0. Please understand this is a lot of work. It didn't happen overnight on 2.0 either.

el_supremo
11-07-2012, 11:32 PM
I'll do the same on Teensy 3.0. Please understand this is a lot of work.
Yep, no problem, Paul. Now that I understand why it is happening I can workaround it.

Pete