|
|
View previous topic :: View next topic |
Author |
Message |
garen
Joined: 30 Aug 2015 Posts: 17
|
can't handle string get from module sim900a |
Posted: Sun Aug 30, 2015 10:25 am |
|
|
Hello everyone, i am newbie
I am using module sim 900a to communicate with pic 16f887. I get a string from sim module well, but when sim module get a message, I can't send AT command printf("AT+CMGR=1\r\n") to read message like this Code: |
+CMGR: “REC READ”,
“+8613918186089”, ,
“02/01/30,20:40:31+00”
This is a test
|
and one problem is, how can i just get the content of a message, ex: i get
Code: | +CMGR: “REC READ”,
“+8613918186089”, ,
“02/01/30,20:40:31+00”
on led 1
|
it's so long and i just want to get a "on led 1"
This is my code and maybe this is a bad way to get and handle string so i need your help
Code: |
#include <16F887.h>
#device icd=true
#fuses HS,NOWDT,NOPROTECT,NOLVP,debug
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <lib_lcd.c>
#include <string.h>
unsigned int8 buffer[32],next_in=0,i=0,first_temp=0;
unsigned int1 flag_begin=0,flag_finish=0,flag_2=0,tt=0;
#int_rda
void rda_isr()
{
buffer[next_in]=getc();
if(buffer[next_in]==0x0a)
{
flag_begin=1;
next_in=0;
}
else if((buffer[next_in]==0x0d)&&(flag_begin==1))
{
buffer[next_in]='\0';flag_begin=0;flag_finish=1;
}
else next_in++;
}
void main()
{
set_tris_b(0xff);port_b_pullups(0xff);
set_tris_a(0x00);output_a(0x00);
enable_interrupts(global);
enable_interrupts(int_rda);
lcd_init();
while(true)
{
if(flag_finish==1)
{
lcd_gotoxy(1,1);printf(lcd_putc,"%s",buffer);printf(lcd_putc," ");
flag_finish=0;
}
if(!input(pin_b0))
{
delay_ms(30);
if(!input(pin_b0))
{
printf("AT+CMGR=1\r\n");
while(!input(pin_b0));
}
}
}
}
|
One thing, i was using AT command, AT+CMGD=1, by terminal when sending a message, so i will always get a notice +CMTI:”SM”,1 |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Aug 31, 2015 6:45 am |
|
|
Hi,
There is a complete GSM example in the Code Library that does exactly what you want. This example was submitted by Fourm user 'Gabriel'. It accepts SMS commands to turn Off/On four outputs. _________________ John
If it's worth doing, it's worth doing in real hardware! |
|
|
garen
Joined: 30 Aug 2015 Posts: 17
|
|
Posted: Mon Aug 31, 2015 9:32 am |
|
|
OK Thank ezflyr so much, i see it i will try it |
|
|
garen
Joined: 30 Aug 2015 Posts: 17
|
|
Posted: Tue Sep 01, 2015 11:40 pm |
|
|
Hi ezflyr
are you here? i saw code of user "Gabriel" but i don't understand this function char *strstr(s1,s2); i just know it is a function which find string s1 in s2 but i can't clearly understand it, what will be returned if s1="abcdefoklok\0" and s2='ok\0' .
because i see
Code: |
if(strstr(s1,s2)!='\0') return 1
else return 0
|
s1 is a buffer which i get from module sim by interrupts, and s2 is a string s2[10]={"OK\0"}; can you help me explain it?
and last one is where is memset() function? I don't see it in string.h but i can built |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Wed Sep 02, 2015 1:10 am |
|
|
We can't actually tell you what strstr would return. It depends 'where' the variables are....
strstr, returns a _pointer_. Effectively the 'address in RAM' where it finds the second string in the first. So the value actually returned will depend on where the variables actually 'are' in memory.
So if S1, was sitting at address 0x200, it'd return 0x206 (where the first 'ok' is in the string). However if s1 was at address 0x400, it'd return 0x406.
It returns a NULL pointer, if the string is not found. Since this is '0', this tests as '\0'.
memset, is a standard C memory function. Look in the CCS manual, or in a C book.
Slight waste of space in your declaration.
If you declare a string as:
char s2[10]={"OK"};
a terminating '\0' is automatically added. You don't need to add another. |
|
|
garen
Joined: 30 Aug 2015 Posts: 17
|
|
Posted: Thu Sep 03, 2015 9:05 am |
|
|
Thank Ttelmah. your comment is very clearly, but just one thing i don't understand, if strstr() function just returns a first address of s2 in s1 ,why don't they return int8 instead of a pointer. i mean, instead of use unsigned char *strstr(), they can use just a unsigned char strstr() (have not a pointer). why did they use a pointer |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Thu Sep 03, 2015 10:18 am |
|
|
Because first an int8 would limit you to just 255 locations. There is no reason a string can't be KB long on chips with more memory, and because all the C 'string' functions use pointers. When you call strstr, you hand it the pointer to s1, and the pointer to s2.
So if you are going to use any of these functions, they require the pointer.
If you just want the number of characters 'along' the string where s2 is, just subtract s1, from the value returned. |
|
|
garen
Joined: 30 Aug 2015 Posts: 17
|
|
Posted: Sat Sep 05, 2015 2:05 am |
|
|
ok thank Ttelmah so much for your help, i understand now. |
|
|
|
|
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
|