Instantiate a serial object

In my project I’m using both an AltSoftSerial port and the USB Serial debug port.

I want to set a variable to either a reference to an AltSoftSerial object or else to the normal Serial object. Both inherit from Stream.

At first I made the variable of type Stream& = Serial, but that failed when I used the begin() method, since that isn’t in the Stream definition.

Browsing the code, I see that Serial is actually an instance of the Serial_ class, so I tried the following:

#ifdef DEBUG
#include <USBAPI.h> // Where class Serial_ is defined
Serial_& MyPort = Serial;
#else
AltSoftSerial MyPort; // Calls constructor
#endif

No matter what I do, the compiler throws an error that “Serial_ is not a type”.

I even tried a typedef, as well as creating MySerial class which would inherit from both AltSoftSerial and Serial_.

I think that it just doesn’t like the trailing underscore.

Any help? Both AltSoftSerial and Serial inherit from Stream, and both define the same methods.

Where do you see

#define USBCON ...

?

I'm not seeing that anywhere using a DOS "find" command in the /hardware/arduino/avr/cores/arduino path.

then maybe thats why Serial_ was not defined?

USBCON must be defined for Serial_ to be declared…

You will notice pretty much all of USBAPI.h is encompassed in

if defined(USBCON)

endif

Why not just use a define?
(I don’t have the “AltSoftSerial” lib, so just used “SoftwareSerial” here.)

#define DEBUG

#ifdef DEBUG
#define myPort Serial
#else
#include <SoftwareSerial.h>
SoftwareSerial myPort(2,3);
#endif

Ps991:
then maybe thats why Serial_ was not defined?

USBCON must be defined for Serial_ to be declared…

You will notice pretty much all of USBAPI.h is encompassed in

if defined(USBCON)

endif

So I tried this:

#ifdef DEBUG
#define USBCON
#include <USBAPI.h>
Serial_ MySerial = Serial;
#else
AltSoftSerial MySerial;
#endif

And it didn’t work.

I’ll try to define Serial as OldSteve suggested.

You can use pointers to Stream objects. You can't make another Serial object as that would interfere with the first Serial object that's using that serial port. But you can have a pointer point to that, or to software serial, an I2C stream or a USB serial.

void doSomethingWithSerial(Stream *MySerial) {
  MySerial->read();
}

void loop() {
  doSomethingWithSerial(&Serial3); //Serial 3 exists on a Mega and a Due
}

Note the -> operator replaces the . operator when using pointers to objects. You can even access methods that only exist in Serial and don't exist in Stream by casting the pointer (once you know that you have a serial object in your sights.)

I was using a Stream reference before, and that worked until I tried calling a MySerial.SetTimeout, which doesn’t exist in the Stream class, and I was given an error.

Will a pointer allow me to call methods of derived classes? I will try, but right now the #define is working okay for me since it’s really just for debugging purposes.

Thanks again for the answers!

This works on a Due. I don't remember what the correct class is for hardware serial on the other Arduinos:

(UARTClass*)MySerial->SetTimeout(1234);