Bug in Serial.print?

ChrisRowland

Active member
I've found what seems to me to be a bug in the Serial.print() function, this code illustrates it:
Code:
void setup() 
{
	Serial.begin(9600);
	Serial.println("   abcd");
}

void loop(){}

I would expect that this would print the string I've provided, - " abcd" but what is printed is "abcd", without the leading spaces. It seems pretty generic, I found this after spending hours trying to debug a print number function that wasn't showing the leading spaces I was adding.

Chris
 
If this is the extent of your example - try the added code below - depending on the OS and the USB connection timing OP code may just be printing before USB is online.

Code:
void setup() 
{
	Serial.begin(9600);
	while (!Serial && (millis() < 10000)) ;
	Serial.println("   abcd");

	// For Fun
	Serial.println("...abcd");
	Serial.println("   abcd");
	Serial.print("...abcd");
	Serial.print("   abcd");
	Serial.print("::You are Here!");
}

void loop(){}
 
I'd made my example as small as demonstrated the problem but I guess you are suggesting that I should wait until serial is ready.

It doesn't seem to make any difference, this is what your code gives me:
Code:
Opening port
Port open
abcd
...abcd
abcd
...abcd   abcd::You are Here!

The only case where the leading spaces are printed is if something has already been printed on that line.

Chris
 
Indeed without Serial ready it isn't fair to expect any output.

Those spaces also showed from .print not .println.

Not sure what OS you have or what Terminal program you are using? Do you have a second terminal program for testing? Search forum for TYQT perhaps.
 
I had tried the same test but with a string that didn't start with a space and it was fine even without the delay.

I'm using Visual Micro Release (1603.20), Teensyduino 1.28, Arduino 1.6.8.

I guess that you think the problem may be the serial monitor.

And it is! The Arduino 1.6.8 IDE gives this output.
Code:
   abcd
...abcd
   abcd
...abcd   abcd::You are Here!

I wouldn't have expected that, still now I know. Shame, the Visual Micro is a much nicer IDE generally.

Chris
 
Thanks for getting back to me Paul.

No, the while loop made no difference, and even without the while loop a print without leading spaces worked correctly.

It looks as if I was mistaken and this is nothing to do with Print because the serial monitor in the Arduino IDE shows everything as I'd expect.

I posted about this on the Visual Micro forum and they are talking about a bug in their serial monitor in debug mode.

But the plot thickens - it's now healed! I see the leading spaces all the time now, even with VM. I've not knowingly changed anything but from what they say it may depend on the mode in which the serial monitor is running.

Chris
 
A (perhaps) similar subtle bug might exist on Arduino. If you have a board like Arduino Uno selected and you open the serial monitor, it will run Arduino's version of the serial monitor which doesn't have Teensy's extensions. Then if you switch to Teensy and upload, the window stays open and when Teensy reboots, (maybe) the window could still end up using Arduino's stuff that (sometimes) doesn't work quite right with Teensy.

I don't actually know if this really is a problem.... but it's on my list of stuff to dig into next time I work on Arduino IDE patches (which could be a while....)
 
Back
Top