From af841d166dbc213194badfe94b7b7b0684787ca0 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sun, 6 Mar 2011 12:20:42 -0500 Subject: [PATCH] Flushing outgoing and incoming data in Serial.end(). That is, waiting for outgoing data to transmit and dropping any received data. --- hardware/arduino/cores/arduino/HardwareSerial.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/hardware/arduino/cores/arduino/HardwareSerial.cpp b/hardware/arduino/cores/arduino/HardwareSerial.cpp index 673867554..1154ae7e1 100644 --- a/hardware/arduino/cores/arduino/HardwareSerial.cpp +++ b/hardware/arduino/cores/arduino/HardwareSerial.cpp @@ -313,16 +313,22 @@ void HardwareSerial::begin(long baud) sbi(*_ucsrb, _rxen); sbi(*_ucsrb, _txen); sbi(*_ucsrb, _rxcie); - cbi(*_ucsrb, _udrie); // XXX: what if there's already data in the tx buffer? + cbi(*_ucsrb, _udrie); } -// XXX: should we empty the rx and tx buffers here? void HardwareSerial::end() { + // wait for transmission of outgoing data + while (_tx_buffer->head != _tx_buffer->tail) + ; + cbi(*_ucsrb, _rxen); cbi(*_ucsrb, _txen); cbi(*_ucsrb, _rxcie); cbi(*_ucsrb, _udrie); + + // clear any received data + _rx_buffer->head = _rx_buffer->tail; } int HardwareSerial::available(void)