Adding custom variables to the HardwareSerial class?

Hello,

I would like to do something like this in my sketch:

Serial.NewVariable = SomeValue;

But without modifying the HardwareSerial class itself.

Is it possible? Or, is there any other solution?

Unless the serial class is sealed (too lazy to look), you could create a class that inherits from it and put your new variable in that.

What are you trying to do?

I have an Arduino Mega, and in my sketch I use Serial and Serial1.

I have a function which does something to the serial port that is passed as an argument, example:

void MyFunction( HardwareSerial serialport )
{
    if (serialport.Available() > 0)
    {
        ...
        serialport.connected = true; // example of what I want to do
    }
}

Then later I would like to use:

if (Serial1.connected)
    ...

I have tried to make an inherited class but failed (I don't know how it works) :frowning:

class MyHardwareSerial: public HardwareSerial
{
  bool connected;
};

If only it was that simple...I have already tried this, but failed, or show me how to use this with my example, I may be doing something wrong...

If only it was that simple.

It is, assuming that you then create instances of your new class (and never use instances of the HardwareSerial class).

I have already tried this, but failed

Post your code, and how you know it failed.

Sorry, I don't have code to show, since I can't compile without errors because I don't know how to do...

What I don't understand is how to create an instance of the new class that points to Serial (or Serial1).

Instead of deriving your own class from HardwareSerial it sounds like it's better to define connected variable as a global like Serial and just update it accordingly.

Can someone write a working example, not just words please...?

Can someone write a working example, not just words please...?

(: try something like

bool g_connected=false;

void MyFunction()
{
  if(serialport.available()>0)
  {
    ...
    g_connected=true;
  }
}

Sorry but that doesn't help solving my problem...

guix:
If only it was that simple...I have already tried this, but failed

It ought to be that simple, so if your attempt failed I suggest you post it and perhaps somebody will figure out why it isn't working. It may be a fault in your code, or it may be a design fault in the Arduino runtime library.

Just to give you an alternative avenue of attack, why are you trying to add data to the Serial object? Depending what you have in mind inheritance may not be the only way to achieve it, and may not be the most appropriate way to achieve it.

I just would like to know a more elegant way than doing this:

bool SerialConnected = false;
bool Serial1Connected = false;
bool Serial2Connected = false;

void MyFunction( HardwareSerial serialport )
{
    if (serialport.Available() > 0)
    {
        ...

        if (serialport == Serial)
            SerialConnected = true;
        else if (serialport == Serial1)
            Serial1Connected = true;
        else if (serialport == Serial2)
            Serial2Connected = true;
    }
}

There must be a better way!

There must be a better way!

It isn't clear why you need to do that at all. Perhaps if you explain that.

guix:
There must be a better way!

Well, JarkkoL gave you a code snippet which gave you a starting point based on producing a subclass of HardwareSerial.

I'm still unclear what you aim to achieve with this so I have no idea whether subclassing is a sensible way to achieve it.

What I want to achieve is having the possibility to know which of the serial(s) is/are currently connected to my pc.

The best I can do actually is something like this:

bool SerialConnected = false;
bool Serial1Connected = false;
...
bool isConnected(HardwareSerial serial)
{
  if (serial == Serial)
    return SerialConnected;

  else if (serial == Serial1)
    return Serial1Connected;

  return false;
}

I think there is a more elegant solution... Sorry if you still don't understand, my english is limited and I can't explain more clearly.

What I want to achieve is having the possibility to know which of the serial(s) is/are currently connected to my pc.

The question is WHY you want to know this.

Are you planning, after calling the isConnected() function, to use a specific instance of the HardwareSerial class?

if(isConnected(Serial))
   Serial.print("Something");

If so, then there is a problem. The isConnected() function does not do what it's name implies.

It seems like you are trying to tell your library to use a specific instance of HardwareSerial, like so:

SomeLib myInst(Serial2);

and then later, you want SomeLib::SomeMethod() to know whether to use Serial.print(), Serial1.print(), Serial2.print(), etc.

If that IS what you want, we'd need to see how the library is expecting the HardwareSerial instance to be passed in. Accepting a pointer to an instance would mean that you could just use the pointer, without caring whether it pointed to Serial, Serial1, Serial2, etc.

source:

SomeLib::SomeLib(HardwareSerial *pSerInst)
{
   pSerial = pSerInst;
}

void SomeLib::SomeMethod()
{
   pSerial->print("Yo, dude");
}

header:

class SomeLib
{
   SomeLib(HardwareSerial *pSerIn);
   void SomeMethod();

private:
   HardwareSerial *pSerial;
};

sketch:

SomeLib myInst(&Serial2);

myInst.SomeMethod();