HarwareSerial or SerialBT argument

i have code, pcRead() that handles commands thru the serial monitor interface, Serial. I have a BluetoothSerial interface, serialBt.

i’d like to pass a more generic serial argument to pcRead that can be either Serial or serialBt.

i got the following error when I tried defining pcRead with a “Stream” argument

In file included from C:\stuff\RR\ArduinoRR\Koala\Koala.ino:11:0:

pcRead.h:4: error: cannot declare parameter 'serial' to be of abstract type 'Stream'

 int pcRead (Stream serial);

can I and if so, how can I defining an argument to pcRead that can be either Serial or serialBT?

here is pcRead.h

#ifndef PC_READ_H
# define PC_READ_H

int pcRead (Stream serial);

#endif

Koala.ino (12.2 KB)

pcRead.cpp (6.44 KB)

You're trying to pass "serial" by value. This would copy the argument, which is impossible for abstract types, as creating a copy would discard all dynamic type information in the inheritance hierarchy below Stream.
Use a reference instead:

int pcRead (Stream &serial);

Pieter

Or go Old School and pass a pointer (i.e. Stream *).

thanks both
defining a ptr

gcjr:
thanks both
defining a ptr

A reference would be simpler, surely?

TheMemberFormerlyKnownAsAWOL:
A reference would be simpler, surely?

Absolutely, and it eliminates the need to check for nullptr.

As I said, Old School. I’m a C old-timer. I do appreciate and use the OOP features of C++. There are situations where only a reference will work and situations where only a pointer will work. But, if either will work, I’ll choose the pointer.

Either will work, but imagine turning all those cut-and-paste "Serial.print"s to "Serial->print"s.

Tedious or what?

TheMemberFormerlyKnownAsAWOL:
Either will work, but imagine turning all those cut-and-paste "Serial.print"s to "Serial->print"s.

Tedious or what?

And having to put an ampersand in every call: pcRead(&Serial) instead of pcRead(Serial).

Perhaps. But back in the day, when men were men, that's the way we rolled 8)

gfvalvo:
Perhaps. But back in the day, when men were men, that’s the way we rolled 8)

And before that, men were toddlers, and that’s the way they fell over in uncomfortable heaps.