Is there a way to test whether the UART Tx buffer is empty without waiting?

serial.flush() waits, so it won't help me.

I tried looking into serial.SerialEvent(), but the link was broken on the Arduino reference page.

Can anyone point me to a description of it?

  • 1
    If you need such fine control over the UART then you may need to drop to low-level manipulation of the registers. – Ignacio Vazquez-Abrams May 19 '14 at 14:52
  • No, there is no method to do this in the source of the serial driver in the main Arduino repository, but it shouldn't be too hard to add one, which checks either the software-maintained buffer or the hardware register of the UART itself, or both, according to your need. – Chris Stratton May 19 '14 at 16:11
  • If you use SoftwareSerial then definitely not. With HardwareSerial that could be possible but you'd have to change the source code yourself. – jfpoilpret May 19 '14 at 18:58

It doesn't look like that function is implemented. The source for HardwareSerial.{cpp,h} are in /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino under OS-X. (Someone better versed in Window than I may have to chime in here to point you to that source location.)

Taking HardwareSerial::available() as a model, create a new function, say, HardwareSerial::txremaining(), that does the same thing, but for the transmit buffer.

Left as an exercise for the student:
Make a common function that returns the contents of an arbitrary ring-buffer and use it in both ::available() and ::txremaining().

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