parameter as Stream type

Hi,

I would write a generic code for my device which listen the instructions from from Serial (USB) and Ethernet (web).
I saw theses classes extend the Stream.h.

Can I do something like:

void parseLine(Stream *inputStream);

Then an error occurs while a call:

Foo::parseLine(_client);

_client is type of EthernetClient

error: cannot convert 'EthernetClient' to 'Stream*' for argument '1' to 'void Core::readUint8(Stream*, uint8_t&)'

Can you help me?
Thx :slight_smile:

Can I do something like:

Yes, you can.

Then an error occurs while a call:

Not surprising. You defined the function taking a pointer, but you aren't calling it with a pointer.

Try:

Foo::parseLine(&_client);

Wünderbar!!! Great thanks, it works.

:slight_smile:

Damned!

The skelton function compiles.
But when I write something inside I have a problem.

void readUint8(Stream *inputStream, uint8_t &out)
  {
    int c;
    while ((c=inputStream.read())/*!=-1*/ && '0'<=c && 'c'<='9') {
      out = (out *10) + ((uint8_t) (c -'0'));
    }
  }

error: request for member 'read' in 'inputStream', which is of non-class type 'Stream*'

I include <Stream.h> in my .h…

Well read() is "virtual" in Stream.h but it is called in Stream.cpp
I.E:

int Stream::peekNextDigit()
{
  int c;
  while (1) {
    c = timedPeek();
    if (c < 0) return c;  // timeout
    if (c == '-') return c;
    if (c >= '0' && c <= '9') return c;
    read();  // discard non-numeric
  }
}

This code works.

Why I can't use "inputStream.read()"?
:confused:

I add a '*':

while ((c=(*inputStream).read())

It compiles but I don't know why.

A simpler way exists to do that?

Foo::test(Stream **p) {
p.read();
}

Foo::test(&&Serial)?

The pointer deference operator is ->, not .

while ((c=inputStream->read())

Ok, thx!