CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

gsm modem interfacing with PIC controller

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dxn



Joined: 03 Jun 2015
Posts: 4

View user's profile Send private message

gsm modem interfacing with PIC controller
PostPosted: Wed Jun 03, 2015 3:47 am     Reply with quote

hi
we are interfacing sim 300 with PIC18f.
if i m sending at commands from microcontroller to modem , it is working perfect. we monitor via hyperterminal.
but if i m checking for 'OK' after each at command, it receives only for 1st at command.
here is my code

Code:

void check()
{
char string[6];
char data[]="OK";
gets(string);
 delay_ms( 1000 );

if(strcmp(string , data))
 printf("success \n");
 
 else
  printf("fail \n");

}


                                                             

void main()
{
char c;
int8 i;


 for(i=0;i<10;i++)
  delay_ms( 1000 );
  while(True)
  {
 
printf("AT");
printf("\r");
 check();

 /*
   
do
{
c=getc();

if (c== 'E')
{
   printf("AT");
   delay_ms( 10 );
   printf("\r");
   delay_ms( 10 );
  // c=getc();
}


}while(c != 'K'); 



*/

  delay_ms( 1000 );
 
 printf("AT+CMGF=1");
printf("\r");

 check();

  delay_ms( 1000 );
 
   
 }
}


for this code, i m getting output at hyperterminal as
Code:


+CPIN: READY
AT
OK
success
AT+CMGF=1
OK


ie, for at, it is receiving OK and reading string.
but for AT+CMGF=1, it is receiving OK. but controller couldnot read OK.
expected output was

AT+CMGF=1
OK
success

controller stuck with this instruction
gets(string);
it is getting OK command. but not reading.
is it the delay problem? may be OK reply coming fast?

we r connecting max323 rx to gsm tx and vice versa, just for monitoring purpose only.

please help us....
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Wed Jun 03, 2015 4:08 am     Reply with quote

You need to understand that the UART has just two characters of internal buffering.

You listen for, and receive the message "OK", and then stop listening to the UART. For one second the UART is sitting receiving, and after just two characters of the reply, it's internal buffer overflows. At this point the behaviour will depend on the 'ERRORS' setting. If this is set, then when the UART is next called, it'll work, return the first two waiting characters, but have lost the rests of the message, _and the line feed_ that 'gets' wants . If this is not set, the UART will be fully hung.....

So if the modem is sending anything extra, you have problems...

Look at ex_sisr.c
Or (easier with your code approach, but some caveats later), assuming you are using a V5 compiler, add buffering to your UART in the #USE RS232 declaration "RECEIVE_BUFFER=16". This will generate a 16 character automatic interrupt driven receive buffer for the UART RX.

However the caveats.
Gets, is a 'dangerous' way to handle data. If (for instance) a line feed character gets missed, it can sit waiting for ever. Also, if the line is longer than you expect, it can result in other memory being corrupted.
In 'input.c', there is an alternative 'get_string'. Still not terribly good, but a lot better. It allows you to add a limit to the string length.

There is also a caveat (which may be your problem), are you sure the unit just sends OK<LF>?. It may well send OK<LF><CR>.
Gets will then return at the line feed, and <CR? will be left in the UART. After the next function you then get:
<CR>OK<LF> which _won't_ match on a strcmp....

This is another danger of gets. You really need to write your _own_ function, and genuinely work out how to be sure you only record text characters, and return at the end of the line (look at isalnum()).
dxn



Joined: 03 Jun 2015
Posts: 4

View user's profile Send private message

PostPosted: Wed Jun 03, 2015 4:32 am     Reply with quote

thank you very much for reply

instead of string, we tried to read character wise.
Code:

do
{
c=getc();

if (c== 'E')
{
   printf("AT");
   delay_ms( 10 );
   printf("\r");
   delay_ms( 10 );
  // c=getc();
}


}while(c != 'K');


then also it is receiving k in 1st at command. but for 2nd at command, not receiving 'k'
whether we have to check timer overflow at the same time of reading character
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Wed Jun 03, 2015 1:53 pm     Reply with quote

Use your look for character approach, but use the buffering from EX_SISR. So call bgetc, not getc. You then have a chance.
dxn



Joined: 03 Jun 2015
Posts: 4

View user's profile Send private message

PostPosted: Thu Jun 04, 2015 6:55 am     Reply with quote

hi
We used the interrupt.
Now the program is not hanging. but now the problem is, sometime, it is not detecting 'K'. ie, last character of OK. So, even if the at command sends and got reply 'OK', it is not detecting 'K'. I checked whether 'K' is detected. If not, resend the AT command. Sometimes, even though 'OK' is coming, it is not detecting.
So while sending SMS, 2 -3 times, sms was sending.

Code:

#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;

#int_rda
void serial_isr() {
   int t;

   buffer[next_in]=getc();
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
}

#define bkbhit (next_in!=next_out)

BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}
/////////////////////////////////////  starts from here
void SendSmsAndCheckForOK()
{
char sms[20]="good morning" ;
unsigned char cReturnValue=0;
      cReturnValue = SendSMS(sms);
     
      if(cReturnValue != 0)
       {
        delay_ms( 1000 );
         cReturnValue = SendSMS(sms);
          delay_ms( 1000 );
         if(cReturnValue != 0)
            cReturnValue = SendSMS(sms);
      }
   
    delay_ms( 1000 );
      if(cReturnValue ==  0)
      printf("succ \r");
 
   


}

char SendSMS(char *sbytes)
{
char string[12]= "09999999999" ;

BYTE a;
printf("AT+CMGS=");
delay_ms( 1000 );
printf("\"");
delay_ms( 1000 );
printf(string);
delay_ms( 1000 );
printf("\"\r");
delay_ms( 1000 );
//printf("\r");
delay_ms( 1000 );
printf(sbytes);
delay_ms( 1000);
putc(CNTRLZ);

while(bkbhit)
{
do
{
 a=bgetc();
 if(a=='E')
   return 2;
}
while(a != 'K');
return 0;
}

}

temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jun 04, 2015 7:13 am     Reply with quote

you've only shown 'snippets' or parts of your program and we need to see 'main()' to determine what's going on.

jay
dxn



Joined: 03 Jun 2015
Posts: 4

View user's profile Send private message

PostPosted: Thu Jun 04, 2015 10:46 pm     Reply with quote

here is main
Code:


void main()
{
char d;
int8 i;


 enable_interrupts(int_rda);
  enable_interrupts(global);

 for(i=0;i<10;i++)
  delay_ms( 1000 );
 

 printf("AT");
printf("\r");


  delay_ms( 1000 );
 printf("AT+CMGF=1");
printf("\r");

delay_ms( 1000 );


SendSmsAndCheckForOK();


delay_ms( 1000 );


while(True)
  {
 
 }
}
liusse



Joined: 26 Apr 2015
Posts: 3
Location: Spain

View user's profile Send private message

PostPosted: Sun Jun 07, 2015 11:58 am     Reply with quote

You can use this
Code:

   printf("AT+CMGS=");
   delay_ms( 1000 );
   printf("\"");
   delay_ms( 1000 );
   printf(string);
   delay_ms( 1000 );
   printf("\"\r");
/////delay_ms( 1000 );
   //printf("\r");
/////delay_ms( 1000 );


///////////////////////////////////////////////////////////////////////////////////
// Before you send the text ,you must wait until receive the ">"character and the space character(">  ").
//////////////////////////////////////////////////////////////////////////////////

   printf(sbytes);
   delay_ms( 1000);
   putc(CNTRLZ);

Regards!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group