Go Down

Topic: Unable to use pass HardwareSerial to classes in due (Read 4268 times) previous topic - next topic


I have a Due that is using a gps and I am trying to build a library to simplify its use. I actually am going to reuse the code on a couple of Dues so I want to be able to pass a specific serial port to use for communication so I can change it based on what pins I use. For now I have a simple class set up that accepts a HardwareSerial pointer in the constructor and a function to print a test message. The code compiles fine and on a Uno it actually works as expected. When I compile it for the Due I get the following warning.

sketch_jan14a.cpp.o: In function `SerialTest::begin(long)':
C:\Users\pepi0003\Downloads\arduino-1.5.1r2/sketch_jan14a.ino:29: warning: undefined reference to `HardwareSerial::begin(unsigned long)'
Binary sketch size: 9,720 bytes (of a 524,288 byte maximum)

The code still compiles and can be uploaded to the due but doesn't actually print anything on the serial port. Here is the code I am using. Any help would be appreciated.

Code: [Select]
class SerialTest{
    SerialTest(HardwareSerial *ser);
    void begin(long baud);
    void printHelloWorld();
    HardwareSerial *hwSerial;   

SerialTest test(&Serial);

void setup(){

void loop(){

SerialTest::SerialTest(HardwareSerial *ser){
  hwSerial = ser;

void SerialTest::begin(long baud){

void SerialTest::printHelloWorld(){
  hwSerial->println("Hello world!");


Jan 15, 2013, 02:20 am Last Edit: Jan 15, 2013, 01:34 pm by drobe011 Reason: 1
Not sure if this helps, but here is how I have done it in my endeavours:

I originally tried to use HardwareSerial Class to pass, but seems it wasn't the right object.  In the compiler error output it complained about trying to convert UARTClass to HardwareSerial.  Once I dug around a few, I found the actual two classes I needed to be able to pass a HardwareSerial object.


Code: [Select]
UARTClass *ucTx;
ucTx = &Serial;


Code: [Select]
USARTClass *ucTx;
ucTx = &Serial1; // Can be Serial 1-3 [s]Could just use Serial for HardwareSerial0[/s]


Edit: USARTClass is for HardwareSerial1-3(Serial1-Serial3).
HardwareSerial0(Serial) is of class UARTClass (virtual COM port via ATmega16U2 to PC).

main.cpp includes Arduino.h
Arduino.h includes HardwareSerial.h and variant.h
variant.h includes both UARTClass.h and USARTClass.h


Jan 15, 2013, 02:50 am Last Edit: Jan 15, 2013, 03:00 am by lloyddean Reason: 1
Code: [Select]

class SerialDevice
   HardwareSerial&     _serial;

   SerialDevice(HardwareSerial& serial)
       : _serial(serial)

   void begin(long baud)   { _serial.begin(baud);             }
   void printHelloWorld()  { _serial.println("Hello world!"); }

SerialDevice device(Serial);

void loop()

void setup()


Where does your class learn what the HardwareSerial name refers to? You appear to missing some necessary include statements.
The art of getting good answers lies in asking good questions.


I tried your code Dave and it is working perfectly now. I had been poking around in the old libraries and didn't realized that the ones for the due where stored in a separate folder. Is there any drawback to passing a reference to the class like in lloydeans code as apposed to using a pointer.


I believe in the context above pointer/reference would equal to the same thing.

The goal I was trying to achieve passing by pointer or reference was to eliminate passing by value, ie. sending the whole USARTClass object as a function param.  Save more memory just passing reference or actual memory location to USARTClass versus a (basically) literal copy of it.



If by reference as above the compiler will never allow you to compile without passing the Serial device.

Code: [Select]

SerialDevice device;

Will generate a compile time error/warning


Feb 10, 2014, 12:54 am Last Edit: Feb 10, 2014, 09:39 am by ahkub Reason: 1
Hello brickmaster32000,
I'm fighting the same issue on a Due as you did.
You said, when implementing Dave's workaround, it works for you.
I have a hard time figuring out where exactly you needed to place Dave's code.
May I ask you to post your code with Dave's workaround included?
Much appreciated.

Best regards,

Sorry for the dumb question. I looked at your code at GitHub (Century-Fablab-Projects) and found the answer:
I need to replace all "HardwareSerial" with "USARTClass" in the CPP file as well as in the H file.
Best regards,

Go Up