Discussion in "Embedded GSM Development" started by    DaveChika    Oct 25, 2010.
Mon Oct 25 2010, 02:17 am
#1
Hi, i want to extract sms message from AT+CMGR=1; AT command issued by 8051 to gsm Modem,
i get a response like this
+CMGR:"REC READ"," +2347060580383", "Date and time", sms text here
Ok.
i want to extract the sms text here using c/c++
here is my code but i simulated using keil with protues Vsm but still cant read anything on my SBUF.


CODE:

#include <reg51.h>
#include<string.h>
#include <stdio.h>


void init()
{
EA=0;
TL1=0XFD; //9600 @ 11.0592
TH1=0xFD;
TMOD=0x21;
SCON=0x50;
ET0=1;
TR1=1;
RI=0;
}

void SMSString(char*text) //function to send SMS using GSM modem
{
int count = 0;
while (text[count])
{
tx0(text[count++]);
}
}
void tx0(unsigned char x) //send data to serial port 0
{
while(TI==0){;}
TI=0;
SBUF=x;
}

unsigned char Rx_data( ) // returns data take note
{

while(!RI);//waits till a byte is recieved
RI = 0;
return(SBUF);

}

void main ()
{
P1=0x00;

init();

SMSString("AT\r"); // AT commands to initialize gsm modem

delay_sms(1000);
SMSString( "ATe0\r"); // turn off echo
delay_sms(1000);
SMSString( "AT&W\r"); // save settings
delay_sms(9000);
sSMSString( "AT+CMGF=1\r"); // select text mode for sms
delay_sms(90000);
SMSString( "AT+CNMI=2,1,0,0,0\r"); // notification of new sms
delay_sms(90000);
SMSString( "AT+CMGR=1\r"); // AT command to read sms

/* i get a response like this
// +CMGR:"REC READ"," +2347060580383", "Date and time", sms text here
// Ok. */


// read sms and store in buffer msg1

for(j=0;j<100;j++)
msg1[j] = Rx_data(); // stroring data in buffer for string compare.
delay_sms(9000);

i am using Protues with compim interfaced with gsm modem on the serial port.
i am able to send sms via protues but cant receive and extract sms.


[ Edited Fri Jun 24 2011, 06:33 am ]
Mon Oct 25 2010, 09:54 am
#2
hi DaveChika
use a virtual terminal in Proteus
run a Proteus and right click on virtual terminal and click on option echo character display.
in Proteus there is no mobile model so u should enter text in hyper terminal to suppose gsm Modem is responding.
use serial receive interrupt to receive data
check here
void tx0(unsigned char x) //send data to serial port 0
{
while(TI==0){;}
TI=0;
SBUF=x;
}

ur waiting for here for transmitting a byte even u don't place byte in sbuf the control will wait forever on this line
while(TI==0){;}
u should use interrupt in receiving instead of polling in polling method control will wait for byte to receive and can't do other job
try this
#include <reg51.h>

#include<string.h>


void tx0(unsigned char);
void delay_sms (unsigned int);
void SMSString(char*text) ;
void init(); 


unsigned char j,abc;
unsigned char msg1[100];


void serial () interrupt 4
{
msg1[abc]=SBUF;
abc++;
RI=0;
}

void main ()
{

/*
if u do not want to check "OK" response after each AT command then 
enable interrupt IE=0X90 ; after last AT command of  
SMSString( "AT+CMGR=1\r"); // AT command to read sms
so after that u recieve a no of bytes from modile if u disable it then u do not reieve 
it even data comes 
*/

init();
SMSString("AT\r"); // AT commands to initialize gsm modem
delay_sms(1000);
SMSString( "ATe0\r"); // turn off echo
delay_sms(1000);
SMSString( "AT&W\r"); // save settings
delay_sms(1000);
SMSString( "AT+CMGF=1\r"); // select text mode for sms
delay_sms(1000);
SMSString( "AT+CNMI=2,1,0,0,0\r"); // notification of new sms
delay_sms(1000);
SMSString( "AT+CMGR=1\r"); // AT command to read sms

/* i get a response like this
// +CMGR:"REC READ"," +95445677888", "Date and time", sms text here
// Ok. */

// read sms and store in buffer msg1

delay_sms(1000);
while(1);
}


void init()
{
j=0;
abc=0;
IE=0X90;   // Enable serial interrupt
TL1=0XFD; //9600 @ 11.0592
TH1=0xFD;
TMOD=0x20;
SCON=0x50;
TR1=1;
}

void SMSString(unsigned char* text) //function to send SMS using GSM modem
{
unsigned char count = 0;
while (text[count])
{
tx0(text[count++]);
}
}

void tx0(unsigned char x) //send data to serial port 0
{
EA=0;
SBUF=x;
while(TI==0);
TI=0;
EA=1;
}


void delay_sms (unsigned int count)
{
unsigned int i;		       		// Keil v7.5a 
    while(count) {
        i = 115; 
		while(i>
0) i--;
        count--;
}
}

 DaveChika like this.
Tue Oct 26 2010, 02:51 pm
#3
@ Majoka,thanks for your reply,i can now receive strings from my modem, but i only want the msg1 to contain only the response of AT+CMGR=1(AT command to read Sms),
i did as you said by enabling IE after my my AT command but i didnt get anything. below is what i used
CODE:

init();
SMSString("AT\r"); // AT commands to initialize gsm modem
delay_sms(1000);
SMSString( "ATe0\r"); // turn off echo
delay_sms(1000);
//Clearmsg1();
SMSString( "AT&W\r"); // save settings
delay_sms(1000);
SMSString( "AT+CMGF=1\r"); // select text mode for sms
delay_sms(1000);
SMSString( "AT+CNMI=2,1,0,0,0\r"); // notification of new sms
delay_sms(1000);

SMSString( "AT+CMGR=901\r"); // AT command to read sms
IE=0X90; // Enable serial interrupt
delay_sms(1000);

thanks for your response /reply
Tue Oct 26 2010, 03:16 pm
#4
hi DaveChika
in the above coding message is in the msg1[100] array but in the above code i do not display it becoz UART is used by modem now i can only send it using software uart to pc or on lcd to display what data is received in the msg1[100]
ru understand dear


[ Edited Tue Oct 26 2010, 03:18 pm ]
Tue Oct 26 2010, 03:21 pm
#5
@majoka,pls i cant get you,
Tue Oct 26 2010, 03:34 pm
#6
see above program carefully i do not send msg1 array to pc to see that what message is readed from modem now how will u see that u cannot send it to pc becoz UART portion is used by modem
ru got the point
Tue Oct 26 2010, 04:05 pm
#7
before this line
SMSString( "AT+CMGR=1\r"); // AT command to read sms
msg1[100] array is empty
aftre this line
msg1[100] array has some data
now i do not send it any where i was leave it on u that what u want
now if u want to see that what message is in array u have to send it to pc to see but pc need uart that is used by modem
now 2nd option is that u use software uart to send data to pc
what is software uart u can see that in uart tutorial on this site
3rd option is that u interface lcd with controller when controller read the msg it display message on lcd display
4th option is that u introduce a push button on any pin and 1 led power up controller led is off when it send last command on the led to verify this line is executed now code wait a push button to push now u disconnect the modem now uart is free attach it to pc serial port open terminal program and press push button now in coding send msg1 array to uart now it will display in pc software
this is slow processes and waist ur time too but has a option too
in this manner u need 1 uart that is hardware built in 8051
hope so now u got it
Wed Oct 27 2010, 04:21 pm
#8
Hi,Majoka, can you take a look at the code below for extracting senders gsm number from sms,

#include <reg51.h>

#include<string.h>


void tx0(unsigned char);
void delay_sms (unsigned int);
void SMSString(char*text) ;
void init();
void Clearmsg1(void);
read_text( unsigned char msg);



unsigned char j,abc;
unsigned char msg1[100],sms_text;


void serial () interrupt 4
{
 // while (msg1!='+');
msg1[abc]=SBUF;

abc++;
RI=0;
}

void main ()
{

init();
SMSString("AT\r"); // AT commands to initialize gsm modem
delay_sms(1000);

SMSString( "ATe0\r"); // turn off echo
delay_sms(1000);
//Clearmsg1();
SMSString( "AT&W\r"); // save settings
delay_sms(1000);

SMSString( "AT+CMGF=1\r"); // select text mode for sms
delay_sms(1000);
SMSString( "AT+CNMI=2,1,0,0,0\r"); // notification of new sms
delay_sms(1000);
// IE=0X90;   // Enable serial interrupt
SMSString( "AT+CMGR=902\r"); // AT command to read sms
 // IE=0X90;   // Enable serial interrupt
delay_sms(1000);

/* i get a response like this
// +CMGR:"REC READ","  +2347060580383  "Date and time", sms text here
// Ok. */

// read sms and store in buffer msg1

delay_sms(1000);

   read_text(msg1);

while(1);
}


void init()
{
j=0;
abc=0;
 IE=0X90;   // Enable serial interrupt
TL1=0XFD; //9600 @ 11.0592
TH1=0xFD;
TMOD=0x20;
SCON=0x50;
TR1=1;
}

void SMSString(unsigned char* text) //function to send SMS using GSM modem
{
unsigned char count = 0;
while (text[count])
{
tx0(text[count++]);
}
}

 /* void Clearmsg1(void)				//Function for Clr the Array
{
	unsigned char Ch=0;
	while(Ch<100)
		msg1[Ch++]=0; 			//Initialisation of Array
		
} */
void tx0(unsigned char x) //send data to serial port 0
{
EA=0;
SBUF=x;
while(TI==0);
TI=0;
EA=1;
}


void delay_sms (unsigned int count)
{
unsigned int i;                         // Keil v7.5a 
    while(count) {
        i = 115;
                while(i>
0) i--;
        count--;
}
 }

 read_text( unsigned char msg)

{
  unsigned char text_msg,date_time,phone_number;

  unsigned char *response ,*date,*number,*msg1;
  
  //   *msg1 = *response  ;
// unsigned char *number ;// = number;

 
while(1)             

{
  
    while (*msg1 !='"')
	    msg1++;

  // after this loop,you have reached the starting of number.
  
       msg1++;	  // bypass the space
         msg1++;  // bypass the quote

		 while (*msg1!=',')
		 {
		 *number = *msg1;
			  number++;
		  msg1++;
	   phone_number = number;
}}


/*
	   response ++;  // bypass ','
	   response++; // bypass quote

	   while (*response!='"')
	   {
	   *response = *date ;
	   date++;
	   response++;
	    *date = date_time ;
     response++;
	 response++;
	 while (*response!='O')
	 {
	 response++;
	  *response = text_msg  ;
	 text_msg++;
	 response++;

	// sendCommand( text_msg );
//	 return *text_msg;
}
}
}  */

}


athached is mt debugging section,but i cant still extract the senders gsm number,data and sms text seperately.
Wed Oct 27 2010, 10:39 pm
#9
hi DaveChika
let say u extract the number date, time everything correctly now how u will see what is the number , time and date etc because it is in the array how u will see that
ru want to send it to hyper terminal to see let clear my this point then according to that i suggest u
can u provide me the exact output when u send command to modem for reading message then it reply with many parameters how they are arrange i want to see that
Wed Oct 27 2010, 11:37 pm
#10
@majoka,
Thanks for you prompt response to all my Queries.
here is the response i get after my At command to read sms from my modem

// this is the sequence of the At commands simulated from my protues vd51 simulator //with keil.

AT
ATe0
AT&W
AT+CMGF=1
AT+CNMI=2,1,0,0,0
AT+CMGR=902

// And this the response from my modem,am using the modem memory index of 902 to turn device off

//
OK

OK

OK

OK

OK

+CMGR: "REC READ" ,"+2347060580383","10/10/23,14:29:33+04"
device1 off

OK

******************************************************************
the second if my second output to extract longer sms text message

AT
ATe0
AT&W
AT+CMGF=1
AT+CNMI=2,1,0,0,0
AT+CMGR=905
AT
ATe0
AT&W
AT+CMGF=1
AT+CNMI=2,1,0,0,0
AT+CMGR=905

****************************************************************
Response from modem here


OK

OK

OK

OK

OK

+CMGR: "REC UNREAD" ,"+2347060580383","10/10/27,18:54:32+04"
Wellcome To University of Nigeria Nsukka,Department of Electronic Engineering.

OK

these are the two examples of the At commands response you requested.
the first one i want to extract the senders number,time, date separately and the sms text (device1 off),
the second one is to extract the senders number,time,date, and that long string sms text(Wellcomo to University of Nigeria Nsukka,Department of Electronic Engineering).
thanks for your anticipated response,
Majoka

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Clydehet
Wed May 01 2024, 06:44 pm
Davidoried
Wed May 01 2024, 06:11 pm
KevinTab
Sun Apr 28 2024, 05:35 am
Tumergix
Sun Apr 28 2024, 12:59 am
StevenDrulk
Sat Apr 27 2024, 08:47 pm
StephenHauct
Sat Apr 27 2024, 09:38 am
Adamsaf
Sat Apr 27 2024, 07:12 am
Robertphype
Sat Apr 27 2024, 12:23 am