Calling Serial or Serial1 from my own class

I'm trying a rewrite a simple class for a serial-based Wifly module shield that was written to use SoftwareSerial to communicate with the module. Since I have a Mega, I can use Serial1.

The old class used:

class WiflyClass: public SoftwareSerial {
public:
WiflyClass(int,int);

I am trying to do either:

class WiflyClass: public Serial1 { // do i really need to inherit Serial1?
class WiflyClass {

However on compilation with the first line I get:

/Users/wsanders/Documents/Arduino/libraries/WifiShieldMega/WiflyMega.h:10: error: expected class-name before '{' token

Or with the second form I get:

/Users/wsanders/Documents/Arduino/libraries/WifiShieldMega/WiflyMega.cpp:6: error: 'Serial1' is not a class or namespace (line 6 is the line where I call Serial1.begin.)

I am including <Arduino.h> in the class's cpp and .h files.

I don't really need to inherit Serial1, since there will only be once instance of my class "owning" the Serial1 port.

So, then it seems like my class is having trouble finding Serial1, but, like Serial, it should be available everywhere Arduino.h is included? My class should have no trouble finding it....

Could someone post a link to a library that uses Serial or Serial1? Their use ought to be identical since both are defined in Arduino.h....

Thanks --w

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.


Serial1 isn't a class, it's an instance of HardwareSerial.

You might find this helpful:

class myMenu 
  {
  private:
    Stream & port_; 
  public:
    myMenu (Stream & port) : port_ (port) { }
    void begin ();
  };

void myMenu::begin ()
  {
  port_.println ("Menu initialized."); 
  }

myMenu menu (Serial);

void setup ()
  {
  Serial.begin (115200);
  menu.begin ();
  }  // end of setup

void loop () { }

Note that passing a class instance by reference like that can only be assigned in the constructor and can't ever be re-assigned.

Should you want more flexibility, you might want to consider a pointer instead. In ICSC I use this:

class _ICSC {
        //....
        HardwareSerial *_serial;
        //....
};

And then later on I use things like:

void _ICSC::begin(unsigned char station, unsigned long baud, HardwareSerial *sdev, int dePin)
{
    _serial = sdev;
    _serial->begin(baud);
    //....
}

Note the use of -> instead of . when working with a pointer.

Thanks for the replies, they were useful. I need to dig out my beginner C++ books, they are around somewhere!