How to tell if the Serial1 output buffer is empty?

I'm using hardware Serial1 connected to a RS485 transceiver. When sending, I need to set the Enable pin HIGH on the transceiver, and then LOW once the output is sent so that it can switch to receive mode.

Apparently (probably a feature) there is an internal Serial1 buffer, such that my code can send it's data to Serial1 and then continue running while Serial1 is still sending data. So what happens is the Serial1 buffer gets loaded and my code turns off the transceiver before Serial1 can send all of my data.

Is there a way to tell when Serial1 has actually sent all of its data?

And out of curiosity, how large is the buffer?

Thanks in advance.

You could check Serial1.availableForWrite()
Whenever the returned value is less than the full capacity of the buffer you know that there are still pending bytes to send.

This also answers your other question

Before you put anything in the buffer (or after a call to Serial1.flush()) you will get the capacity of the buffer.
This is the safer way then giving a discrete number as the default size of the buffer may vary from platform to platform and from one Device OS release to the next.
Since you can also provide your own buffer via acquireSerial1Buffer() the size of the buffer may even be almost any arbitrary number.

1 Like

Worked perfectly - Thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.