13

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Piotr Kula
  • 9,597
  • 8
  • 59
  • 85
  • Have you included hardwareserial.h in your main source too? You'll need it there. Make sure hsrdwareserial.cpp or the library that contains it is also in your project. Does your device have a serial1? I was just going by your example but if it's not an atmega 1280 then it only has "Serial". Make sure your class declaration in your header is terminated with a ";". If your project settings allow you to specify a device then make sure that definition is correct. Failing that you'll need to put the exact error message and code, filenames, project settings etc online somewhere. – tinman Sep 17 '11 at 20:06
  • Yea I am using Mega1280, if i include it in the .cpp and .h it errors that Serial1 is not defined. If i include hardwareserial.h in my sketch is says Serial1 is not defined, try using *constructor.. etc. I looked in the hwseria.h/cpp and it auto isntantiated the Serial,Serial1,serial2,serial3 in there. But I still cant get it workin. The ahrdware serial is in the arudino source, not my directory. – Piotr Kula Sep 18 '11 at 21:52
  • What if your library needs to call begin() or end() on a Stream object? It doesn't work, how would I fix that? – Nuclear_Man_D Jan 09 '17 at 01:57

3 Answers3

31

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
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
tinman
  • 6,348
  • 1
  • 30
  • 43
  • @ppumkin: You also need to include the HardwareSerial.h header in the .h and .cpp files because the compiler error means that it cannot find the definition for the HardwareSerial class. – tinman Sep 17 '11 at 15:31
  • ohh man! i knew it was somethign thick like that :( i do get this error in themain program now `sep17a:15: error: 'Serial' was not declared in this scope sketch_sep17a:18: error: 'Serial1' was not declared in this scope` and that was working before i put these references in.. – Piotr Kula Sep 17 '11 at 15:35
  • Its like the main program lost its reference to the hardware serial, i try to include it and define it and it asks me for all the HardWareSerial constructor things.. – Piotr Kula Sep 17 '11 at 15:47
  • I tried using a pointer instead- if i dont include myclass then the Serial1 works, as soon as i refence myClass it does not know what it is anymore :( – Piotr Kula Sep 17 '11 at 16:00
  • FYI, Doesn't seem to work with the Teensy, or i'm doing something else wrong i can't figure out. – Emile Oct 23 '13 at 14:31
9

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

Timofey Stolbov
  • 4,501
  • 3
  • 40
  • 45
EternityForest
  • 316
  • 2
  • 6
  • Thanks for that. I still have not solved that issue.. it has been a while but I will remember to try this when I get back to that some day. Thanks – Piotr Kula Oct 23 '13 at 22:02
  • "I believe Serial is not actually an instance of HardwareSerial" - How does one find the C++ definition of Serial from Arduino processing? Is it possible to cast Serial to a custom library that overrides Serial or HardwareSerial? – T.Coutlakis Jan 28 '14 at 23:20
1

I have one improvement on Mr Murdoch's code (thanks a lot by the way, following your example on how to initialize a serial object made me a happier, less compiler frustrated individual).

His method will let you pass any hardware serial object to his class. Mine by default will let you pass (and configure) a hardware object to the class, but will also let you switch in a software serial object later if you are interested.

Almost the exact same as his code, except I replace his instance of the HardwareSerial object in his class with the Stream object.

myclass.h

myClass(HardwareSerial& serial, int baud, int config); // Constructor taking reference
Stream& _s; // reference member within class

myclass.cpp

// Constructor takes reference to a serial port object
myClass::myClass(HardwareSerial& serial = Serial, int baud = 9600, int config = SERIAL_8N1) :
_s(serial) // Need to initialise references before body
{
    serial.begin(baud, config);  //We guarantee a hardware interface at first
    this->_s = serial;           //The constructor uses the hardware serial class
                             //but we can replace it with a virtual hardware
                             //class if we have to thanks to stream
} 
dda
  • 6,030
  • 2
  • 25
  • 34
Meek_The_Geek
  • 61
  • 1
  • 1