user55067: Is there a difference between the following two line codes?


while (Serial.availableForWrite <= 0) {}


versus


Serial.flush()


Thanks!

user37622: 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.

user7508: 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.

55067: <p>Is there a difference between the following two line codes?</p>

<blockquote>
  <blockquote>
    <p>while (Serial.availableForWrite &lt;= 0) {}</p>
  </blockquote>
</blockquote>

<p>versus</p>

<blockquote>
  <blockquote>
    <p>Serial.flush()</p>
  </blockquote>
</blockquote>

<p>Thanks!</p>
 37622: <p><a href="https://www.arduino.cc/reference/en/language/functions/communication/serial/availableforwrite/" rel="nofollow noreferrer">https://www.arduino.cc/reference/en/language/functions/communication/serial/availableforwrite/</a></p>

<blockquote>
  <p><code>availableForWrite</code> Get the number of bytes (characters) available for writing in the serial buffer without blocking the write operation</p>
</blockquote>

<p>Write operations on Arduino streams wait if they can't write the data to output. You can use the count returned by <code>availableForWrite</code> to avoid this blocking.</p>

<p><a href="https://www.arduino.cc/reference/en/language/functions/communication/serial/flush/" rel="nofollow noreferrer">https://www.arduino.cc/reference/en/language/functions/communication/serial/flush/</a></p>

<blockquote>
  <p><code>flush</code> Waits for the transmission of outgoing serial data to complete.</p>
</blockquote>

<p>One use of <code>flush()</code> is to wait until the data are sent away before putting the MCU into sleep mode.</p>
 7508: <p>The line</p>

<pre class="lang-cpp prettyprint-override"><code>while (Serial.availableForWrite() &lt;= 0) {}
</code></pre>

<p>waits until the serial output buffer is <strong>not full</strong>. Whereas</p>

<pre class="lang-cpp prettyprint-override"><code>Serial.flush();
</code></pre>

<p>waits until the serial output buffer is <strong>empty</strong>.</p>
