Go Down

Topic: SoftwareSerial causes error in library (Read 2432 times) previous topic - next topic

wiccan2

Hi,

I'm in the process of writing my first library, I need to use software serial but i keep getting an error when ever I do, what is it I'm doing wrong.

The library is for the motor controller from web4robot

any ways heres the code followed by the error, hope some one can help.

Motor.cpp
Code: [Select]

#include "WProgram.h"
#include "Motor.h"
#include "SoftwareSerial.h"

Motor::Motor(int rx,int tx)
{
 mySerial =  SoftwareSerial(rx,tx);
 pinMode(rx, OUTPUT);
 pinMode(tx, OUTPUT);
 mySerial.begin(9600);
}

void Motor::motor1Stop(){motorComand(1,1,0x00);}
void Motor::motor2Stop(){motorComand(2,1,0x00);}
void Motor::motor1Forwards(char mSpeed){motorComand(1,1,mSpeed);}
void Motor::motor1Backwards(char mSpeed){motorComand(1,2,mSpeed);}
void Motor::motor2Forwards(char mSpeed){motorComand(2,1,mSpeed);}
void Motor::motor2Backwards(char mSpeed){motorComand(2,2,mSpeed);}

void Motor::motorComand(int motor, int mDirection, char mSpeed){
 char buff[5];      
 buff[0]=254; // Start byte
 buff[1]=77; // Device ID
 buff[2]=0x03; // Comand (03 = Motor Speed)

 if(motor <= 1){
   if(mDirection <= 1){
     buff[3]=0x00; // Motor and Direction (00 = Motor 1 Forwards, 01 = Motor 1 Bacwards, 02 = Motor 2 Forwards, 03 = Motor 2 Bacwards,
   }
   else{
     buff[3]=0x01; // Motor and Direction (00 = Motor 1 Forwards, 01 = Motor 1 Bacwards, 02 = Motor 2 Forwards, 03 = Motor 2 Bacwards,
   }
 }
 else{
   if(mDirection <= 1){
     buff[3]=0x02; // Motor and Direction (00 = Motor 1 Forwards, 01 = Motor 1 Bacwards, 02 = Motor 2 Forwards, 03 = Motor 2 Bacwards,
   }
   else{
     buff[3]=0x03; // Motor and Direction (00 = Motor 1 Forwards, 01 = Motor 1 Bacwards, 02 = Motor 2 Forwards, 03 = Motor 2 Bacwards,
   }
 }

 buff[4]=mSpeed; // Speed

for(int i=0;i<5;i++){mySerial.print(buff[i],BYTE);}

}



Motor.h
Code: [Select]
#ifndef Motor_h
#define Motor_h

#include "WProgram.h"
#include "SoftwareSerial.h"
class Motor
{
 public:
   Motor(int rx,int tx);
   void motor1Stop();
     void motor2Stop();
     void motor1Forwards(char mSpeed);
     void motor1Backwards(char mSpeed);
     void motor2Forwards(char mSpeed);
     void motor2Backwards(char mSpeed);
 private:
   void motorComand(int motor, int mDirection, char mSpeed);
     SoftwareSerial mySerial;

};

#endif



Error
Code: [Select]
Motor.cpp: In constructor 'Motor::Motor(int, int)':
Motor.cpp:11: error: no matching function for call to 'SoftwareSerial::SoftwareSerial()'
hardware\libraries\SoftwareSerial/SoftwareSerial.h:34: note: candidates are: SoftwareSerial::SoftwareSerial(uint8_t, uint8_t)
hardware\libraries\SoftwareSerial/SoftwareSerial.h:26: note:                 SoftwareSerial::SoftwareSerial(const SoftwareSerial&)

AlphaBeta

Try this:

Motor.cpp
Code: [Select]
Motor::Motor(int rx,int tx) : mySerial(SoftwareSerial(rx,tx)
{
 pinMode(rx, OUTPUT);
 pinMode(tx, OUTPUT);
 mySerial.begin(9600);
}

wiccan2

#2
Jul 18, 2009, 12:39 am Last Edit: Jul 18, 2009, 12:52 am by wiccan2 Reason: 1
Thank you so much, that gave no errors on start up.

Out of interest though why is it that this is required, I'm used to programming in java and have never come across this syntax before.

However when I tried to use my library I get this error.

Code: [Select]
hardware\libraries\Motor/Motor.h:5:28: error: SoftwareSerial.h: No such file or directory

This seems strange as SoftwareSerial does exist and is working in other sketches.

AlphaBeta

#3
Jul 18, 2009, 12:58 am Last Edit: Jul 18, 2009, 12:59 am by AlphaBeta Reason: 1
This is required because the constructor tries to contruct every member of the class in RAM as it is constructed.
When you write something inside the { }, the object is already initialized in memory.

This causes a problem when trying to construct an object that needs some parameters passed as arguments to the constructor.
It will complain that there is no constructor available with no parameters. (As I said, it tries to construct the software serial object at ... well ... construction.)

In C++ you need to have a way of passing arguments to member variables as they are constructed. Not afterwards. The : is just the syntax on how to accomplish that.


Code: [Select]

class Stupid {
public:
 Stupid( int x );
};

class Example {
public:
 Example();
private:
 Stupid stupid; //object is not initialized, just declared
};

Example::Example() : stupid(9000) {
 //the stupid object has been constructed in memory with parameter 9000 accounted for
}



Sorry for a poor explanation.


[edit]
You need to have both these on the top of your sketch:'
Code: [Select]
#include <SoftwareSerial.h>
#include <Motor.h>

:)[/edit]

wiccan2

Thanks for the explanation, but i'm still getting this error when i try to compile my sketch

Code: [Select]
hardware\libraries\Motor/Motor.h:5:28: error: SoftwareSerial.h: No such file or directory


AlphaBeta

#5
Jul 18, 2009, 01:16 am Last Edit: Jul 18, 2009, 01:16 am by AlphaBeta Reason: 1
[glow]#include "SoftwareSerial/SoftwareSerial.h"[/glow] maybe?
[edit]That is, in your .h file.[/edit]

wiccan2

Thanks for all your help, I cant really remember how I fixed that last one, all I can remember is it was something trivial, missing semicolon or something.

Any ways the library is finished now and is available at my website for download: http://www.wiccan-two.co.cc/arduino/dmc1-library

Thank you for all your help with this.

Go Up