Go Down

Topic: SoftwareSerial::flush() broken (Read 4 times) previous topic - next topic

fat16lib

I believe pre 1.0 flush() discarded all characters in the input buffer.

I think most people use something like this:
Code: [Select]
  while (Serial.read() >= 0) {}

If you want to discard all but n characters you could do this:
Code: [Select]
while (Serial.available() > n) Serial.read();

For n == 0 this is like the old flush().

I understand the Arduino group is thinking of bringing back the old flush with a new name.  Way to go team!

Paul Stoffregen

Actually, I've been thinking about it, since someone recently made a compelling case for efficiently discarding unnecessary protocol stuff from a certain wifi-to-serial adaptor.

I created a patch, but haven't had time to throughly test it yet.

The name will be Serial.clear(), because Arduino generally follows Processing's naming conventions.

WizenedEE


Actually, I've been thinking about it, since someone recently made a compelling case for efficiently discarding unnecessary protocol stuff from a certain wifi-to-serial adaptor.

I created a patch, but haven't had time to throughly test it yet.

The name will be Serial.clear(), because Arduino generally follows Processing's naming conventions.


I'm not sure if you're the best one to ask, but it would be nice if it accepted an optional argument that says how many bytes to discard.

Useful for when your protocol states that a transaction will always be (say) 10 bytes with the first being what kind of message it is -- if a message only needs to be 4 bytes, you'll want to discard the other 6. Or if you receive a message from a GPS unit telling you the time, you may want to just ignore it and throw away several bytes.

I don't think a method that does the same as the old flush will be as helpful though, since it's possible the next message has started and you accidentally throw away it also.

fat16lib

Maybe some of the Stream functions would work http://arduino.cc/en/Reference/Stream.  You can skip bytes using these functions.

You are really talking about parsing an input stream. At this level I write my own functions.  In fact I wrote a replacement for HardwareSerial for my personal use.

bperrybap


Actually, I've been thinking about it, since someone recently made a compelling case for efficiently discarding unnecessary protocol stuff from a certain wifi-to-serial adaptor.

I created a patch, but haven't had time to throughly test it yet.

The name will be Serial.clear(), because Arduino generally follows Processing's naming conventions.


It so hurts me to keep seeing new terms being used to define "new" functionality
when it suddenly becomes obvious again that some of that old functionality really was needed.
In this case, technically, I guess the IOSTREAM library guys and processing have created the problems here.

My preference would be that if
names are to be changed/updated how about we use the names and meanings
that were always historically used in serial devices?

flush() means discard,
drain() means wait for things to empty.

It had been that way for decades - long before the stream i/o guys
incorrectly picked the term "flush" to mean to drain out queued data.
and long before APIs like Win32 which now use the term "purge" to flush out buffers.
(Although I think "purge" is more intuitive than "flush" as to what it really does)

It used to be so simple and consistent.

For those too young to remember: here is some information on the topic:
http://www.serialio.com/support/jspUsing.htm

But I guess I can only dream.....

---- bill


Go Up