Problem include library softwareSerial

My code:
Prova.ino :

#include "ll.h"
#include "ll.cpp"
#include "ini.h"
#include <SoftwareSerial.h>

void setup() {
mySerial.begin(9600);
}

void loop() {

}


ll.h

#ifndef LL_H
#define LL_H

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2,3);

#endif


ll.cpp

#include "ll.h"
#include "ini.h"

ini.h

#ifndef INI_H
#define INI_H

#endif

ini.cpp

#include "ll.h"
#include "ini.h"

Error:
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
ll.cpp.o: In function __static_initialization_and_destruction_0': /ll.h:6: multiple definition of mySerial'
ini.cpp.o:/ll.h:6: first defined here
prova.cpp.o: In function loop': C:\Program Files (x86)\Arduino/prova.ino:12: multiple definition of mySerial'
ini.cpp.o:/ll.h:6: first defined here

I can not get out of this situation, I have to call "myserial" from various parts.
Tanks

You must NOT create instance of classes in header files. You declare variables in header files. You initialize them in source files.

You are far better off declaring mySerial in the sketch, and passing a reference or pointer to it to the constructor of your class.

PaulS:
You must NOT create instance of classes in header files. You declare variables in header files. You initialize them in source files.

You are far better off declaring mySerial in the sketch, and passing a reference or pointer to it to the constructor of your class.

Thanks for the reply, I understand what the problem is.
Could you give me an example of how to pass mySerial?
Thanks

My code:

prova.ino

#include "ll.h"
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);
rs_485 rtx_485; //Inizializza la classe

void setup() {
mySerial.begin(9600);
}

void loop() {
rtx_485.rxd(mySerial.read());
}


ll.h

#ifndef LL_H
#define LL_H

#include <arduino.h>

class rs_485
{
private:

public:
rs_485(); //Costruttore
void rxd(byte rx);
void txd(); //not work
};
#endif

ll.cpp

#include "ll.h"

//Costruttore
rs_485::rs_485()
{

}

void rs_485::rxd(byte rx)
{
//...
}

void rs_485::txd()
{
mySerial.write(2); //Not work
}

error:

ll.cpp: In member function 'void rs_485::txd()':
ll.cpp:16: error: 'mySerial' was not declared in this scope

See if something like this works for you. I have put the class in the same file as the program for convenience; you should be able to move it into the library files.

The instance of SoftwareSerial is passed to the constructor as a reference.

You had not shown the definition of rxd and txd, so I have assumed they should work like read and write methods on serial - rxd returns one byte or -1 if none available, txd sends one byte. You may want to add an available method.

Note: compiles OK but not tested.

#include <SoftwareSerial.h>

class rs_485
{
  private:
    SoftwareSerial& _port;
    
  public:
    rs_485(SoftwareSerial& port): _port(port)
    {
      
    };
    int rxd()
    {
      if (_port.available())
      {
        return _port.read();
      }
      else
      {
        return -1;
      }
    };
    void txd(byte b)
    {
      _port.write(b);
    };
};


SoftwareSerial mySerial(3, 4);

rs_485 myRS485(mySerial);


void setup()
{
}

void loop()
{
}

:smiley: Grazie, adesso funziona!
Thank you, now it works!