0
\$\begingroup\$

Hi all I am interfacing with GSM SIM900 with atmega32. I am trying to extract the message contents. User will send message like CHANGE7532167845 to SIM installed in GSM device. So to extract this I am using for loop followed by many if conditions. All I need to do is to save the number in a another variable. Following is my code:

for(int k =0; smscontent[k]!=NULL;k++)
        {
            if (smscontent[k] == 'C')
            {
                if (smscontent[k+1] == 'H')
                {
                    if (smscontent[k+2] == 'A')
                    {
                        if (smscontent[k+3] == 'N')
                        {
                            if (smscontent[k+4] == 'G')
                            {
                                if (smscontent[k+5] == 'E')
                                {


                                    if(isdigit(smscontent[k+6]))
                                    {
                                        serial_Tx("MATCH");

                                    }

                                }
                            }

                        }
                    }
                }


            }
            }

My control is reaching isdigit because on terminal I am getting MATCH but after that what I should do to save number in a variable.? Please help

serial_Tx function

void serial_Tx(char *str)
{
    for (unsigned int i=0;str[i]!=0;i++)
    {
        UDR=str[i];
        while(!(UCSRA&(1<<UDRE))); 


    }


}
\$\endgroup\$
0
\$\begingroup\$

You should check out some basic string handling tutorials.

I would suggest using strstr (C) or string::find (C++) instead of an elaborate if-if-if.

//C++
std::size_t found = smscontent.find("CHANGE");
if (found!=std::string::npos){
   std::string valueString = smscontent.substr(found+6/*strlen("CHANGE")*/);
   int value = std::stoi(valueString,nullptr);
}

I actually have no idea if the code above compiles or is anything like you're searching for.

//C
char buffer[20] = {'\0'};
int value = 0;
char * pposition = strstr(smscontent,"CHANGE");
ptrdiff_t position = pposition - smscontent;

if(pposition != NULL){
   strcpy(buffer, pposition);
   value = atoi(buffer);
}

There might be way easier options though, especially when using C++ or sscanf (if possible)

simply:

sscanf(smscontent,"CHANGE%u",value);

and the value will be saved in the variable: "value"

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ ok so you mean to say by using strstr we can search for CHANGE and then using pointer, we will copy it in buffer, is it? \$\endgroup\$ – anna carolina Jul 30 '15 at 12:47
  • \$\begingroup\$ error: unknown type ptrdiff_t, do we need to include any header file for it? I think cstddef.h need to be included and this is for C++ I am in C \$\endgroup\$ – anna carolina Jul 30 '15 at 12:49
  • \$\begingroup\$ it's in stdio.h for me, but it's typedef int ptrdiff_t; so you could get away with a regular int I guess. \$\endgroup\$ – Paul Jul 30 '15 at 13:48
  • \$\begingroup\$ hey I have extracted the number and saved it in newnum which is a char. Now I am doing sprintf(call,"ATD%s;\r\n",newnum); and then serial_Tx(call); where serial_Tx is a function which transmit data serially but I am not getting any call. \$\endgroup\$ – anna carolina Jul 31 '15 at 4:59
  • \$\begingroup\$ %s would specify a string (char array)? And newnum was a char. Also, an (unsigned) char can only hold up values from 0 to 255. And be sure to send the serial string to the SIM900 modem and not just to your terminal/debug/screen \$\endgroup\$ – Paul Jul 31 '15 at 6:14

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.