How do i know whether the OUT_PUT_BUFFER is empty or not?

hi,

i'm sending datas from Arduino Mega to PC through Serial port at 115200 bps. I know that the Serial.print will store the output data to the OUT_PUT_BUFFER and it will return immediately, then another thread(?) will send the data? (but is arduino available for multithread?) and if the buffer is full, then the Serial.print will wait until it can add the output to the buffer. but i don't want my Serial.print to wait. i want to know the spare space of the buffer to judge whether to print or not. do you have any suggestions for me to know the spare space of the buffer?

Thanks for reading.
Lancy

See:- Detecting available space in Serial transmit buffer - Programming Questions - Arduino Forum

Thanks a lot!

Lancy

That advice is out of date now. The hardware serial class now includes a function called availableForWrite() which will do exactly what that thread proposed.

Somehow this function doesn't appear in the Arduino documentation. It's only been a couple of years.

  if(Serial.availableForWrite() >= messageLength) {
    Serial.write(message, messageLength);
  } else {
    //do something else while waiting
  }