Using SoftwareSerial in a class..

Hello All,

I’m trying to create a generic class that handles Serial Communications. I would like to add SoftwareSerial as a member to this class. So, in my Sample.h file I have the following declaration.

#include <SoftwareSerial.h>

class SomeClass
{
public:
SomeClass(SoftwareSerial serial);
public:
SoftwareSerial serial;
};

However, I cannot add SoftwareSerial as a data member. Can you please help me as I would like to use this as a property.

The other thought I’ve had is perhaps deriving a class by

class SomeClass : public SoftwareSerial
{
public:
SomeClass(SoftwareSerial serial);
};

But don’t think this is an elegant solution. This doesn’t work either.

Can someone help me out please?

Thanks in advance,

Richard

However, I cannot add SoftwareSerial as a data member.

Because SoftwareSerial does not have a default (no-argument) constructor. You can create a reference, though, and populate the reference when your class is constructed. Or, use a pointer.

But don't think this is an elegant solution. This doesn't work either.

Solutions that aren't solutions generally are not elegant.

Hello All,

I have found the solution to this. It quite simply could not find the type.

I therefore added

#include <Arduino.h>
#include <SoftwareSerial.h>

And as if by magic it worked.

Thanks anyway.

Richard