2

I am using PIC18F2520 and trying to communicate with GSM SIM900. I am using UART interrupt to receive data from the GSM. I am saving each byte in rxData. Now most of the commands ends with response OK. But there are few commands which does not respond with OK like when we receive sms notification from GSM, GSM send following notification

 +CMTI: "SM",2

Now this response doesn't have any OK. How to receive data in this case. In my application I need to check for the received sms. I am using following code:

void rx_handler(void)
{
  rxData[index] = ReadUSART();

  if(<some condition>)    //condition to check for sms notification
  {
    rxFlag = 1;        //set flag to process it in main loop

  }
  index++;

PIR1bits.RCIF==0;

}

I am getting confused on how to check for the sms notification. Till now for other commands, I was setting condition for OK but this doesnt have any OK response. Please help.

enter image description here

Bence Kaulics
  • 6,353
  • 12
  • 33
  • 60
S Andrew
  • 621
  • 10
  • 29
  • Maybe all commands (both the OK responses and unsolicited ones) really end up with a newline ('\n' or '\r' or "\r\n").. Then it will be easy to know when either message ends.. – m.Alin Aug 02 '16 at 11:12
  • yes I have tried checking for `\r \n` but what happens is lets say I send `AT\r` so it stops after this command and doesnt wait for response. I checked into the buffer while debugging and found out that it stores ` A T \r \r \n` so due to last `\r \n` it stops after sending command and doesnt wait for response from sim900 – S Andrew Aug 02 '16 at 11:16
  • From the [AT command manual](http://simcom.ee/documents/SIM900/SIM900_AT%20Command%20Manual_V1.11.pdf): "Commands are usually followed by a response that includes " – m.Alin Aug 02 '16 at 11:17
  • @m.Alin I have added an image. You can see I have send `AT+CMGF?\r` command. After the command it adds `\r \n`. So if I check for `\r\n` it will stop here only and will not proceed further – S Andrew Aug 02 '16 at 11:20
  • 1
    So just ignore the first `\r\n` and look for the second pair of `\r\n` you receive.. – m.Alin Aug 02 '16 at 11:23
  • 1
    The SIM900 echos back the command it received, this feature can be disabled. I found it pretty annoying in some cases. But all the same, there will be two `\r\n` sequences, acknowledge the first and stop and the second as Alin has told you. – Bence Kaulics Aug 02 '16 at 11:43
  • As far as I know, SIM900 has a pin called RI. The module gives a specific signal (described in datasheet) when an unsolicited response is given. – Bence Kaulics Aug 02 '16 at 11:50
  • @BenceKaulics @m.Alin Thanks. I used this double checking method for normal commands and it is working. I just want to confirm that for unsolicited commands, I still have to check for the 2nd pair of `\r\n` or those command will only respond with `\r\n` only. – S Andrew Aug 02 '16 at 13:18
  • I suppose those responses are the same as well, so you should check. Please if you have implemented a working solution, post it as an answer, same for your previous questions. – Bence Kaulics Aug 02 '16 at 13:41
  • @BenceKaulics I have added the code in the answer. Please have a look at the logic. Although its working fine but if you found any mistake, please tell me. – S Andrew Aug 02 '16 at 13:50

1 Answers1

1

Thanks to @Bence Kaulics & @m.Alin. I have solved the issue by double checking the \r \n. Following is the code I used:

volatile int flag = -1;   
volatile int sFlag = 0; 
volatile int uFlag = 0;

void rx_handler(void)
{
    rxData[index] = ReadUSART();

    if(rxData[index]=='\n' && rxData[index-1]== '\r' )
    {
      flag = flag + 1;

      if(flag == 1)    
      {
         sFlag = 1;
      }
    }

}

index++;

PIR1bits.RCIF==0;

}

I have used a flag = -1. So when I first got \r \n value of flag is 0. And again when I got \r \n that means we have detected a line end and this is what we are expecting so set sFlag = 1. When sFlag = 1 that means we have received the data and now we can process it.

S Andrew
  • 621
  • 10
  • 29
  • Code looks okay. But i think unsolicited reponse have onlt one \r\n. You will also have to add a condition for it. – Aircraft Aug 02 '16 at 14:00