2

I'm using the Serial1 of the Arduino Leonardo to send and receive data with another MCU. The following code will always retrieve garbled data:

byte streamReadResponse;
Serial1.begin(115200);
Serial1.setTimeout(9000);
Serial1.print(cmd);
Serial1.print('\r');
Serial1.flush(); //wait for all the data to be sent to the serial
streamReadResponse = Serial1.readBytesUntil('>', data, dataLength);

But, if I add a delay(50) before the Serial1 read, I would get the expected output. The value 50 was found by trial and error.

What am I missing? Why is the flush() command not working as expected?

hithwen
4432 silver badges15 bronze badges
asked Mar 25, 2014 at 9:26
5
  • 2
    Could you please tell more about "garbled data". You mean what it is inside your "data" variable? What is the final value of streamReadResponse after the call to readBytesUntil with and without the delay? Commented Mar 25, 2014 at 10:34
  • Try putting a ~2.2k pull-down resistor on the Arduino's RX pin (connect the resistor to ground and the pin). Commented Mar 25, 2014 at 19:40
  • 1
    Which version of Arduino IDE are you using? It seems HardwareSerial.cpp has changed a lot along different versions, in particular the flush() method. In version 1.0.5 it should be OK normally. Commented Mar 27, 2014 at 22:36
  • This question really is not answerable without more details, such as the nature of the garbled data as already requested. It appears to have been abandoned and should probably be closed. Commented Apr 26, 2014 at 16:08
  • @ChrisStratton you are right - I was not even sure on how to provide the details - I still hope my own answer can help other people. Commented Apr 29, 2014 at 13:25

2 Answers 2

2

I've finally found that when the MCU to which Arduino is talking to was initialized, it would send spurious data to the Serial - I guess it would start first with a lower baud rate, and then switch to the programmed baud rate of 115200.

To fix this, I had to flush the Arduino serial RX before sending any data. So:

byte streamReadResponse;
Serial1.begin(115200);
Serial1.setTimeout(9000);
while (Serial1.read() >= 0)
 ; // do nothing
Serial1.print(cmd);
Serial1.print('\r');
Serial1.flush(); //wait for all the data to be sent to the serial
streamReadResponse = Serial1.readBytesUntil('>', data, dataLength);
answered Apr 29, 2014 at 9:32
0

Usually, this kind of issue is the result of the several times by time unity of loop statements execution, with the baudrate defined for your serial i/o it's very possible that the flush are override by the new information input and the result are the "garbage" on that.

On my sketches, ever I put "idle function calls" in some points of the code to avoid this and works succesfully.

answered Mar 27, 2014 at 8:42
3
  • 1
    I don't understand why this would be. Surely .flush() should do as documented unless Roberto has overridden it? Commented Mar 27, 2014 at 8:54
  • I don't know why, but, for example, for prevent in a project the malfunction of a clock module, I must call to read it fewer than times, when the documentation not tell nothing about this.The fact is that many times the execution speed of the main loop can make some features of functions and/or response of devices will be wrong. As if you push data onto a funnel and arrives to the exit mistaken. Commented Mar 27, 2014 at 11:50
  • .flush() pauses execution until the output buffer is empty. It shouldn't have anything to do with garbage on your input. Try putting a ~2.2k+- pull-down resistor between serial-in and ground to make sure you aren't getting noise or cross talk in the input buffer. Commented Mar 27, 2014 at 18:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.