Is there a difference between the following two line codes?
while (Serial.availableForWrite <= 0) {}
versus
Serial.flush()
Thanks!
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.
Sign up to join this communityIs there a difference between the following two line codes?
while (Serial.availableForWrite <= 0) {}
versus
Serial.flush()
Thanks!
The line
while (Serial.availableForWrite() <= 0) {}
waits until the serial output buffer is not full. Whereas
Serial.flush();
waits until the serial output buffer is empty.
https://www.arduino.cc/reference/en/language/functions/communication/serial/availableforwrite/
availableForWrite
Get the number of bytes (characters) available for writing in the serial buffer without blocking the write operation
Write operations on Arduino streams wait if they can't write the data to output. You can use the count returned by availableForWrite
to avoid this blocking.
https://www.arduino.cc/reference/en/language/functions/communication/serial/flush/
flush
Waits for the transmission of outgoing serial data to complete.
One use of flush()
is to wait until the data are sent away before putting the MCU into sleep mode.