ArduinoCore-API icon indicating copy to clipboard operation
ArduinoCore-API copied to clipboard

add isFlushed() to Stream/HardwareSerial

Open budulinek opened this issue 5 years ago • 0 comments

Please add Serial.isFlushed() method for checking that all data in Tx buffer have been flushed. We need it for non-blocking Serial.write() to half duplex communication (such as RS485). At the moment I use something like this in my non-blocking code:

while (Serial.availableForWrite() > 0 && ndx < sizeof(buffer)) {
      Serial.write(buffer[ndx]);                
      ndx++;
}
if (Serial.availableForWrite() == SERIAL_TX_BUFFER_SIZE - 1 && ndx > sizeof(buffer)) {
ndx = 0;
// Disable RS485 transmit and start receiving
}

But Serial.availableForWrite() == SERIAL_TX_BUFFER_SIZE - 1 is not reliable for checking that Tx buffer has been flushed.

Here is an earlier PR which was never merged: https://github.com/arduino/Arduino/pull/3737

and a similar PR for ArduinoCore-sam here: https://github.com/arduino/ArduinoCore-sam/pull/7

I would help, but coding Arduino core is beyond my skills.

budulinek avatar Apr 05 '20 16:04 budulinek