ppumkin ppumkin - 1 year ago 263
C Question

How to pass Serial object by reference to my class in Arduino?

I have been reading up a couple of days now about pointers, references and dereferences in C/C++ targeted for the Arduino and can't fully udnerstand what I am missing.

I have my sketch which has a setup of

Serial.begin(9600); //Just for logging.
Serial1.begin(9600); //Arduino Mega-> Other Device

I use a wrapper class to send BYTE's over Serial1 by calling a simple function like

The issue I am having is I would like to make my class more dynamic, so I would like my class to use Serial1, Serial2, Serial3 or even the base Serial - but I do not know how to build my .h and .cpp files to use these as references.

At the moment my class has a static Serial1 but if somebody would like to use my class they would have to rename everything to Serial if they use Arduino Uno.

In myclass.h I have something like

myClass(HardwareSerial *serial);

and in myClass.cpp (constructor):

myClass::myClass(HardwareSerial &serial) {
_HardSerial = serial;

But the compiler keeps on moaning about
) expected before & or *

I have tried various ways and always get the same error- except when I reference my class to the Serial object-- but it says Serial was not defined..

I can't find any tutorials, except Pointer Resource for Arduino.

Declaration and Creation

#include "Print.h"
//datatype* variablename = ⌖
Print* printer = &Serial; Usage


//this is the equivalent of Serial.print
printer->print("print using the Serial object"); //notice the -> as opposed to .
//change target address (or which address to point to)
printer = &Serial2;
//this is the equivalent of Serial2.print
printer->print("print using the Serial2 object");

Which is exactly what I want - but it seems I do not understand it. I did what they did there, and it does not work for me.


Thanks, I did it the reference way because it is better yet. I still get these errors, though:

:88: error: 'HardwareSerial' has not been declared

Line 88 from .h:

uint8_t Init(long BaudRate, HardwareSerial& serial);

.h:136: error: ISO C++ forbids declaration of 'HardwareSerial' with no type
.h:136: error: expected ';' before '&' token

Line 136 from .h (this keeps on happening- I don't know what to use, Serial.write? / Serial?):

HardwareSerial& _HardSerial;


So, basically because I import the
file in file
which is imported in my sketch to use all myClasses stuff - it kills the references in the sketch and wants me to redefine the Serial instances. It seems like the inheritance is the wrong way around…annoying. What's the default constructor used in the sketch?

It's like it's asking me to do this:

HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);

Really? Again I don't understand...

Answer Source

You're mixing C++ references and pointers. It looks from that example snippet that you should be using pointers, so I have made my answer follow that style. References would probably be a better choice though.

Your code should look something like this:


myClass(HardwareSerial *serial); // ctor
HardwareSerial * _HardSerial; // member within class


// Constructor takes address of serial port
myClass::myClass(HardwareSerial *serial) {
 _HardSerial = serial;

calling code:

// Gets the address of "Serial1" and passes that to the constructor

If you wanted to do it as references it would look something like this:


myClass(HardwareSerial& serial); // ctor taking reference
HardwareSerial& _HardSerial; // reference member within class


// Constructor takes reference to a serial port object
myClass::myClass(HardwareSerial& serial) :
    _HardSerial(serial) // Need to initialise references before body

calling code:

myClass(Serial1); // Compiler automatically uses reference