PDA

View Full Version : Bug in Serial.println() ?



el_supremo
01-25-2013, 08:13 PM
This sketch behaves rather oddly using beta12 on Teensy3.


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

int count = 0;
void loop(void)
{
if((count/5) == 0) {
Serial.println("RMC A 2013/01/25 18:43:18.000 52 05.5686N - 106 35.7915W 9 A");
} else {
Serial.println("RMC A 2013/01/25 18:43:18.000 52 05.5686N - 106 35.7915W 10 A");
}
count++;
if(count > 9)count = 0;
delay(1000);
}

It should just repeatedly write the first string 5 times, then the second string 5 times etc.
The first string is 61 chars long and each one is printed as soon as it is generated. The other string is 62 chars long and five of them are written but none of them will appear on the Serial Monitor until the next 61 char string is printed which flushes the buffer.
Adding Serial.flush() doesn't help.
I don't think this is in the Serial Monitor itself because this doesn't happen when run on a Nano (also using 1.0.3).
When the 62 char string is written, println() adds CR/LF on the end which makes it 64 chars long.
If you change the second string from 62 chars to 126 chars, the same thing happens so it appears that writing a string that is an exact multiple of 64 chars is the problem.
Also, if you write the 62 char line 64 times, that will cause a flush of the whole lot, presumably because it has filled an internal 4096 char buffer.


Pete

t3andy
01-27-2013, 05:01 PM
Adding Serial.flush() doesn't help.
You have to use Serialx.clear();

http://forum.pjrc.com/threads/13017-Teensy-3-0-Serial-flush()-does-not-appear-to-work

el_supremo
01-27-2013, 05:43 PM
But why would I need to do that anyway? If I output lines that are not a multiple of 64 chars in length (including the CR/LF) then they are shown immediately on the Serial monitor. Lines that are a multiple of 64 chars are buffered.

Does the code I posted work as I describe?

Pete

PaulStoffregen
01-27-2013, 06:00 PM
Which operating system are you using?

el_supremo
01-27-2013, 06:54 PM
Windows 7 64-bit

Pete

el_supremo
01-27-2013, 08:40 PM
I wrote a Windows command line program to read the 62 char+CR/LF output of my GPS sketch. The windows program also soaks up 64 lines before displaying them all in one go but lines shorter or longer than that are displayed as they are sent.
So, it is a weirdness in Windows and not a bug in println, nor is it a bug in the Serial Monitor.

Sorry Paul.

Pete

PaulStoffregen
01-27-2013, 08:54 PM
I was able to reproduce the problem, at least partially, using Linux. My workbench is currently consumed a big pile of stuff for the WS2811 library.

You are probably right, this is likely a bug in my USB code. In fact, I think I know what it is.... but it's going to take me a few days to investigate. I need to change my setup to use a Widows test machine and the USB protocol analyzer.

PaulStoffregen
01-31-2013, 12:41 AM
Just a quick followup, I'm working on this today. It's complicated to fix.....

PaulStoffregen
01-31-2013, 02:11 PM
Here is a fix. Replace hardware/teensy/cores/teensy3/usb_serial.c with this file.

Please confirm if this fully resolves the problem?

el_supremo
01-31-2013, 03:36 PM
I've tested it with strings with lengths of from one to 79 chars and it works with all of them.
Thanks Paul.

Pete