# strstr function strange behaviour in PIC18F

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)
{

{
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
{
}

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.

• 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. – David Aug 2 '16 at 7:01
• 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. – Lundin Aug 9 '16 at 14:45

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);


Maybe this is the problem:

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



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.

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

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.

• 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. – S Andrew Aug 1 '16 at 10:43
• "+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 – Flanker Aug 1 '16 at 11:10

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.