Is my Serial transmission finished?

Hello,

Arduino's Serial (Serial - Arduino Reference) library is very good!
It is easy to use and satisfies most needs.

However, sometime we need more.
In my application, I need to change the direction of a transmit/receive multiplexer when transmission finishes. Timing is critical! Thus I need to know when Serial.write or Serial.print finish its job.

  • Is there a generic way of testing (or waiting until) if a Serial send function finishes its job?

Thx

Almost. Serial sends are synchronous except the last byte. So, after write or print returns you only need to wait for the last byte to leave.

This is the code of interest...

  while (!((*_ucsra) & (1 << _udre)));

For most processors it looks like _ucsra is "&UCSR0A" and _udre is "UDRE0". Give this a try...

  while (!((UCSR0A) & (1 << UDRE0)));

And, if you don't mind, please ask for this to be added as an enhancement...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=swbugs

Thank you for your message.

How your code ("while (!((UCSR0A) & (1 << UDRE0))); ") should differs if we use Serial, Serial1, Serial2, or Serial3?

Thx

The UDRE0 bit (buffer empty) is used to determine if the serial output buffer is ready to accept a new character.

You need to check the TXC0 bit (transmitter complete) flag to dtermine if the last character submitted to write/print has been fully shifted out.

while (!(UCSR0A & _BV(TXC0)));

Thank you BenF for your message.

Based on your message, I assume that for Serial, I need to have the following code;

   Serial.print("xyz");
   while (!(UCSR0A & _BV(TXC0)));
   digitalWrite(Multiplex_Direction, LOW); // Change the multiplex direction pin

What about Serial1, Serial2, Serial3?

IS THIS CORRECT?
    Serial1.print("xyz");
    while (!(UCSR1A & _BV(TXC1)));
    digitalWrite(Multiplex_Direction, LOW); // Change the multiplex direction pin

    Serial2.print("xyz");
    while (!(UCSR2A & _BV(TXC2)));

    Seria3.print("xyz");
    while (!(UCSR3A & _BV(TXC3)));

The UDRE0 bit (buffer empty) is used to determine if the serial output buffer is ready to accept a new character.

Yikes! Sorry about that. :-[