0
\$\begingroup\$

I am using PIC18F2520 and C18 compiler. I am trying to communicate with SIM900. I am using interrupt for receiving data. So when I am sending AT+CMGF? SIM900 response with +CMGF=1. I am storing this data in a buffer inside ISR and then calling a function to check.

void rx_handler(void)
{
  Rx[k] = ReadUSART();   //read byte and save it in Rx


  if(Rx[k]=='\r')        //if carriage return is received, stop reading
  {
     LATCbits.LATC2=1;   // turn on led to indicate we have received everything

     process();         // call function to check the received data
  }
  k++;                   //increment k
}

void process()
{
  if(strstr(Rx,"+CMGF:") != NULL)  //if rx data contains +CMGF 
  {
     UARTSend("Data Match");
  }
  else
  {
    UARTSend("Data not found");
  }

  memset(Rx,0,sizeof(Rx);    //clear Rx
}

The problem I am facing is it doesnt match the data. But when I pause the debugger and see the contents of Rx, it shows +CMGF: 1. I don't know why strstr function not working. I can also check each byte one by one but that would be very lengthy process. Can anyone please tell me the error. Please help.

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ Your code as shown would not compile due to the erroneous matching of parentheses in memset(). It would be best if you showed the original code. \$\endgroup\$ – David Aug 2 '16 at 7:01
  • \$\begingroup\$ You obviously have to null terminate the strings or strstr will run off into la-la-land. It is not obvious from this code if the buffer is initialized to all-zeroes or not. \$\endgroup\$ – Lundin Aug 9 '16 at 14:45
1
\$\begingroup\$

As everyone has already answered the question correct. I would like to add one more point. I found this strange while dealing with C18 Compiler

If the function is not working as expected try including const far rom char

strstrrampgm(Rx,(const far rom char *)"+CMGF:") !=NULL);

Like for sprintf

sprintf(data,(const far rom char *)"Hello %s",str);
\$\endgroup\$
1
\$\begingroup\$

Maybe this is the problem:

Rx[k] = ReadUSART();   //read byte and save it in Rx
k++;                   //increment k

if(Rx[k]=='\r')        //if carriage return is received, stop reading

By the time you get to the if statement, you have already incremented k, so you are not checking the character you just read, but whatever rubbish is in the buffer from the last time.

\$\endgroup\$
2
  • \$\begingroup\$ But the screenshot which I have attached shows that the data is received in the Rx. \$\endgroup\$ – S Andrew Aug 1 '16 at 7:59
  • \$\begingroup\$ Ok I got it what you are saying & I have edited the code but my question is regarding strstr function. \$\endgroup\$ – S Andrew Aug 1 '16 at 8:02
1
\$\begingroup\$

As you can see from serial monitor, "+CMGF" is preceded with "\r\n". Now imagine that your code finds first '\r' and then calls process(), which in turn clears buffer with memset (regardless of strstr result). But it doesn't resets 'k' index variable, so next received char will be placed NOT at buffer start but somewhere in the middle where k was. This leads that buffer will have zero as 1st char - it is empty string and that is why strstr is "not working" (@DoxyLover pointed that out).

Adding k=0; after memset shall fix this.

Also after Rx[k] = ReadUSART(); add Rx[k+1] = 0; to have properly terminated string. Also buffer overflow checks have to be added.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks. The problem I am facing is I dont know how to check if the complete data is received or not. As I am interacting with SIM900, few commands ends with OK but few commands doesnt respond with OK. Like if I send a msg to GSM modem, it replys with +CMTI: "SM",2 and this command doesnt end with OK. So I am confused how to check it I have received the complete data or not. what should I put inside if condition. Can you please tell me. \$\endgroup\$ – S Andrew Aug 1 '16 at 10:43
  • \$\begingroup\$ "+CMTI:" is unsolicited message, it could be received any time, IIRC even during another commands; Do you have commands manual - "SIM900_AT Command Manual_V1.11.pdf" from simcom.ee/documents/?dir=SIM900 - there is description on every command and what responses could be expected; note that some responses have several lines ("\r\n"); you should check for "\r\n" pair, not just '\r' (SMS body text might have many '\r'); general idea is you perform checks when line end detected - if it is the one of valid responses (complete response); timeout check shall be last line of defense \$\endgroup\$ – Flanker Aug 1 '16 at 11:10
0
\$\begingroup\$

Add a null termination after you get '\r' and re-check. If that also doesn't work, try with writing your own implementation to check if it is a problem with the library. I have had similar expecience library functions in MPLAB while using strstr. Writing my own implementation solved the issue.

\$\endgroup\$

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.