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?
HardwareSerial.cpp
has changed a lot along different versions, in particular theflush()
method. In version 1.0.5 it should be OK normally.