Pages: [1]   Go Down
Author Topic: Unable to use pass HardwareSerial to classes in due  (Read 1671 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
class SerialTest{
  public:
    SerialTest(HardwareSerial *ser);
    void begin(long baud);
    void printHelloWorld();
     
  private:
    HardwareSerial *hwSerial;   
};


SerialTest test(&Serial);

void setup(){
 
  test.begin(9600);
}

void loop(){
  test.printHelloWorld();
  delay(1000);
}

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

void SerialTest::begin(long baud){
  hwSerial->begin(baud);
}

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

MS, USA
Offline Offline
Newbie
*
Karma: 1
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

[Updated]

Code:
UARTClass *ucTx;
ucTx = &Serial;

[/Updated]

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

-Dave

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
« Last Edit: January 15, 2013, 07:34:46 am by drobe011 » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
class SerialDevice
{
    HardwareSerial&     _serial;

public:
    SerialDevice(HardwareSerial& serial)
        : _serial(serial)
    {}

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


SerialDevice device(Serial);

void loop()
{
    device.printHelloWorld();
    delay(1000UL);
}

void setup()
{
    device.begin(9600);
}
« Last Edit: January 14, 2013, 09:00:58 pm by lloyddean » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50956
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where does your class learn what the HardwareSerial name refers to? You appear to missing some necessary include statements.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

MS, USA
Offline Offline
Newbie
*
Karma: 1
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Dave
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
SerialDevice device;

Will generate a compile time error/warning
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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,
Ahkub

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,
Ahkub
« Last Edit: February 10, 2014, 03:39:32 am by ahkub » Logged

Pages: [1]   Go Up
Jump to: