Why, if I set a serial speed above 9600, serial text gets garbled?

I wrote a small sketch that does just this:

Serial.println("HAHA");

in loop forever... It works if I do Serial.begin(9600) or speeds below, but if I set the speed to something higher I get strange characters in the serial monitor... (even setting the baud rate to the appropriate one). Why?

but if I set the speed to something higher I get strange characters in the serial monitor...

Try adding a small delay at the end of loop. The loop() function is called millions of times per second. You are jamming serial data out faster than is reasonable.

Don't quite follow the logic of that - data will only be written to the transmit register when it is empty, effectively a busy wait inside Serial.print

Don't quite follow the logic of that - data will only be written to the transmit register when it is empty, effectively a busy wait inside Serial.print

I understand that. I'm simply interested in whether an additional delay in loop() masks the problem. I don't think that it should be necessary.

On the other hand, having Arduino send the same message over and over again, as fast as it can, seems pointless.

wrote a small sketch that does just this:

Please post the complete sketch, as there might be some other error e.g. calling Serial.begin() in the wrong place, parameter error etc.

Have you tried another cable e.g. from your printer?

I wrote a small sketch that does just this:

Probably best to post all of your sketch.

Are you using the regular serial port on the ide? If your not using 9600 you need to select which rate on the bottom of the screen, it doesn't automatically know

The full sketch is no more available as I rewrote it a thousand times :slight_smile: But don't worry I'm a professional programmer, this is easy stuff for me. Serial.begin is called in the setup() handler and everything is done in the loop.

Anyway there is some confusion if print and println are blocking or not. Of course I assumed it to be blocking. Isn't it??

I'm using USB with my computer. So it is weird it cannot reach high baud rates... it would have been understandable with other devices but usb-serial should be fast.... Am I wrong?

No way it works like that even with another cable :frowning:

The full sketch is no more available as I rewrote it a thousand times But don't worry I'm a professional programmer, this is easy stuff for me.

So what are you doing in this forum? Lonely? :slight_smile:

The way you appear to be using it Serial is blocking after the first two characters.

So you are saying that this

void setup () {
   Serial.begin (19200);  // EDITED, was 9600
}

void loop () {
  Serial.println ("HAHA");
}

Doesn't work?


Rob

No, Rob. If over 9600 baud it doesn't work, supposedly.

The full sketch is no more available as I rewrote it a thousand times ...

Oddly enough, I find this hard to believe.

You did a thousand variations of:

void setup () { Serial.begin (115200); } 
void loop () {   Serial.println ("HAHA"); }

You must have a better imagination than me to manage that many.

Then you deleted all of them.

Then you approach this forum without a single piece of code posted, asking for help?

if I set the speed to something higher I get strange characters in the serial monitor ...

Can you be a tiny bit more specific?

What "something higher"? The number?

What strange characters? Copy and paste.

Are these strange characters at the start, or in the middle? If in the middle how far in? Instead of HAHA what do you see?

Anyway there is some confusion if print and println are blocking or not.

Without seeing your code it is hard to know whether you used hardware or software serial.

It is also hard to guess what board you are using. We also don't know what operating system you are using. Windows? Mac? Linux?

over 9600.

Oops, post edited.


Rob

@Nick, I don't know what's so strange. I did the basic serial example, I found the "speed limit" problem, then I went on playing with my Arduino... Anyway I've that problem both with the early example and with a more elaburated program I am writing now.

For the rest you are right: I should give more details:

Board: Arduino Duemilanove (not original, a chinese clone).
SO: Windows 7, 64 bit

Software or hardware serial? You mean on my pc? On my pc I am using the usb cable to connect to the arduino board. Thus is a Usb-Serial thing.... I guess this is what you call "Software serial". On the arduino I am using just Serial.setup and Serial.println on a duemilanove.

I find the "garbled text problem" both on the serial monitor in the Arduino IDE and in Java programs that I wrote (yet using RXTX as the arduino IDE). Now I can't copy and paste (because I'm away from home using somebody else pc). Anyway if the speed is, for instance, 14400, 28800, I don't get HAHA, but I get things like squares and random chars. It seems to me that writing to the serial goes out-of-sync. Is there a way to enforce syncing? Maybe using write?

It is strange that nobody knows for sure the blocking semantics of print and println for Serial. There is no word about that on docs.. which is odd.

It is strange that nobody knows for sure the blocking semantics of print and println for Serial. There is no word about that on docs.. which is odd.

That's not true, and if you'd read all the responses in this thread, you'd know the answer.

There is a register that holds the value to be/being transmitted. When that register is in use (i.e. has a character in it), Serial.print() needs to wait (i.e. block) until that register is free. So, Serial.print("HaHa") will block until the last a is in the register, then return.

Serial.print("a") may, or may not, block, depending on when it is called with respect to the last Serial.print(), Serial.println(), or Serial.write().

Serial.println(), of course, always blocks, since it is outputting a minimum of three characters (the one or more it is called to output, the carriage return, and the line feed).

Serial.write() may or may not block, depending on how many bytes it is called to output. One byte may or may not, depending on the timing of the call, as in the Serial.print() case.

Serial.write() for more than one byte will block, just as Serial.print() does.

The behavior of Serial.print() is dependent on the behavior of Serial.write(), since all calls to Serial.print() eventually end up calling Serial.write() to do the actual output.

None of this is relevant to your problem, though, since the garbling is not caused by whether or not Serial.write() blocks.

dariodariodario:
Software or hardware serial? You mean on my pc?

No, I mean, without seeing your sketch (which I still don't see) I don't know whether you used normal Serial.begin () or a software (bit-banged) variant.

Anyway if the speed is, for instance, 14400, 28800, I don't get HAHA, but I get things like squares and random chars. It seems to me that writing to the serial goes out-of-sync. Is there a way to enforce syncing? Maybe using write?

You mean always? Or at the start? Or after 100 characters? Or two characters? More detail please.

FWIW I found that with higher baud rates it took a few characters for the serial monitor to "sync" (at the start) which I am not totally surprised at.

If you start "reading" a continuous stream of serial comms (e.g., you see 0001111000111100) then you may see a bogus "start bit" which make you think you have an incoming byte, which turns out to be rubbish. So for the receiver to sync properly it needs to align itself on a byte boundary. Sending continuous 0xFF at the start may help. Or just a simple delay after the Serial.begin().

As for the blocking issue, I would generally assume that any function call would complete (i.e., block) unless stated otherwise. This blocking might involve putting things into an internal buffer, but absent warnings in the documentation, I would not expect garbage from repeated function calls.