problem with reciving answer from sim800

STP.Asghari

New Member
When I give the AT command to the module. The module should give me the answer AT\r\nOK\r\n but to my surprise it gives the answer AT\r\r\nOOK\r\n. I understood this problem by getting the answer from the code below. While if I delete the wrong lines from the code below, the module does not respond.
Code:
Send_AT();
    do
    {
      buffer=get_char();
    }while(buffer!='A');
    
    
        buffer=get_char();
        if(buffer=='T')
                   {
                       buffer=get_char();
               if(buffer=='\r')
                          {
                             buffer=get_char();
                             if(buffer=='\r')
                                  {                             
                                      buffer=get_char();
                                      if(buffer=='\n')
                                              {
                                                  buffer=get_char();
                                                   if(buffer=='O')
                                                           {
                                                                 if(buffer=='O')
                                                                    {
                                                                           buffer=get_char();
                                                                           if(buffer=='K')
                                                                                {
                                                                                     buffer=get_char();
                                                                                     if(buffer=='\r')
                                                                                           {                                                                                             
                                                                                             buffer=get_char();
                                                                                             if(buffer=='\n')
                                                                                                    {                                                                                   
                                                                                                                   for(i=0;i<5;i++)
                                                                                                                    {
                                                                                                                            HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
                                                                                                                            HAL_Delay(500);       
                                                                                                                            HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
                                                                                                                            HAL_Delay(500);
                                                                                                                    }
                                                                                                    }
                                                                                           }
                                                                                }
                                                                    }
                                                           }
                                              }
                                  }                       
                          }       
                   }
 
The second "if(buffer=='O')" does not have the get_char() line before it, so it's looking a the previous character again.

I'd advise not using nested character checks like that, especially the first bit:
do
{
buffer=get_char();
}while(buffer!='A');


If anything goes wrong, the program will be stuck in that loop forever, with no recovery.
"Blocking" code is a bad idea for anything other than really trivial programs...


I'd use a routine that collects characters until the end-of-line, then stores the string in an array, on a continuous basis (eg. in an interrupt routine).

Use a state machine (ie. a byte to track what's happening) & look for any new strings received each pass through your main program.

When one is received, do a string comparison against possible keywords.
eg. Idle is state 0, "AT" seen go to state 1, state 1 & "OK" seen take whatever action and reset to state 0 for the next data.
 
Thank you for guiding me. you are right. I had added an if(buffer=='O') which made me think the module would send the O character twice.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…