Wait for Serial.println to complete

The thing is that I want to change the communication speed on the fly. The protocol follows: computer: "s115200;" atmega: "s ok" (println) and the speed changes.

The problem is on the atmega side: If I call Serial.println("s ok") and Serial.begin(152000), the last bytes of "s ok" get trashed. And this should happen, as the reference says that println returns before sending the last character. But how do I wait for that very last character to be actually sent?

The piece of

long sp;
sp=atol(&(RxBuffer[1]));//get the new speed
UCSR0A &= ~B01000000;//clear TX-complete flag
Serial.println("s ok");
while((UCSR0A & B01000000)==0);//an attempt to wait, does not help

btw uC=ATMEGA644 Arduino version = 0017

I'd use delay. Figure at 9600 bps, you're sending a character at about one per ms. So if you delay 10 ms, your 4 bytes will surely have been sent. If you speed is something other than 9600 adjust your delay acordingly.


Thanks. That will do, for now. I really hope that delayMicroseconds won't interfere with timer/counter 1... :-/ I really need timer1 for pulse train generation.

;) I can grab the loop from delayMicroseconds implementation to be absolutely sure.

delayMicroseconds(3000); works like a charm! thanks! 8-)