0

Is there a difference between the following two line codes?

while (Serial.availableForWrite <= 0) {}

versus

Serial.flush()

Thanks!

2

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.

| improve this answer | |
2

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.

| improve this answer | |

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.