Go Down

Topic: Using SoftwareSerial.h in user made libraries (Read 2142 times) previous topic - next topic

dimitri

I was wondering if anyone knows how to use SoftwareSerial in a library.  for example, here's my .c file

#include "WProgram.h"
#include "WConstants.h"
#include "SoftwareSerial.h"


Example::Example()
{
   SoftwareSerial Serial;
}

Void Example::Set_Baud(int baudRate)
{
   Serial.begin(baudRate);
}







This does not currently work when I try to compile in the Arduino environment.  "Serial" is not recognized, does anyone know what I should do?

mellis

A few things.  One, you'll need to declare the SoftwareSerial variable inside your class, in the Example.h header file.  Otherwise, it will only be defined within the scope of the Example::Example function.  See the _pin variable in the library writing tutorial for an example.  You probably don't want to call the variable Serial, since that's already used for the hardware serial object (it might work, but it's confusing).  You'll need to give your file a .cpp extension, not .c.  And you'll need to also #include "SoftwareSerial.h" in your main sketch (i.e. Sketch > Import Library > SoftwareSerial).  

dimitri

Thanks a ton,

Should I #include "SoftwareSerial" anywhere in my .h or .cpp files?  

Sorry for the elementary questions, I haven't done tons of serious programming in a year or so, and usually it was embedded micros, programmed in C.  My bad for not seeing the .cpp (whoops, good luck with classes!!).

If I declare (for example) SoftwareSerial myConnection;    in my .h file, what pins will it communicate on?  How would it interfere with with Serial communications outside of my library (like in the .pde file when I call Serial.print(x,x))?

dimitri

Also, I was wondering,

How is it that in the main sketch, i do not have to #define "SoftwareSerial.h" ?

Go Up