Serial port object in custom library

Hi everyone! I’m new on this forum but I have some time working with Arduino and other stuff.

Well my problem starts because I’m building a custom library to comunicate with a XBee module. It’s based on the library that we can found on the “libraries page”, but I’m changing some functionalities according to my project. My specific problem is with a HardwareSerial pointer that I’m declaring in order to get the Arduino’s Serial Port. My class looks something like this:

myOwnClass.h

#ifndef myOwnClass_h
#define myOwnClass_h

#if defined(ARDUINO) && ARDUINO >= 100
	#include "Arduino.h"
#else
	#include "WProgram.h"
#endif

#include <inttypes.h>

class myOwnClass {
public:
         myOwnClass();
         void begin(long baudRate);
private:
         HardwareSerial *_serial;
}

#endif

myOwnClass.cpp

#include "myOwnClass.h"

#if defined(ARDUINO) && ARDUINO >= 100
	#include "Arduino.h"
#else
	#include "WProgram.h"
#endif

myOwnClass::myOwnClass(){

}

void myOwnClass::begin(long baudRate){
      _serial->begin(baudRate);
      _serial->println("Initializing Serial Port ....");
}

Then my sketch is something like this:

#include <myOwnClass.h>

myOwnClass _testMyOwnClass = myOwnClass();
int ledPIN = 13;

void setup(){
      _testMyOwnClass .begin(9600);
      pinMode(ledPIN, OUTPUT);
}

void loop(){
  //Just to do something ...
  digitalWrite(ledPIN, HIGH);
  delay(1000);
  digitalWrite(ledPIN, LOW);
  delay(1000);
}

Well, of course it has a lot of more coding … methods, attributes and so on. But my specific problem is with these lines. According to me! this should be sending a message to the serial monitor of the IDE, where I could see “Initializing Serial Port …”
Unfortunately it’s doesn’t do anything … not even the LED blinking … :open_mouth: … It’s important to say that the code always compiles and that I can always upload the code to my Arduino, but as I said it doesn’t do anything.

What I have done to solve this issue is to replace the “begin” method on the class definition, and use something like this:

void myOwnClass::begin(long baudRate){
      Serial.begin(baudRate);
      Serial.println("Initializing Serial Port ....");
}

What am I missing? Is it something related to the HardwareSerial pointer? I almost forgot how to program with pointers :slight_smile: … Do I need to do this “Serial handle” always like in the code above?

Please try someboy to send me an advice.

Best regards!

You don't seem to initialise _serial anywhere. I would have thought that you would want to have it initialised by a constructor argument, which might be a HardwareSerial, or a Print interface, or whatever other interface you want to use to represent the abstraction that you're using.

Thanks for your post!

I don't know if that object needs to be initialized in order to use the methods (begin, write, read ... ) If you look at the code that I've replaced I'm using "Serial.begin(9600)" and I'm not initializing anything.

Any other idea why it has this behavior?

Best regards!

SpectroCL:
I don't know if that object needs to be initialized in order to use the methods (begin, write, read ... ) If you look at the code that I've replaced I'm using "Serial.begin(9600)" and I'm not initializing anything.

What is the value of _serial when this code executes?

     _serial->begin(baudRate);
     _serial->println("Initializing Serial Port ....");

I understand what you mean ... the value is just a pointer of HardwareSerial type. But how to initialize this pointer? Should I pass the Serial object to this pointer?

I'm a little confused because when a use "Serial.begin(9600)" I'm no initializing anything ... am I right?

Can you give an advice on how to initialize this pointer?

Thanks for the posts!

'Serial' is already an object which is initialised, that's why Serial.begin() works fine.

All you have to do to initialise your serial object is simply:

_serial = &Serial;

You could do something like:

in the .h
void begin(long baudRate, HardwareSerial * serial);
in the .cpp
void myOwnClass::begin(long baudRate, HardwareSerial * serial){
      _serial = serial;
      _serial->begin(baudRate);
      _serial->println("Initializing Serial Port ....");
}

Then in your sketch you could have:

_testMyOwnClass.begin(9600,&Serial);

or for the Mega, you could even do:

_testMyOwnClass.begin(9600, &Serial1);
or
_testMyOwnClass.begin(9600, &Serial2);
or
_testMyOwnClass.begin(9600, &Serial3);

You can pass by reference, eg.

class myMenu 
  {
  private:
    Stream & port_; 
  public:
    myMenu (Stream & port) : port_ (port) { }
    void begin ();
  };

void myMenu::begin ()
  {
  port_.println ("Menu initialized."); 
  }

myMenu menu (Serial);

void setup ()
  {
  Serial.begin (115200);
  menu.begin ();
  }  // end of setup

void loop () { }

Thank you very much for your help, now it’s working fine :slight_smile:

The main idea was togive the hability of work with any Serial Port, thinking that maybe in the Mega I can use several serial ports. So I’m using it like this:

void begin(long baudRate, HardwareSerial * serial);

Well thanks again and I will continue my development, cyou soon :wink:

Unfortunately, due to design decisions taken by the implementors of the HardwareSerial and SoftwareSerial classes, it's not possible to have a common abstraction of a bidirectional serial port. :frowning:

But if the serial port is only going to be used for input or only for output, you can refer to it by an abstraction which is common to the hardware and software serial ports.