Go Down

Topic: Using SoftwareSerial inside a library - Arduino 1.02 (Read 2 times) previous topic - next topic

WanaGo

Hello,

Just after a bit of help please.
Attempting to write a library.

The Library is going to use the SoftwareSerial library that comes with the Arduino IDE.
The Sketch itself doesnt need access to the Software Serial port, just the library.

Ive tried a few things, but so far I get:
error: 'SoftwareSerial' does not name a type

in the library.h file I have the #include <SoftwareSerial.h>
in the library.cpp file I have tried a few things, but same result. SoftwareSerial mySerial(2, 3). And then it uses mySerial.write() etc throughout.
And in the sketch I have nothing, as the sketch I dont need to reference it.

I tried the SoftwareSerial mySerial; in the header file, and then myLibrary::mySerial(2,3) in the cpp file... but whatever combination I use I seem to get the same result.
Also SoftwareSerial mySerial = SoftwareSerial(2,3);

If a better code example is required, I can whip one up, but if someone could help me understand what needs to go where, so the library can use the Software Serial, that would be apprecaited.
I clearly dont know what I am doing.

So far I have been following http://arduino.cc/en/Hacking/LibraryTutorial but I havent found anything from searching that has helped yet.

I did find this, however obviously havent understood correctly as I am still having problems, http://arduino.cc/forum/index.php/topic,114761.0.html

Any help would be appreciated

Regards
J

WizenedEE


And in the sketch I have nothing, as the sketch I dont need to reference it.
---
I did find this, however obviously havent understood correctly as I am still having problems, http://arduino.cc/forum/index.php/topic,114761.0.html


That thread (correctly) says you need to #include the header in the main sketch as well. So you have

lib1.h
Code: [Select]

#include <lib2.h>
// stuff


sketch.ino
Code: [Select]

#include <lib1.h>
#include <lib2.h>
// stuff

WanaGo

Ok, I have added that to the main sketch too - thought I had tried that.
Now get:

error: a call to a constructor cannot appear in a constant-expression
error: ISO C++ forbids initialization of member 'mySerial'
error: making 'mySerial' static
error: invalid in-class initialization of static data member of non-integral type 'SoftwareSerial'

All pointing to the same line of code in the .h file

private:
SoftwareSerial mySerial = SoftwareSerial(2, 3);

In that post it suggested I cant do SoftwareSerial mySerial = SoftwareSerial(2, 3); there, and that I just do SoftwareSerial mySerial; ?
And then have to call mySerial(2,3); somewhere else?

Will keep trying, but I must just be stupid.

PaulS

Quote
All pointing to the same line of code in the .h file

private:
SoftwareSerial mySerial = SoftwareSerial(2, 3);

Sure, because that line of code is wrong. You can not assign a value to a variable in a header file. You can only declare variables in the header file.

You have to value the variable in the source file. Standard C++ stuff.

lemming

#4
Dec 05, 2012, 12:31 pm Last Edit: Dec 05, 2012, 12:33 pm by lemming Reason: 1
I had a similar problem a while back but with the serial port.  PaulS helped me out.

Have a look at this:

http://arduino.cc/forum/index.php/topic,40435.0.html

I haven't tried it but the same principles should apply to the software serial.

Go Up