Issue to adapt a function regarding my code

Dear All,
I am sorry to bore you but I would really appreciate if you can help me do adapt that function. After this, I hope :), I will be able to continue.

It would be very nice from you.

I have to adapt a sendATcommand() function to get the answer.

First let me show you again how I started my library.
Sim908.h

/*
  Sim908.h - Library 
*/

#ifndef Sim908_h
#define Sim908_h

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

class Sim908{
	private:
		int _pinToPowerOnModule;
		//Stream* _cell;
		SoftwareSerial* _cell;
		int _debug;
		
	public:
		Sim908(SoftwareSerial* serial, int baud_rate, int pinToPowerOnModule, int debug);
		void powerOnSim908();
		int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout);
		void blinkLed(int lPin, int nBlink, int msec);

};

#endif

For Sim908.cpp, I do not copy all code because it work. I just copy the two concerned function and the constructor. The first is just because it contain the call to sendATcommand(“AT”, “OK”, 2000);

The problem is int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout);

// Contructor
Sim908::Sim908(SoftwareSerial* serial, int baud_rate, int pinToPowerOnModule, int debug)
{
	pinMode(pinToPowerOnModule, OUTPUT);
	_pinToPowerOnModule = pinToPowerOnModule;
	_cell = serial;
	_debug = debug;
	serial->begin(baud_rate);
	
};

[... code...]
// Look at sendATcommand("AT", "OK", 2000);
void Sim908::powerOnSim908(){
   uint8_t answer=0;

    digitalWrite(_pinToPowerOnModule,HIGH);
    delay(3000);
    digitalWrite(_pinToPowerOnModule,LOW);

	if(_debug){
		Serial.print(F("Powering up SIM908.\n"));  
	}
    while(answer == 0){     // Send AT every two seconds and wait for the answer

    	answer = sendATcommand("AT", "OK", 2000);
        
        
    }
    if(_debug){
	    Serial.print(F("SIM908 is powered on!\n"));  
	}
}


int8_t Sim908::sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){

    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;
    
    // For debug. It display well "OK" and "2000"
    Serial.println(expected_answer);
    Serial.println(timeout);

    memset(response, '\0', 100);    // Initialice the string
    
    delay(100);
    
    while( Serial.available() > 0) Serial.read();    // Clean the input buffer
    
    if (ATcommand[0] != '\0')
    {
       // It display "AT"
        Serial.println(ATcommand);    // Send the AT command 
    }

    x = 0;
    previous = millis();

    // this loop waits for the answer
    do{
        if(Serial.available() != 0){    // if there are data in the UART input buffer, reads it and checks for the asnwer
            response[x] = Serial.read();
            Serial.print(response[x]);
            x++;
            if (strstr(response, expected_answer) != NULL)    // check if the desired answer (OK) is in the response of the module
            {
                answer = 1;
            }
        }else{
        if(_debug){
         Serial.println(F("No data in UART"));
         }
        }
    }while((answer == 0) && ((millis() - previous) < timeout));    // Waits for the asnwer with time out

if(_debug){
Serial.println("");
Serial.println(answer);
Serial.println("");
}

    return answer;
}

When I upload the code, my terminal is always retuning me

No data in UART

and

if(_debug){
Serial.println("");
Serial.println(answer);
Serial.println("");
}

is displaying

0

So, I supposed that
Serial.available()and

Serial.read()

is not reading my Serial port. I think, it’s because I use _cell (see in my constructor)
In my .ino file, I do have:

Sim908 sim908(new SoftwareSerial(2, 3), baud_rate, pinToPowerOnModule, debug);

Then I try to replace
Serial.available()and

Serial.read()

by
_cell.available()and

_cell.read()

but it generate an error:

/Users/pierrot/Documents/Arduino/libraries/Sim908/Sim908.cpp: In member function ‘int8_t Sim908::sendATcommand(char*, char*, unsigned int)’:
/Users/pierrot/Documents/Arduino/libraries/Sim908/Sim908.cpp:86: error: request for member ‘available’ in ‘((Sim908*)this)->Sim908::_cell’, which is of non-class type ‘SoftwareSerial*’

So, now I wunder if I well declared

SoftwareSerial* _cell;

in my Sim908.h file???

I am a bit lost…

Here is my sendATcommand() from my Sim908.cpp file

int8_t Sim908::sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){

    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;
    
    Serial.println(expected_answer);
    Serial.println(timeout);

    memset(response, '\0', 100);    // Initialice the string
    
    delay(100);
    
    while( Serial.available() > 0) Serial.read();    // Clean the input buffer
    
    if (ATcommand[0] != '\0')
    {
        Serial.println(ATcommand);    // Send the AT command 
    }

    x = 0;
    previous = millis();

    // this loop waits for the answer
    do{
        if(Serial.available() != 0){    // if there are data in the UART input buffer, reads it and checks for the asnwer
            response[x] = Serial.read();
            Serial.print(response[x]);
            x++;
            if (strstr(response, expected_answer) != NULL)    // check if the desired answer (OK) is in the response of the module
            {
                answer = 1;
            }
        }else{
        if(_debug){
         Serial.println(F("No data in UART"));
         }
        }
    }while((answer == 0) && ((millis() - previous) < timeout));    // Waits for the asnwer with time out

if(_debug){
Serial.println("");
Serial.println(answer);
Serial.println("");
}

    return answer;
}

My god, I hope I was clear and fell you free to ask me more.
Thank a lot, to help my adapt sendATcommand() function or to higlight my errors

Cheers

Does this work?

_cell->available()
_cell->read()

Hello, I already tried this. The compilation do no display errors, but the terminl display the same result

... No data in UART No data in UART

Answer : 0

Expected answer: OK Timeout: 2000 Command: AT No data in UART ...

Answer shoud not return 0 :cold_sweat:

OK, so that fixed the syntax errors but there are other problems.

First, in sendATcommand(), presumably this is meant to be _cell and not Serial, and you have changed them.

Serial.println(ATcommand);    // Send the AT command
...
        if(Serial.available() != 0){    // if there are data in the UART input buffer, reads it and checks for the asnwer
            response[x] = Serial.read();

Second, the following statements should be removed from the constructor and put into a begin() method called in setup() (or maybe you could put them in powerOnSim908())

pinMode(pinToPowerOnModule, OUTPUT);
    serial->begin(baud_rate);

EDIT added a reference for this last comment

http://forum.arduino.cc/index.php?topic=111463.msg837662#msg837662

See last three messages in that thread.

I am wondering if the is another IDE that arduino (I have a Macbook) What do you with? Is there a comment to print the content of

_cell->begin

or

_cell.begin

and what the difference between -> and the dot

I ma very anoyed to be block with the atCommeand()

Thang for all can help me with this (I hope my description is clear enough)

Cheers

If the left-hand argument is a pointer to an object, -> is used.