• AVR Freaks

AnsweredHot!Why my loop runs twice?

Author
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
2018/08/30 06:15:47 (permalink)
1 (1)

Why my loop runs twice?

I'm trying to read from a gsm sim800,DSPIC33FJ64GP206A,XC16.
Here is my code:
 
    
bool GsmCommand(char * command, char * ResponseText, uint16_t Timeout)
{
    
    uint16_t GsmCounter=0;
  
    ClearGsmBuffer();
    Eusart1_Write(command);
    __delay_ms(20);
    while(!strstr((char *)Eusart1_Received.bytes,ResponseText))
    {
        GsmCounter++;
        if(GsmCounter>Timeout)
        {
          //ClearWifiBuffer();
          return false;
          break;
        }
    }
    return true;
}


 void ReadSms(void)
 {
     
    if(GsmModule.isReady && Sim.isReady && Network.isReady)
    {
        
        GsmCommand(CMGF,OK,NORMALTIME);
        int i;
        for(i=0;i<=20;i++)
        {
            sprintf(txt,"%s%d\r\n",READSMS,i);
            if(GsmCommand(txt,MESSAGEISEXIST,LONGTIME))
            {
               
               if(NumberIsValid(i))
               {
                //GsmCommand(DeleteSms(i),OK,NORMALTIME);
               }
           }
        }
    }
 }
 
 bool NumberIsValid(int Index)
 {
     
    char *p;
    for ( p = strtok((char *)Eusart1_Received.bytes,"\""); p != NULL; p = strtok(NULL, "\""))
        {
            
            if(strstr(p,"+")&&strlen(p)==13)
            {
               strcpy(MobileNumber, p);
               sprintf(txt,"%s,%d",MobileNumber,Index);

                Eusart2_Write(txt);
               __delay_ms(20);

               return true;
               break;
            }
        }
    return false;
     
 }




When i write to uart and send it to my pc, I see that it not read all numbers.
Here is my terminal output.
 
+3069xxxxxxxx,3
+3069xxxxxxxx,5
+3069xxxxxxxx,7
+3069xxxxxxxx,9
+3069xxxxxxxx,11
+3069xxxxxxxx,13
+3069xxxxxxxx,15
+3069xxxxxxxx,17
+3069xxxxxxxx,19
As you can see it skips one sms in each read.
#1
rodims
Super Member
  • Total Posts : 1491
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: online
Re: Why my loop runs twice? 2018/08/30 06:47:35 (permalink)
+1 (1)
How should we know which data you receive from from your GSM ?
It would be so easy just to step through your code with the debugger, why don't you try that ?
#2
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Why my loop runs twice? 2018/08/30 06:56:45 (permalink)
0
I dont know how, but this routine GsmCommand it influences my loops
#3
rodims
Super Member
  • Total Posts : 1491
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: online
Re: Why my loop runs twice? 2018/08/30 08:18:51 (permalink)
+1 (1)
What is it what you do not know ?
GsmCommand does not influence your 'for' loop, it influences your output.
Use your debugger.
#4
moser
Super Member
  • Total Posts : 481
  • Reward points : 0
  • Joined: 2015/06/16 02:53:47
  • Location: Germany
  • Status: offline
Re: Why my loop runs twice? 2018/08/31 01:34:02 (permalink) ☼ Best Answerby dimis1989 2018/08/31 04:34:52
+2 (2)
How do you know for sure that for the even numbers if(GsmCommand(txt,MESSAGEISEXIST,LONGTIME)) must be successful?
How do you know for sure that for the even numbers Eusart1_Received.bytes contains the expected string in the expected valid format (quotes, starting with +, length 13)?
 
There are three points which stop you from reaching the debug output line:
if(GsmCommand(txt,MESSAGEISEXIST,LONGTIME))

for ( p = strtok((char *)Eusart1_Received.bytes,"\""); p != NULL; p = strtok(NULL, "\""))

if(strstr(p,"+")&&strlen(p)==13)

Each of those lines has the power to prevent the output Eusart2_Write(txt). And you did not show us your input.
#5
Jump to:
© 2019 APG vNext Commercial Version 4.5