Click here to Skip to main content
14,303,133 members
Rate this:
Please Sign up or sign in to vote.
See more:
Dear Sir,

I communicated GSM Modem (SIMCOMM300) from PIC16F877A and can able to send and recieve SMS/Calls fine. But, when i issue AT+CLCC command when the GSM receives RING, to know the caller number I can able to receive the response to the "AT+CLCC" command and also I can able to process all the commands followed by "AT+CLCC". But whats the problem is I can't able to process another incoming call (RING) or SMS. Program hangs. I don't know where the problem is. So, I give you the code here for your convenience. Pl go thro' the code and correct me if there is any error. I will be very thankful and humble to all who provide help. Advance thanks.
#include <stdio.h>
#include <htc.h>
#include "usart.h"
#include "lcd.h"
#include "string.h"

__CONFIG(HS & WDTDIS & UNPROTECT &  LVPDIS);

unsigned char gsmInput[60];
unsigned int i=0;
unsigned int lenOfGSMInput = 0;

bit OK;
bit Error;
bit Ring;

void showGSM_DATA(char GSM_DATA[]){ //to show the GSM OUTPUT after eliminating the chars '\r' and '\n'
    
    if (strcmp(GSM_DATA, "OK") == 0){ 
        OK = 1;
    }
    else if (strcmp(GSM_DATA, "ERROR") == 0){
            Error = 1;
    }
    else if (strcmp(GSM_DATA, "RING") == 0){
       
        Ring = 1;
        
        GSM_DATA[0] = '\x00';
        puts("AT+CLCC");
        putch(0x0D);
        
    }
    else{
        lcd_clear();
        lcd_goto(0);
        lcd_puts(GSM_DATA);
    }
  GSM_DATA[0] = '\x00';
 
} // end function showGSM_DATA

void main(int argc, char* argv[]){

	unsigned char input;
       
	INTCON=0;	// purpose of disabling the interrupts.
    
	lcd_init(); // initiate LCD
	
	init_comms();	// set up the USART - settings defined in usart.h
    			    
    puts("ATE0");
    putch(0X0D);
    
    puts("AT+CMGF=1");
    putch(0X0D);
    
    while(1){
		               
        input = getch();	// read a response from the GSM
        
        switch(input){
            
            case '\x0A': // if line feed detected in the GSM output i.e. '\n'
                        break;
            case '\x0D': // if carriage return detected i.e. '\r'
                        gsmInput[i] = '\x00';
                        
						lenOfGSMInput = strlen(gsmInput);
                        if (lenOfGSMInput > 0){
                            i = 0;
                            lenOfGSMInput = 0;
                            showGSM_DATA(gsmInput);
                        }
                        break;
            case '\x3E': // if greater-sign (in order to send SMS)
                        break;
            
        default: // if characters received
                gsmInput[i] = input;
                i++;
                break;
        }//end brace for switch
	} //end brace for while loop
}//end brace for main</htc.h></stdio.h>
Posted
Updated 15-Mar-18 19:41pm
v3
Comments
Toniyo Jackson 8-Apr-11 4:58am
   
Added pre tag for code.
pmk_1969 8-Apr-11 5:12am
   
thanks. I am inexperienced for forum. Once again thanks.
Toniyo Jackson 8-Apr-11 5:33am
   
Welcome
Rate this:
Please Sign up or sign in to vote.

Solution 1

The default code looks dangerous: are you sure i won't reach 60 thus overruning the buffer?
   
Comments
pmk_1969 8-Apr-11 8:36am
   
Hi Cpallini,

I have changed the code as follows to limit the chars. But still the same result.

default: // if characters received
if (i < 13){
gsmInput[i] = input;
i++;
}

break;
pmk_1969 11-Apr-11 11:40am
   
Hi Cpallini,

I solved the problem. Thanks to you for the help provided.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Check GSM_DATA[] because with the code as is, if there happens to be an extra blank space, strcmp() will not result in 0 (i.e. if there's a blank space at the beginning of the string).
   
Comments
pmk_1969 9-Apr-11 10:46am
   
I have checked GSM_DATA output. correct data received. It works fine if i did not do anything other than just display the result. But if I start validate the GSM_DATA then only onetime validation done and simply sits. I have to restart the device to process next command. I don't know why?

The following code works fine:

void showGSM_DATA(char GSM_DATA[]){ //to show the GSM OUTPUT after eliminating the chars '\r' and '\n'

if (strcmp(GSM_DATA, "OK") == 0){
OK = 1;

if (strcmp(NextCmd, "CMGF") == 0){
strcpy(NextCmd, "CLIP");
puts("AT+CMGF=1");
putch(0X0D);

}
if (strcmp(NextCmd, "CLIP") == 0){
*NextCmd = 0;
puts("AT+CLIP=1");
putch(0X0D);

}
return;
}

if (strcmp(GSM_DATA, "ERROR") == 0){
Error = 1;
*GSM_DATA = 0;
return;
}
if(strlen(GSM_DATA) > 6){

lcd_clear();
lcd_goto(0);
lcd_puts(GSM_DATA);
}
*GSM_DATA = 0;
return;

} // end function showGSM_DATA

But following code executes only one time and sits.

(Just only I added substring function):

void substring(size_t start, size_t stop, char src[]){

int j = 0;

if (start >= stop){

return;
}

for(i = start;i < stop; i++){
substringOfInput[j] = src[i];
j++;
}

substringOfInput[j] = '\x00';
lcd_clear();
lcd_goto(0);
lcd_puts(substringOfInput);
*src=0;
*substringOfInput=0;
j=0;
return;
}

if(strlen(GSM_DATA) > 6){
substring(0,5,GSM_DATA);
//lcd_clear();
//lcd_goto(0);
//lcd_puts(GSM_DATA);
}
pmk_1969 9-Apr-11 13:17pm
   
thanks Albert Holguin.

I solved the problem. As you told, I started to check GSM_DATA step by step. I located the problem. I declared variable i as global variable. I removed that and declare inside for loop.

That is, problem is here:

for(i = start;i < stop; i++){

Just I modified as follows:

for(int i = start;i < stop; i++){

Now, Everything works fine.

Thanks once again Mr.Albert Holguin
Albert Holguin 9-Apr-11 14:11pm
   
glad you found your problem... you have an awful lot of global variables, I'd recommend trying to cut that down, there's really no need for them, also... you posted this question as C not C++, declaring a variable within a loop as for(int i;...;...) is not a valid C statement, all variables must be declared at the beginning of a function
pmk_1969 11-Apr-11 11:39am
   
I can't able to differentiate C++ from C. Because I am new to C and C++. It works well on PIC Compiler(HI-TECH C PRO Compiler / MPLAB IDE v8.63). Thanks a lot once again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100