SoftwareSerial Instance within custom Arduino Library

Im creating a library which is gives any Software Serial/UART the ability to be addressed, just like an I2C device can be addressed.

The problem im having is that i have no idea how to successfully instantiate a SoftwareSerial object within the library, without the Arduino IDE (1.5.2) throwing an error. This is my header file:

#ifndef AddressedUart_H
#define AddressedUart_H

//#include <WProgram.h>
#include <Arduino.h>
#include <SoftwareSerial.h>

class AddressedUart {
  private:
    byte _RX;
    byte _TX;
    int _ADDRESS;
    int _UARTBAUD;
    SoftwareSerial UART;
  public:
    AddressedUart(byte Rx, byte Tx, int Address);
    int available();
    void begin(int UARTBAUD);
};

#endif

Arduino IDE throws up the following error:
“‘SoftwareSerial’ does not name a type”

Thank you for any suggestions in advance :slight_smile:

Are you including SoftwareSerial.h in the sketch that goes with that library? You need to.

The SoftwareSerial class does not have a no-argument constructor, so that code is not going to work.

You need to make UART a pointer to a SoftwareSerial instance, and use new SoftwareSerial(xx, xx) to create the instance in the AddressedUart() instance.

Thanks for the fast reply :slight_smile:

I added an include line for SoftwareSerial.h to the main sketch.

How do i make UART a pointer to a SoftwareSerial instance? that would do exactly what i need, i think :smiley:

Also, is there a way to make the SoftwareSerial instance available to all class functions (Global declaration), when it is instantiated within the AddressedUart(byte Rx, byte Tx, int Address) by using Software AddressedUart = SoftwareSerial(0,1);?

My sketch:

#include <SoftwareSerial.h>
#include <AddressedUart.h>

const int MYADDRESS = 29;

AddressedUart UART(8,9,MYADDRESS);

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

void loop() {
  int tmpdata = UART.read();
  // Code here for testing data retreived.
}

AddressedUart.h:

#ifndef AddressedUart_H
#define AddressedUart_H

//#include <WProgram.h>
#include <Arduino.h>
#include <SoftwareSerial.h>

class AddressedUart {
  private:
    byte _RX;
    byte _TX;
    int _ADDRESS;
    int _UARTBAUD;
    SoftwareSerial UART;
  public:
    AddressedUart(byte RX, byte TX, int Address);
    void begin(int UARTBAUD);
};

#endif

AddressedUart.cpp:

#include "AddressedUart.h"

//<<constructor>> setup the LED, make pin 13 an OUTPUT
AddressedUart::AddressedUart(byte RX, byte TX, int Address){
  _RX = RX;
  _TX = TX;
  _ADDRESS = Address;
}

//turn the LED on
void AddressedUart::begin(int UARTBAUD){
  _UARTBAUD = UARTBAUD;
  UART.begin(_UARTBAUD);
}

After adding the #include line for SoftwareSerial.h to my main sketch, i now get this error code: “no matching function for call to ‘SoftwareSerial::SoftwareSerial()’”

i now get this error code: “no matching function for call to ‘SoftwareSerial::SoftwareSerial()’”

That was what I warned you about.

    SoftwareSerial UART;

This tries to create an instance of the SoftwareSerial class, calling the no argument constructor. There isn’t one.

    SoftwareSerial *UART;

defines a variable called UART, of type pointer to SoftwareSerial.

Then, in the constructor for your class, you use

UART = new SoftwareSerial(RX, TX); // Or maybe the other way around

Since UART is a class member, it will be available to all methods of the class.

Of course, since it is a pointer to an object, not an object, you need to use pointer notation, not object notation, to access the SoftwareSerial members:
UARTprint(“Working…”);

Software AddressedUart = SoftwareSerial(0,1);

NEVER try to create a SoftwareSerial instance using the hardware serial pins.

I never knew about the use of → and * in that context. I fixed up all my code the way you suggested. It compiles way passed all my previous errors - thankyou so much! :smiley:

Then the compiler threw an error about "‘AddressedUart’ does not name a type’. After some googling, i found your suggestion on the Arduino forum about changing the include line for a library to an absolute/explicit path to the .h file and that worked too!

Now i can fully compile my code - thanks :smiley:

But im curious, is there a way for the compiler to still find my library .h file, without using an absolute path?? Having to use an absolute path makes it hard for me to copy the sketch & library folder elsewhere, and still have it work.

But im curious, is there a way for the compiler to still find my library .h file, without using an absolute path?? Having to use an absolute path makes it hard for me to copy the sketch & library folder elsewhere, and still have it work.

Yes, but you have provided nowhere near enough information. Just where is the header file?

Oh sorry! for the development stage, i had my .h and .cpp file in the same folder as my sketch. Now ive based my library on inheriting from SoftwareSerial, and i moved my .h and .cpp file to their own folder in my librarys folder.

So everything works now :3 thanks for your help :smiley:

Hello DorianMcCarthy can i have tour library file becouse i've got the same problem :stuck_out_tongue: