|
|
View previous topic :: View next topic |
Author |
Message |
dave10
Joined: 09 Apr 2013 Posts: 14 Location: UK
|
extracting data from ascii string return by gsm modem |
Posted: Fri Apr 26, 2013 9:04 am |
|
|
Hi there i been trying to figure out for a couple of days how to remove the data from an ascii string of data returned by a modem via a AT commands. I basically want to know if the modem has received a message so i can then act upon the message. Or later do a response based on what text has been sent
1, First I sent a request to the modem asking if there has been any messages delivered with AT commands as below. using the serial file exsr.c. to return the response from the modem (which work very well )
response displayed on hyperterminal
'fprintf(modem, "AT+CPMS=\"SM\"")'
2, The modem returns with the data I require which is placed in value 'data' from the buffer. Which I have sent to hyperterminal to see what has been delivered.
The terminal shows.
+CMPS: 0,25,0,25. (HYPER TERMINAL) which is correct
3, When a text message is sent the value changes to 1 etc. I want to act off the data when it changes from zero. How do I extract the data (ascii element) to just show the '0' so I can do the logic on that zero, or a specific text message that has been sent later.
+CMPS: 1,25,0,25. (message sent) only want '1'
I've had a look on internet and 'parsing the data' is the way to go I think ??. But my whole message is stored in value 'data'. How do I extract just the part I require. My head is spinning trying to figure it out.
I tried removing individual elements from the buffer array what gets the message before placing in value data.
But all you receive is random parts of the ascii string not in any particular order ??
Basically how do you remove elements from an ascii string ??
Please see attached code
interrupt retrieving the data as exsr (thank you)
Code: |
#Int_rda
void serial_isr() {
int t;
buffer[next_in]=getc(topc);
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);
} |
Ascii being sent to modem and cget grabbing response
Code: | char check_for_messages()
{
fprintf(topc, "AT+CPMS=\"SM\""); // check for text messages ??
fprintf(topc,"\r\n");
char data= bgetc(); //sit and wait for data
delay_ms(100);
fprintf(modem,data "\r\n");
delay_ms(1000) |
Any help greatly received. Thanks in advance |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Fri Apr 26, 2013 11:37 am |
|
|
Hi Dave,
Can I make a suggestion that might improve what you are trying to do?
Most GSM modems support an AT command that enables 'Unsolicited Message Notification'. This command, CNMI, causes the GSM modem to notify the host whenever a SMS message is received. So, instead of continuously 'polling' the GSM modem to see if a SMS message has been received, you simply wait until the notification is sent by the GSM modem.
This code turns the feature On with the GSM modem I'm using, the ADH8066.
Code: |
fprintf(ADH8066, "AT+CNMI=1,1\r");
|
When an SMS message is received, the GSM modem will send:
Code: |
<CR><LF>+CMTI: "SM",1<CR><LF>
|
I simply wait for characters to be received into my receive buffer, and then when I see 'CTMI' has been sent, I know I have an SMS message waiting. At that point, I read the message like this:
Code: |
fprintf(ADH8066, "AT+CMGR=1\r");
|
To your specific question, there are many ways to do this, but the simplest would be to simply start filling a character array whenever the '+' is detected, and then stop when a <CR> or <LF> was detected. Now the entire modem response is inside your character array. You could then simply look at the 8th character to see if it's a '0' or a '1'.
John |
|
|
dave10
Joined: 09 Apr 2013 Posts: 14 Location: UK
|
|
Posted: Fri Apr 26, 2013 1:03 pm |
|
|
Hi exflyr
Thankyou for replying to my message I really apprecate the support
I did try the command, 'AT' 'CNMI=1' and the modem I'm using responded with an an error code. So I looked up the error code which said it was unsupported. So I'm kinda stuck doing it this way.
But when I hopefully do start recieving meaningful messages I will need to shorten these responses down anyway to respond to the incoming text message.
The message is recieved and stored in 'char data'.
So does this mean there is a string i.e "hello world" stored in 'data ?
Do I now need to convert the string into a character array to remove the element I want to use for logic ?
(which is the bit I'm stuckon!!) ?? |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Fri Apr 26, 2013 1:38 pm |
|
|
Hi Dave,
What GSM modem are you using? It's possible that you've got the syntax wrong rather than the CNMI command is not actually supported? I've worked with a number of GSM modems, and they've all supported that functionality!
Here is some code that sends 'AT' to the GSM modem, and puts the return data in an array. You should be able to get the idea, and modify
it to suit your needs.
Code: |
// Here we reset the circular buffer index to avoid reading old messages!
Next_In = Next_Out;
fprintf(ADH8066, "\rAT\r"); //Note: leading CR format is required!
do
{
if(bkbhit)
{
TempChar = bgetc();
if (TempChar == '\r')
iReturnIndex++;
if ((iReturnIndex == 1) && (TempChar != '\r') && (TempChar != '\n'))
{
//fprintf(Diag, "Msg1 Char = %c\n\r", TempChar);
Msg1[ArrayIndex1] = TempChar;
ArrayIndex1++;
if (ArrayIndex1 > 70)
return; //Msg1 buffer has overflowed, so exit the subroutine!
}
}
} while (iReturnIndex < 2);
// Here we null terminate the Msg line from the ADH8066 module for printing!
Msg1[ArrayIndex1++] = '\0';
fprintf(Diag, "Message 1: %s\n\r", Msg1);
|
John |
|
|
dave10
Joined: 09 Apr 2013 Posts: 14 Location: UK
|
|
Posted: Fri Apr 26, 2013 2:28 pm |
|
|
Hi John
Thanks for the sample code. I'll let you know how I get on. I'll retry the AT commands as well to ensure I haven't made a mistake.
Thanks for your time.
Dave |
|
|
|
|
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
|