Pages: [1] 2   Go Down
Author Topic: Using SoftwareSerial inside a library - Arduino 1.02  (Read 1821 times)
0 Members and 1 Guest are viewing this topic.
NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#include <lib2.h>
// stuff

sketch.ino
Code:
#include <lib1.h>
#include <lib2.h>
// stuff
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Australia
Offline Offline
Sr. Member
****
Karma: 10
Posts: 396
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 05, 2012, 06:33:50 am by lemming » Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys, still cant get it working.

Here is a better layout of what I have currently, if you can point out what I have done wrong I would appreciate it.
I have little knowledge at all of C++.

So as mentioned above, I am just trying to get my library to be able to use the Software Serial, my sketch does not need access to it. But if it has to, then that is fine...

Library.h File
Code:
#ifndef Library_h
#define Library_h
 
#include "Arduino.h" // for Arduino 1.0

#include <SoftwareSerial.h> //Software Serial Library

class Library
{
public:
//Bunch of stuff here

private:
SoftwareSerial mySerial;
}

Library.cpp File
Code:
#include "Library.h"
#include "Arduino.h" // for Arduino 1.0

mySerial = SoftwareSerial(2, 3); //Tried various combinations, in and out of the constructor, but similar results.

Library::Library() 
{
mySerial.begin(9600);
}

My Sketch
Code:
#include <SoftwareSerial.h>
#include <Library.h>

Library Library;

void Setup()
{

}

void loop()
{
}

The above might not be the best combination I have tried, but is one of them.
Really not sure what I am doing, so help would be apprecaited.

Thanks
J
Logged

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

You need to construct the SoftwareSerial instance at the same time you construct the class:

Library::Library() , mySerial(2,3)
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry so that goes in the cpp file?

So the h file and the sketch looks ok...?

But the cpp file needs to be this?:
Code:
#include "Library.h"
#include "Arduino.h" // for Arduino 1.0

Library::Library() 
{
mySerial(2,3);
mySerial.begin(9600);
}

not sure sure what Library::Library() , mySerial(2,3) means... is that a single line I put in place of the Library::Library() ?

Im getting a truck load of errors...

error: ISO C++ forbids declaration of 'Library' with no type
error: no 'int Library::Library()' member function declared in class 'Library'
etc

Sorry can you spell it out for me...
Thanks
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

made a typo which didnt help, in the Library::Library() line.

Errors I get now are:

Quote
Library.cpp: In constructor 'Library::Library()':
Library.cpp:13: error: no matching function for call to 'SoftwareSerial::SoftwareSerial()'
SoftwareSerial.h:83: note: candidates are: SoftwareSerial::SoftwareSerial(uint8_t, uint8_t, bool)
SoftwareSerial.h:48: note:                 SoftwareSerial::SoftwareSerial(const SoftwareSerial&)
Library.cpp:15: error: no match for call to '(SoftwareSerial) (int, int)'

Line 13 is:
Code:
Library::Library() //Line 13
{
mySerial(2, 3);
mySerial.begin(9600);
}
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to construct the SoftwareSerial instance at the same time you construct the class:

Library::Library() , mySerial(2,3)


Probably a typo, sholuld be (in the cpp file):

Code:
Library::Library() : mySerial(2,3) {
    // code
}
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep thats seems to be better, thanks.

I get past this bit now, got more errors related my other code now I think.

Thanks
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If Class has member objects, its constructor takes this general form:

Class::Class() : obj1(...), obj2(...), ... {
   // Class instance construction code
}
Logged

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

I know this is an old post, hopefully it pops up somewhere. I'm trying to implement the same concept but I don't understand what I'm doing wrong.

I want to run an instance of softwareserial in the library files, the error I'm running into though is

ISO C++ forbids declaration of 'SoftwareSerial' with no type

The line it refers to is

     private:
      SoftwareSerial *SSerial;

in my header file.

I attempted the change mentioned by PaulS where I instantiate the softwareserial when I create the constructor

Library::Library(): SSerial(4,5)
{
   
}

I've looked at examples and tried to match them, my first attempt everything worked properly except when I was trying to retrieve data using SSerial.available() and SSerial.read()

My code works fine without the software serial on the standard TX/RX pins, so what I'm trying to narrow down is what exactly do I need to place into my .h and .cpp files to have the software serial functioning properly.
« Last Edit: March 30, 2013, 05:43:13 pm by Sweepstar » Logged

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

Quote
My code works fine without the software serial on the standard TX/RX pins
Please explain this. You can NOT do software serial on the hardware serial pins.

http://snippets-r-us.com might be able to help you with the snippets. Here, we need to see ALL of your code.
Logged

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

Quote
My code works fine without the software serial on the standard TX/RX pins
Please explain this. You can NOT do software serial on the hardware serial pins.

http://snippets-r-us.com might be able to help you with the snippets. Here, we need to see ALL of your code.

What I mean by this is without implementing any type of softwareserial the code executes successfully, my problem is I have plans for the Pin 1 and Pin 0 so I have to move my device from pins 1/0 which is why I'm trying to implement the software serial to move them to pins 4,5. The snippets link isn't working for me, perhaps you know another link?
Logged

Pages: [1] 2   Go Up
Jump to: