0

I use Arduino Uno. I get garbage in Serial Monitor or HyperTerminal right after I connect to the port. Here is an example code:

void setup() {
  // initialize serial:
  Serial.begin(57600);


}

void loop() {

    Serial.println("Test");
}

An example screenshot of the output. When port is re-connected the first received data is not Test:

enter image description here

So my problem is when the port is initialised the first data received is garbage. Is there a way to prevent this happening? Is that something to do with bootloader baud rate? I tried other baud rates and get same issue and I really want to use 57600 or 115200 baud rate.

  • I mean during Serial.begin(57600); I get garbage values. How can I skip this? – ty_1917 Jan 9 at 13:51
  • @jsotola I re-articulated the problem. Please see the edit. – ty_1917 Jan 9 at 21:22
  • If I comment out I get nothing. By "When port is re-connected" means clicking "call" in HyperTerminal or openning Arduino serial monitor. First data is always garbage. – ty_1917 Jan 9 at 21:30
  • In HyperTerminal there is Disconnect and Call buttons which closes and initiates the port. Similar to Arduino's serial monitor. I couldnt find a fix. – ty_1917 Jan 9 at 21:33
  • this may be an XY problem ... why are random characters a problem? – jsotola Jan 9 at 22:38
0

There are probably a number of reasons that might contribute to the problem that you are observing:

  1. You are trying to send the string "Text" within the main loop without any delay between println calls. So the microprocessor is running at full speed and trying to send the string "Text" much faster than the baud rate can accommodate. The serial interface has a finite I/O buffer, which is probably filling very fast and overwriting old values in the middle of serial transmission. Hence potentially garbage characters.

  2. Even when HyperTerminal is not running, the PC and Arduino are exchanging serial messages. So when you start HyperTerminal at some arbitrary time, the data in the serial buffer might not correspond cleanly to the start or end of a transmission.

As an alternative, you may want Arduino wait for a serial command from the PC before starting to send the serial data. For example, when using the Serial Monitor, you may send a key press to Arduino, which would then start sending data so that no data is lost while there is no body listening on the PC side or while a terminal program is not running.

Finally, flushing the serial buffers on either side of the serial connection might help you clear partial serial messages.

  • Reason 1 is not valid: Serial.println() does not overwrite bytes that have not yet been sent. It instead switches to blocking mode, where it waits for the interface to make enough room in the buffer for the new message. – Edgar Bonet Jan 10 at 20:41
  • The Serial object blocks when the UART buffer is full, so no data gets overwritten in the UART buffer. – JRobert Jan 10 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.