Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 getStatus().

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

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.

EDIT 1

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?):

private:
HardwareSerial& _HardSerial;

EDIT 2

So, basically because I import the HardWareserial.h file in file myClass.h 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...

like image 293
Piotr Kula Avatar asked Sep 17 '11 14:09

Piotr Kula


2 Answers

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.h

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

myclass.cpp

// 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
myClass(&Serial1); 

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

myclass.h

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

myclass.cpp

// 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
like image 195
tinman Avatar answered Oct 14 '22 05:10

tinman


On the Arduino Leonardo, I believe Serial is not actually an instance of HardwareSerial, but rather of Serial_, due to it being a virtual USB link and not a normal USART.

However, both of these classes are subclasses of Stream.

So maybe you can declare Stream* instead of HardwareSerial*.

like image 24
EternityForest Avatar answered Oct 14 '22 04:10

EternityForest