Pages: [1]   Go Down
Author Topic: Using of NewSoftSerial in custom library  (Read 1389 times)
0 Members and 1 Guest are viewing this topic.
Russia
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to modify this excellent library http://husks.wordpress.com/2011/05/23/a-minimal-arduino-library-for-processing-serial-commands/ for processing serial commands to work with software serial port. I modified .h and .cpp files, so here is a beginning of NSSerialCommand.h file (modified library)
Code:
#ifndef NSSerialCommand_h
#define NSSerialCommand_h
#include "WProgram.h"
#include <string.h>
#include <..\NewSoftSerial\NewSoftSerial.h>

class NSSerialCommand
{
public:
NSSerialCommand();    // Constructor
void clearBuffer();       // Sets the command buffer to all '\0' (nulls)
char *next();              // returns pointer to next token found in command buffer (for getting arguments to commands)
void readNSSerial();     // Main entry point. 
void addCommand(char *, void(*)());   // Add commands to processing dictionary
void addDefaultHandler(void (*function)());    // A handler to call when no valid command received.

private:  ...
In NSSerialCommand.cpp  I wrote:
Code:
#include "WProgram.h"
#include <string.h>
#include <..\NewSoftSerial\NewSoftSerial.h>
#include "NSSerialCommand.h"
NewSoftSerial NSSerial(2, 3);
void NSSerialCommand::readNSSerial()
{
while (NSSerial.available() > 0)
{
int i;
boolean matched;
inChar=NSSerial.read();   // Read single available character, there may be more waiting
                         ...
Main sketch:
Code:
#include <NewSoftSerial.h>
#include <NSSerialCommand.h>
NewSoftSerial NSSerial(2, 3); // 2-Rx, 3-Tx
NSSerialCommand SCmd;      // The SerialCommand object

void setup()

   NSSerial.begin(57600);//Start talking with PC
  // Setup callbacks for SerialCommand commands
  SCmd.addCommand("vel",process_velocity);  // Converts two arguments to integers and echos them back
  SCmd.addDefaultHandler(unrecognized);  // Handler for command that isn't matched  (says "What?")
  NSSerial.println("Ready");
}

void loop()

  SCmd.readNSSerial();     // We don't do much, just process serial commands
}
The problem that i get an error:
 "NSSerialCommand\NSSerialCommand.cpp.o: In function `NSSerialCommand::clearBuffer()':
D:\Development\arduino-0022\arduino-0022\libraries\NSSerialCommand/NSSerialCommand.cpp:45: multiple definition of `NSSerial'
NSS_commands.cpp.o:(.bss.NSSerial+0x0): first defined here"
And it's quit obvious that here is multiple definition of `NSSerial'. But if I try to define SoftSerial port once in a main sketch, i got definition errors in library.cpp and if I try to define port in library.cpp  the main sketch don't "see" definition. I'm not familiar with C programming, so I appreciate any help.
Thanks in advance.
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1926
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

add extern NewSoftSerial NSSerial(2, 3); in a header file.
remove one of the NewSoftSerial NSSerial(2, 3); from the cpp files.
Best regards
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

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

Thanks for your reply,Jantje.
Exuse me for my stupidity, but I don't understand exactly what does that meen?
Quote
extern NewSoftSerial NSSerial(2, 3);
But I have tried just to add this line to my header file
Code:
#ifndef NSSerialCommand_h
#define NSSerialCommand_h
#include "WProgram.h"
#include <string.h>
#include <..\NewSoftSerial\NewSoftSerial.h>
#define SERIALCOMMANDBUFFER 20
#define MAXSERIALCOMMANDS 10
#define MAXDELIMETER 2
extern NewSoftSerial NSSSerial(2, 3);
class NSSerialCommand
{
public:
   NSSerialCommand();            // Constructor
void clearBuffer();       // Sets the command buffer to all '\0' (nulls)
char *next();              // returns pointer to next token found in command buffer (for getting arguments to commands)
void readNSSerial();    // Main entry point.  
void addCommand(char *, void(*)());   // Add commands to processing dictionary
void addDefaultHandler(void (*function)());    // A handler to call when no valid command received.
                      ...
 
But I have the same problems. If I comment out NewSoftSerial NSSerial(2, 3); in main.cpp or library.cpp I got the redefinition errors and if I comment out this line in both files, I got "multiple definition of `NSSerial' " error again. I was trying to figure out the "the stretegy" and read a lot about writing libraries. I understand that my library isn't a good example of implementing С++ function). Thanks for you help.
« Last Edit: November 15, 2011, 07:21:37 am by tunz » Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1926
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

tunz
The idea in C/C++ is that you can only define stuff once but you can declare it multiple times.
You declare variables with the extern statement telling that the variable is not defined here but externally. This leads you with the declaration but not with the definition.
You still need to define it once.

If you look at it from a code perspective a header can be included many times. So you should only "declare" variables and functions. If you define variables or methods you'll get the error "multiple definition of XXX" you are talking about.
The analogy is the same as with functions
void Myfunction(); // defines a function
void Myfunction(){;}// implements the function

As to the behavior you are describing. Are you sure you saved the files properly each time before compiling? Because this should work (I just tested it here to be 100% sure)

Best regards
Jantje

PS note that the C++ inline is a exception to this rule. It is possible because the inline code is copied  by the preprocessor in the real code.
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

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

Understood the idea, more or less, thank you Jantje. Mistake was in header file:
extern NewSoftSerial NSSerial(2, 3); -> extern NewSoftSerial NSSerial;
Now everything working and it's great! Thanks a lot for your attention and help!
Logged

Pages: [1]   Go Up
Jump to: