0

Thank you for everyone's help in trying to find a solution. This library I'm working with was prewritten and is one of the most buggy hardware libraries I've had the misfortune to use. I have now solved to problem by reusing the response parsing code from the library and rewriting my own sequence to return the number using a completely different approach. Many Thanks again!

I am currently attempting to understand how this function works and am struggling. I want to be able to access the 'number' variable in my main function but currently when I do that I get nothing (as in "") when I try to printout the variable with:

Serial.println(incomingNumber);

I am currently preassigning the number as:

char incomingNumber[14];

at the top of my project file. With a class initial setup of:

DFRobot_SIM808 sim808(&Serial);

I'm then later calling the function like this:

if(sim808.isCallActive(incomingNumber)){Serial.println(incomingNumber);}

This seems like I'm currently making a fundamental error in understanding data types but I'm a bit lost so any help would be really appreciated.

I havent included my entire project here because its currently quite long and spread across both the main and some library files but if anyone needs more info please ask and I'll upload the extras.

The function from the library I'm working with is included below:

bool DFRobot_SIM808::isCallActive(char *number)
{
char gprsBuffer[46];  //46 is enough to see +CPAS: and CLCC:
char *p, *s;
int i = 0;

sim808_send_cmd("AT+CPAS\r\n");
/*Result code:
    0: ready
    2: unknown
    3: ringing
    4: call in progress

  AT+CPAS   --> 7 + 2 = 9 chars
            --> 2 char              
  +CPAS: 3  --> 8 + 2 = 10 chars
            --> 2 char
  OK        --> 2 + 2 = 4 chars

  AT+CPAS

  +CPAS: 0

  OK
*/

sim808_clean_buffer(gprsBuffer,29);
sim808_read_buffer(gprsBuffer,27);
//HACERR cuando haga lo de esperar a OK no me har閾哸 falta esto
//We are going to flush serial data until OK is recieved
sim808_wait_for_resp("OK\r\n", CMD);    
//Serial.print("Buffer isCallActive 1: ");Serial.println(gprsBuffer);
if(NULL != ( s = strstr(gprsBuffer,"+CPAS:"))) {
  s = s + 7;
  if (*s != '0') {
     //There is something "running" (but number 2 that is unknow)
     if (*s != '2') {
       //3 or 4, let's go to check for the number
       sim808_send_cmd("AT+CLCC\r\n");
       /*
       AT+CLCC --> 9

       +CLCC: 1,1,4,0,0,"656783741",161,""

       OK  

       Without ringing:
       AT+CLCC
       OK              
       */

       sim808_clean_buffer(gprsBuffer,46);
       sim808_read_buffer(gprsBuffer,45);
        //Serial.print("Buffer isCallActive 2: ");Serial.println(gprsBuffer);
       if(NULL != ( s = strstr(gprsBuffer,"+CLCC:"))) {
         //There is at least one CALL ACTIVE, get number
         s = strstr((char *)(s),"\"");
         s = s + 1;  //We are in the first phone number character            
         p = strstr((char *)(s),"\""); //p is last character """
         if (NULL != s) {
            i = 0;
            while (s < p) {
                number[i++] = *(s++);
            }
            number[i] = '\0';            
         }
         //I need to read more buffer
         //We are going to flush serial data until OK is recieved
         return sim808_wait_for_resp("OK\r\n", CMD); 
       }
     }
  }        
} 
return false;
}
5
  • On the face of it, what you are doing is perfectly correct. Either the library isn't detecting the phone number correctly or the module you are using doesn't report the phone number. – Majenko Apr 17 '20 at 18:09
  • so the gsm module is on Serial or the Serial Monitor? 14 is the size of the array, not the value. the char array content is uninitialized. – Juraj Apr 17 '20 at 18:47
  • From what I understand is it generally better to initialize the array size at the maximum, even if the end product is much shorter? – AidenDawn Apr 17 '20 at 20:26
  • you write "I am currently preassigning the number as char incomingNumber[14];" – Juraj Apr 18 '20 at 12:45
  • Yeah, DFRobot libraries SUCK. For example, I had to rewrite their DFPlayerMini MP3 player library cause it was so terrible – P_B Apr 18 '20 at 23:05
-1

You can't use Serial.println(array);. In fact, I'm not even sure how the compiler even let you do this...

A better approach would be to use Serial.write(array, sizeof(array));

Also, in order to ensure the function is even executing that portion of the code in the first place, you should but some debugging prints in the code. Like this:

     if (NULL != s) {
        i = 0;
        while (s < p) {
            number[i++] = *(s++);
            Serial.write(number[i++]); //-------- debug
            Serial.print(' '); //-------- debug
        }
        number[i] = '\0';     
        Serial.println(); //-------- debug
     }

Also, proper use of whitespace is your friend ;)

1
  • You can't use Serial.println(array); -- Yes you can. It's just a C string that's NULL terminated. There's absolutely nothing wrong with that. – Majenko Apr 17 '20 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.