Every time I initially connect to the serial port on my Uno I see several (sometimes many) garbage characters trickling onto my screen. Once I start typing they go away so up until now they haven't been a problem. Now my project is maturing they are getting in the way (machine to machine comms is much less forgiving of such things).
Is this something others see as well? Any solutions out there? Could this be some sort of serial settings discovery thing?
This does tend to happen, particularly if you have just turned the device on, or have changed baud rates. There is a finite time at the new baud rate when data at the old baud rate might look valid. Building in a brief pause (at the receiving end) and/or sending some character from the sending end (eg. 0xFF) may help synchronize things.
I recently wrote an error-checking protocol to handle this sort of situation:
The intention there was to wrap "packets" of data with a start and end character, and also a sumcheck.
The receiving end then validates the packet, before accepting it. You may lose one or two, but that is surely better than just getting bad data?
If you use that protocol, along with (if necessary) some code to ask for missing data to be resent, it should all work reliably. If the data isn't critical (eg. temperature readings from a sensor) then you can just ignore any bad packets that are received.
I suppose I'm already headed in the direction of error checking/resend. I was just hoping there was a way to fix the communication parameters on both ends in code so they come up in sync.
I don't suppose you have any suggestion on how to do that?
Apart from delays, it isn't really solvable. A certain bit pattern at one speed (eg. 10101010) might look like another valid pattern (eg. 11001100) at another speed.
An initial delay (at the receiving end) will probably help. Something like delay for 100 mS and then flush the serial buffer. Why not? They won't all get turned on at the same time anyway.
But even then, without error checking of some sort (or at least looking for a start character) you might come in "half-way" through some data and mistake it for something else.
After giving it more thought, I guess I disagree with your assertion.
These garbage characters are coming from the Uno without any input from me. There are therefore no characters available to the Uno to read and misinterpret. I simply connect, sit back and watch the unrecognized characters trickle in. I also see the occasional garbage character from time to time coming from the Uno after communication begins (albiet rare).
I'm beginning to believe that this is actually either a bug in either the Serial library, or the hardware itself. It appears to be some sort of random noise in the system. If you see it too, it is unlikely that it has anything to do with my (our?) code.
I suppose a simple test using the blink sketch is in order. If the random characters appear, something else is up (i.e. not my bug).
I thought I read somewhere that the Uno defaults to 19200 baud by default, and setting to 9600 baud in setup() changes that. Is that true?
They are the characters sent by the boot loader that checks if a new sketch needs to be loaded. They are very fast so you don't always see them in the serial monitor.
Do you mean "any output" from the Uno? If you are seeing garbage in the serial monitor that is output from the Uno you are monitoring. As I said before switching baud rates is quite likely to cause problems briefly. Especially with the Uno and its USB-to-serial conversion chip. I don't know how that chip can handle switching baud rates without a moment or so of "guessing" so to speak about what the new baud rate is.
Like I said before, a brief pause, followed by a Serial.flush (), on power-up, is likely to help with incoming characters. Sending some harmless characters outwards upon powering up is likely to help with outgoing characters. And using sumchecks etc. will help with the occasional "bad" character in the middle (noise, possibly).
Interesting. I spent the day refactoring a huge portion of my code (for other reasons). When I got it all working again I happened to notice that I'm not seeing the garbage characters now. None.
Now I'm back to thinking it was a bug in my own code (that I have since factored out).
Now when I connect I see absolutely nothing coming from the Uno, regardless of how long I wait. So far (after a long afternoon of debugging) I haven't seen a single strange character.
Hmm...
Thanks again for your responses. I will keep my eye open for garbage characters, but for now, things seem stable again.