PDA

View Full Version : Teensy 3.0 Serial.flush() does not appear to work



Madox
12-24-2012, 12:27 PM
I'm new to "Arduino" but the Serial.flush() function does not appear to work.

From :
http://www.pjrc.com/teensy/td_serial.html
-----
Serial.flush()

Discard any received data that has not been read.
-----
Even if I call Serial.flush() before a Serial.read(), the Serial.read() will still return data.
Even if I call Serial.flush() before Serial.available(), Serial.available will still return number of available octets assuming Serial.flush() does nothing.

//Hope I didn't miss a memo about flush() not working yet. At the moment just discarding using Serial.read().

t3andy
12-24-2012, 02:40 PM
Serial.flush() function does not appear to work.
@ Madox ...Only on the USB serial port or on the other serial ports or on all ports? (Serial1.flush(),Serial2.flush(),Serial3.flush())

Madox
12-24-2012, 11:05 PM
@ Madox ...Only on the USB serial port or on the other serial ports or on all ports? (Serial1.flush(),Serial2.flush(),Serial3.flush())

I have only tried the USB Serial, no handy UARTs to test the Serial1-3 sorry.

t3andy
12-25-2012, 02:44 PM
I did a serial turnaround test (with series current limits) to spot check serial 1 and serial 2 ports. Serial1.flush() and Serial2.flush() does nothing?:confused:



void setup() {
Serial1.begin(9600);
Serial2.begin(9600);
delay(10000);
}

void loop() {

Serial1.println("12345");
delay(1000);
Serial1.flush();
delay(1000);
loopx:
if(Serial1.available() )
{
int x = Serial1.available();
Serial.print ("number of chars in rec buffer = ");
Serial.println(x,DEC);
char c = Serial1.read() ;
Serial.print("char in buffer = ");
Serial.println(c);

goto loopx;
}
else{
Serial.println ("buffer empty");
}
}

el_supremo
12-25-2012, 03:13 PM
On the Arduino, starting with V1.0 (IIRC), the functionality of Serial.flush was changed so that it blocked until the OUTPUT buffer was empty, or more precisely, until the last character in the output buffer had been loaded into the hardware UART. I haven't checked, but since beta9 is based on V1.0.2 it probably is doing the same thing.

Pete

t3andy
12-25-2012, 08:57 PM
The code snippet (above) after transmit waited for 1000 msec then issued a serialx.flush and waited for another 1000 msec and there were still characters (ALL) in the buffer. Waited 2 seconds for only 7 chars:confused:

BTW ... For those who want to try this test just put a 1k in series with Tx and Rx and run the test.

PaulStoffregen
12-26-2012, 01:11 AM
On Teensy 3.0, Serial1.flush() waits for output to transmit. To discard input, use Serail1.clear().

On Teensy 2.0, Serial1.flush() waits for output to transmit if using Arduino 1.0 or later. On Arduino 0022 and 0023, flush() discards input. The Arduino specification changed for flush(). Teensyduino attempts to follow the Arduino API.

The website documentation is out of date. (Edit: it's been updated)

In the example above, Serial1.println("12345") transmits 7 characters. They are all sent during the first several milliseconds of the 1 second delay. Serial1.flush() does nothing, because all 7 characters have already transmitted. If you've connected TX1 to RX1, all 7 are already in the receive buffer long before the first delay completes.

You mentioned testing Serial1 and Serial2, but the code above does not seem to use Serial2 after initializing it to 9600 baud. It reads from Serail1. If you've connected TX1 to RX2 but you're running to code above, it will never receive the 7 bytes.

t3andy
12-26-2012, 03:45 AM
In the example above, Serial1.println("12345") transmits 7 characters. They are all sent during the first several milliseconds of the 1 second delay. Serial1.flush() does nothing, because all 7 characters have already transmitted. If you've connected TX1 to RX1, all 7 are already in the receive buffer long before the first delay completes.


I thought that Serial1.flush() "ONLY" clears the Rx buffer? So in my code (above) I sent 7 characters, waited 1 sec, by that time the chars are in the rec buffer. Then I used the Serial1.flush() to clear the Rx buffer of chars. If the buffer is cleared then the serial available should skip but it doesn't because there are still 7 chars in the buffer! I then loop the buffer by reading the chars thus removing the chars from the buffer.


You mentioned testing Serial1 and Serial2, but the code above does not seem to use Serial2 after initializing it to 9600 baud.

I used the code snippet for Serial1 "then" re-wrote it for Serial 2. I was in a hurry.


If you've connected TX1 to RX2 but you're running to code above, it will never receive the 7 bytes.

Correct ... For my testing, I connected Tx1-1k-Rx1 (Serial1) and Tx2-1k-Rx2 (Serial 2).

Serial1.flush() does NOT work BUT Serial1.clear() does and clears the Rx buffer. <----------------<<<<<<<<<<<<<



Serial monitor printout using serial1.flush and code snippet() <------------------------------<<<<<<



number of chars in rec buffer = 7
char in buffer = 1
number of chars in rec buffer = 6
char in buffer = 2
number of chars in rec buffer = 5
char in buffer = 3
number of chars in rec buffer = 4
char in buffer = 4
number of chars in rec buffer = 3
char in buffer = 5
number of chars in rec buffer = 2
char in buffer =

number of chars in rec buffer = 1
char in buffer =

buffer empty



Serial monitor printout using serial1.clear() instead of serial1.flush in code snippet (above) <------------------<<<<<<<


buffer empty
buffer empty
buffer empty
buffer empty

PaulStoffregen
12-26-2012, 08:51 AM
I've updated the serial port documentation.

http://www.pjrc.com/teensy/td_uart.html

t3andy
12-27-2012, 11:21 AM
Spammer Alert

PaulStoffregen
12-27-2012, 06:06 PM
Would you believe we're actually getting several hundred spam messages each day? Fortunately, the Akismet filter is catching most of them. Please report the few that get through, by clicking the little black triangle. Robin usually deletes them in the morning (pacific time).

Robin's investigating more anti-spam options. Sadly, many involve inconvenience to everyone. Some risk automatically moderating legitimate posts, which I believe is much worse that a small amount of spam getting through the filter.

Madox
01-01-2013, 01:19 PM
Thanks for the documentation update Paul, it clears (!!!) things up.

Madox
01-19-2013, 01:23 PM
I've updated the serial port documentation.

http://www.pjrc.com/teensy/td_uart.html

Just a minor one ...
http://www.pjrc.com/teensy/td_serial.html is still out of date :)

PaulStoffregen
01-19-2013, 07:51 PM
Just a minor one ...
http://www.pjrc.com/teensy/td_serial.html is still out of date :)

Updated it.