Using HardswareSerial as Stream

10 views
Skip to first unread message

Victor Aprea

unread,
May 12, 2017, 3:24:24 PM5/12/17
to Arduino Developers
Hey all,

I need to be talked down.

I know this isn't the first time I've encountered this, and maybe even the last time I brought it up as a problem it may have indirectly resulted in the inclusion of the availableForWrite() method in HardwareSerial.

The problem is that it's in HardwareSerial. Stream, as far as I can tell has no analogy, and flush() just empties the UART TXDATA register (on AVR), instead of draining the _tx_buffer. It's not uncommon for a class, e.g. PubSubLibrary to want to be hooked up to a Stream. And it's not uncommon to use &Serial to satisfy that Stream.

So if you want to write a class that gets passed a Stream, you have no way of knowing that the write() you're about to do into that Stream will get overflow the Stream's buffer.

It feels like I must be missing something fundamental. Anyone care to steer me straight?

Victor Aprea // Wicked Device

Paul Stoffregen

unread,
May 12, 2017, 6:57:16 PM5/12/17
to devel...@bcmi-labs.cc
On 05/12/2017 12:23 PM, Victor Aprea wrote:
> So if you want to write a class that gets passed a Stream, you have no
> way of knowing that the write() you're about to do into that Stream
> will get overflow the Stream's buffer.

Indeed, the long term solution will (probably) define
availableForWrite() as a virtual function in the Stream class. Until
that day comes, you can't use it from a library that takes a generic
Stream pointer or reference.

But availableForWrite() is still young. It's 3rd Arduino birthday
coming up July 18. I don't know when it'll be mature enough for Arduino
to officially adopt it in Stream, but hopefully that day will come
eventually?





Tom Igoe

unread,
May 13, 2017, 9:58:55 AM5/13/17
to devel...@bcmi-labs.cc
I assume someone’s filed a pull request for it in Stream, yes?

t.
> --
> You received this message because you are subscribed to the Google Groups "Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@bcmi-labs.cc.

Tilo Nitzsche

unread,
May 13, 2017, 3:00:09 PM5/13/17
to devel...@bcmi-labs.cc
On Sat, May 13, 2017 at 3:58 PM, Tom Igoe <t.i...@bcmi-labs.cc> wrote:
I assume someone’s filed a pull request for it in Stream, yes?

There is a pull request for Print (where it really belongs):
https://github.com/arduino/Arduino/pull/5789

Paul Stoffregen

unread,
May 15, 2017, 2:55:21 PM5/15/17
to devel...@bcmi-labs.cc
Yes, you're right, it really does belong in Print together with write().

Cristian, Massimo, any chance you guys could merge this pull request?


Cristian Maglie

unread,
May 23, 2017, 3:58:39 AM5/23/17
to devel...@bcmi-labs.cc
Il 15/05/2017 20:54, Paul Stoffregen ha scritto:
> Yes, you're right, it really does belong in Print together with write().
>
> Cristian, Massimo, any chance you guys could merge this pull request?

The PR has been merged, I'm wondering to merge also
https://github.com/arduino/Arduino/pull/2387 that is closely related.

C

Rob Tillaart

unread,
May 23, 2017, 5:16:52 AM5/23/17
to devel...@bcmi-labs.cc
Hardware and Software Serial (and more) inherits flush() from Stream so I assume it should not be moved to Print().

Yes in SWSerial there is currently no TX buffering (so nothing to flush) e.g. when someone implements a non-blocking write() 

2 cents,


C

--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@bcmi-labs.cc.

Cristian Maglie

unread,
May 23, 2017, 6:41:38 AM5/23/17
to devel...@bcmi-labs.cc
Il 23/05/2017 11:16, Rob Tillaart ha scritto:
> Hardware and Software Serial (and more) inherits flush() from Stream so
> I assume it should not be moved to Print().

This should not be a problem since Stream inherits from Print, so the
virtual method will be still available in Stream. The point of the PR is
to move the "flush" method down in the hierarchy in Print where it
belongs (since flush() is an "output" method).

C

Rob Tillaart

unread,
May 23, 2017, 9:27:58 AM5/23/17
to devel...@bcmi-labs.cc
HI Cristian,

you're right, I had the dependency not correctly in my mind, so no objections to merge 

regards,
Rob





C

Reply all
Reply to author
Forward
0 new messages