Issue to adapt a function regarding my code


dear all,
i sorry bore appreciate if can me adapt function. after this, hope  :), able continue.

it nice you.

i have adapt sendatcommand() function answer.

first let me show again how started library.
sim908.h
code: [select]

/*
  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, not copy code because work. copy 2 concerned function , constructor. first because contain call sendatcommand("at", "ok", 2000);

the problem int8_t sendatcommand(char* atcommand, char* expected_answer, unsigned int timeout);

code: [select]

// 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...]
// @ 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 sim908.\n")); 
}
    while(answer == 0){     // send @ every 2 seconds , wait answer

    answer = sendatcommand("at", "ok", 2000);
       
       
    }
    if(_debug){
    serial.print(f("sim908 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;
   
    // debug. display "ok" , "2000"
    serial.println(expected_answer);
    serial.println(timeout);

    memset(response, '\0', 100);    // initialice string
   
    delay(100);
   
    while( serial.available() > 0) serial.read();    // clean input buffer
   
    if (atcommand[0] != '\0')
    {
       // display "at"
        serial.println(atcommand);    // send @ command
    }

    x = 0;
    previous = millis();

    // loop waits answer
    do{
        if(serial.available() != 0){    // if there data in uart input buffer, reads , checks asnwer
            response[x] = serial.read();
            serial.print(response[x]);
            x++;
            if (strstr(response, expected_answer) != null)    // check if desired answer (ok) in response of module
            {
                answer = 1;
            }
        }else{
        if(_debug){
         serial.println(f("no data in uart"));
         }
        }
    }while((answer == 0) && ((millis() - previous) < timeout));    // waits asnwer time out

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

    return answer;
}


when upload code, terminal retuning me
quote
no data in uart

and
code: [select]
if(_debug){
serial.println("");
serial.println(answer);
serial.println("");
}

is displaying
quote


0



so, supposed that
code: [select]
serial.available()and
code: [select]
serial.read()
is not reading serial port. think, it's because use _cell (see in constructor)
in .ino file, have:
code: [select]
sim908 sim908(new softwareserial(2, 3), baud_rate, pintopoweronmodule, debug);


then try replace
code: [select]
serial.available()and
code: [select]
serial.read()
by
code: [select]
_cell.available()and
code: [select]
_cell.read()
but generate error:
quote

/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 member 'available' in '((sim908*)this)->sim908::_cell', of non-class type 'softwareserial*'

so, wunder if declared
code: [select]
softwareserial* _cell;
in sim908.h file???

i bit lost...

here sendatcommand() sim908.cpp file
code: [select]

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 string
   
    delay(100);
   
    while( serial.available() > 0) serial.read();    // clean input buffer
   
    if (atcommand[0] != '\0')
    {
        serial.println(atcommand);    // send @ command
    }

    x = 0;
    previous = millis();

    // loop waits answer
    do{
        if(serial.available() != 0){    // if there data in uart input buffer, reads , checks asnwer
            response[x] = serial.read();
            serial.print(response[x]);
            x++;
            if (strstr(response, expected_answer) != null)    // check if desired answer (ok) in response of module
            {
                answer = 1;
            }
        }else{
        if(_debug){
         serial.println(f("no data in uart"));
         }
        }
    }while((answer == 0) && ((millis() - previous) < timeout));    // waits asnwer time out

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

    return answer;
}


my god, hope clear , fell free ask me more.
thank lot, adapt sendatcommand() function or higlight errors

cheers

does work?

code: [select]
_cell->available()
_cell->read()



Arduino Forum > Using Arduino > Programming Questions > Issue to adapt a function regarding my code


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support