1
\$\begingroup\$

Hello all I am trying to send sms using SIM900 and PIC18F2520. I have button attached to pin of MCU. So whenever the button is pressed, sms should be sent.Following is the code I am using to send SMS:

if(PORTCbits.RC1==1)
{
  DelayMs(10);
  if(PORTCbits.RC1==1)
  {
    UARTsend("AT+CMGS=\"99********\"\r ");
    DelayMs(100);
    UARTsend("Hello World");
    DelayMs(10);
    UARTsend("\x1A ");
    DelayMs(10);
 }
}

Now when I run it in debug mode, I receive sms. But when I program the device, what happens is it send AT+CMGS command and then send Hello world and \x1A. After sending all these things, then it shows >, that means now it wait for the message content. I dont know why it is happening. I have given the proper delay after AT+CMGS. I tried increasing the delay but again no changes. How to wait for > while sending sms. Please help

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Easiest way is to write a function which returns the latest character received on UART. And then block with a while loop: while(GSM_uart_recieve() != '>'); . Of course it can be improved with a timer to make a simple timeout functionality to ensure you won't stuck in the while loop if the module does not answer. \$\endgroup\$ – Bence Kaulics Jul 26 '16 at 8:40
  • \$\begingroup\$ You can handle it with and ISR as well, if you want a non-blocking solution. So you can check all received byte in the UART receive complete interrupt and set a flag if > is received and so on. \$\endgroup\$ – Bence Kaulics Jul 26 '16 at 8:43
  • \$\begingroup\$ Yes I have tried receiving it using if(ReadUSART()=='>') and then send sms. but it never enters in the if condition. I have read somewhere that sim900 also sends some <CR><LF> maybe because of these its not entering in if condition. How to process them \$\endgroup\$ – S Andrew Jul 26 '16 at 8:49
  • \$\begingroup\$ What is the return value of ReadUSART()? \$\endgroup\$ – Bence Kaulics Jul 26 '16 at 8:54
  • \$\begingroup\$ ReadUSART() return a byte from UART. I have partially solved the problem by increasing the delay to 5sec after sending AT+CMGS so now its working but I think thats not the proper solution to it. It should check if it reads > and then send the message. I am trying it. \$\endgroup\$ – S Andrew Jul 26 '16 at 8:56
1
\$\begingroup\$

If you are interested in receiving certain character on UART irrespective of time, then check UART interrupt section. You can do like this, -Enable UART reception interrupt. - In while(1) -Infinite loop -As soon as it receive data then check whether it is expected or not. (In your case it will be'>') -And when you receive the expected data you can either disable interrupt, stop communication or go for other task.

\$\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.