How can use a value from a class in a cpp file

Good morning,

I am still blooking on a issue.

I am using a library and this

SoftwareSerial mySerial(2, 3);  //rx, tx

is not in my sketch code but in a GSM_Shield.cpp file.

The best would be to modifiy the class GSM::xxx(){} bt until not I am not able to do usccessefully.

morever, if in my skect code I am writin somethink ike

mySerial.write()

it return me an error message stating that mySerial is not declare in this code, but it is in GSM_Shield.cpp.

So from my skect code (file.ino) how can use mySerial?

It's not something like "gsm->mySerial"?

Is mySerial declared public or private?

And doesn't the GSM class provide its own functions for accessing the serial?

If a class, SomeClass, creates an instance of SoftwareSerial, using:

SoftwareSerial mySerial(2,3);

as a public field, and you create an instance of the class:

SomeClass myInstance;

then you can use the SoftwareSerial instance from the class instance:

myInstance.mySerial.print("this will work");

If the instance in SomeClass is private or local you can NOT access that instance from the sketch.

pierrot10:
The best would be to modifiy the class GSM::xxx(){} bt until not I am not able to do usccessefully.

Depending on the design of the library, it may be possible to solve the problem without modifying the library source code. However, it is certainly possible to modify the source code and if you just want to change the pins used for a SoftwareSerial port or replace a HardwareSerial with a SoftwareSerial then that should not be particularly difficult to achieve.

Dear All,
Thanks for all of your answers.

I do not how to answer correctly.
First, here is my library : GitHub - jgarland79/GSM_Shield: GSM Library for the SIM900 chip and the GBoard Arduino GSM development board

@mahenko:
I do not know exactely. In the GPRS_Shield.h file, the class is starting like this:

class GSM
{
  public:

Is there no private statement.
Apparently there is no function to access the serial. The serial is on GPRS_Shild.cpp and shoed like this:

SoftwareSerial mySerial(2, 3);  //rx, tx

I would say no for your two question

I also can see this

GSM::GSM(void)
{
  // set some GSM pins as inputs, some as outputs
  pinMode(GSM_ON, OUTPUT);               // sets pin 5 as output
  pinMode(GSM_RESET, OUTPUT);            // sets pin 4 as output

  //pinMode(DTMF_OUTPUT_ENABLE, OUTPUT);   // sets pin 2 as output
  // deactivation of IC8 so DTMF is disabled by default
  //digitalWrite(DTMF_OUTPUT_ENABLE, LOW);
  
  // not registered yet
  module_status = STATUS_NONE;

  // initialization of speaker volume
  last_speaker_volume = 0; 
}

to wath it make a relation?

@PaulH
I would say no to your point of view.
In my sketch file, I can see

GSM gsm;

and then some other code like:

gsm.TurnOn(9600);
gsm.PickUp();
gsm.Call(number);

but then, when I try

gsm.mySerial.write()

nothing happen

I think , in GPES_Shiled.cpp, I should create somethink like this?

void GSM::mySerial(void)
{
SoftwareSerial mySerial(2, 3);  //rx, tx

 {

I think , in GPES_Shiled.cpp, I should create somethink like this?

No. The class is already creating an instance of SoftwareSerial. You (think you) want access to that instance, not to create a new one.

Actually, that code creates a global instance of SoftwareSerial that is shared by all instances of the class. A really poor design, but one that you can exploit.

All you need to do is tell your sketch that mySerial is an extern instance of SoftwareSerial:

extern SoftwareSerial mySerial;

Dear Paul,

Thnak again, I am going to try it

A really poor design

But do you know a good library with work with SIM900 and SIM908?

My need are first

  • Collect GPS coords
  • Send data to a remote server to store it in MySQL
  • Delete, Send and read received sms
  • Call and get call (not priority)
  • Write and read SIM phone book

I do not know why, but this library GitHub - jgarland79/GSM_Shield: GSM Library for the SIM900 chip and the GBoard Arduino GSM development board was working but now it does not. Something look to me strange as well.