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

Decode serial messages

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







Decode serial messages
PostPosted: Mon Jul 28, 2003 3:52 am     Reply with quote

Hi,
I'm searching a suggest to implement a decode serial ascii message on my firmware.
I have a modem connect on pic 16f877 uart and i have to decode the answer messages.
Example:
I sent a command to the modem with a printf string 'AT' and i must decode the answer 'OK'.
This message always end with 'return ascii char'

How can i do?
Thank you
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516420
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Decode serial messages
PostPosted: Mon Jul 28, 2003 5:55 am     Reply with quote

Read the data into a buffer until you recieve the 'return char'. Then process the buffer using a string compare function:

char string1[]="OK";

if (stricmp(buffer, string1) == 0)
{
//The strings are equal so do whatever
}

or

char string1[3]; // This must be big enough including the NULL terminator

strcpy(string1, "OK");
if (stricmp(buffer, string1) == 0)
{
//The strings are equal so do whatever
}

:=Hi,
:=I'm searching a suggest to implement a decode serial ascii message on my firmware.
:=I have a modem connect on pic 16f877 uart and i have to decode the answer messages.
:=Example:
:=I sent a command to the modem with a printf string 'AT' and i must decode the answer 'OK'.
:=This message always end with 'return ascii char'
:=
:=How can i do?
:=Thank you
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516427
Steve H.
Guest







What I do....
PostPosted: Mon Jul 28, 2003 7:54 am     Reply with quote

I have similar code posted on my website at the link below.

Steve H.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516432
jpt@tayloredge.com
Guest







Re: Decode serial messages
PostPosted: Mon Jul 28, 2003 8:24 pm     Reply with quote

<a href="http://www.tayloredge.com/utilities/scmd.c.txt" TARGET="_blank">http://www.tayloredge.com/utilities/scmd.c.txt</a>

from

<a href="http://www.tayloredge.com/utilities/index.html" TARGET="_blank">http://www.tayloredge.com/utilities/index.html</a>

This is my command processor, all interrupt level message decode.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516450
mmanisse
Guest







Re: Decode serial messages
PostPosted: Sat Aug 02, 2003 3:16 am     Reply with quote

ok Mark
i untherstood what do you mean, but i have a little difficoult to do this.
My strategy to decode message is this:
my modem message always is:
'CR' 'LF' 'MESSAGE' 'CR' 'LF'.
With this information i'm working in this mode:
I'm looking for the CR and LF than i buffered the chars to next CR.
Now i have a buffer, how can i compare it?

Tnx

I'm attach the function:

=============================================================
gsm_rx_string()
//Rx string from gsm: 'CR' 'LF' 'XXXXXXXXXX' 'CR' 'LF'
{
MESSAGE_READ=0;
buffer_gsm=0;

buffer_gsm[0] = getc();
if (buffer_gsm[0] != 'CR') // Find Carriage Return.
goto exit;

buffer_gsm[0] = getc();
if (buffer_gsm[0] != 'LF') // Find Line Feed.
goto exit;


i=0; // Buffered the string to next CR
do {
buffer_gsm[i] = getc();
++i;
} while ((buffer_gsm[i-1] != 'CR') || (i<35));


if ((buffer_gsm[i-1] == 'CR') && (i>0))
{
MESSAGE_READ=1;
i=i-1; // Delete CR from string.
}

exit:

}

=============================================================





:=Read the data into a buffer until you recieve the 'return char'. Then process the buffer using a string compare function:
:=
:=char string1[]="OK";
:=
:=if (stricmp(buffer, string1) == 0)
:={
:= //The strings are equal so do whatever
:=}
:=
:=or
:=
:=char string1[3]; // This must be big enough including the NULL terminator
:=
:=strcpy(string1, "OK");
:=if (stricmp(buffer, string1) == 0)
:={
:= //The strings are equal so do whatever
:=}
:=
:=:=Hi,
:=:=I'm searching a suggest to implement a decode serial ascii message on my firmware.
:=:=I have a modem connect on pic 16f877 uart and i have to decode the answer messages.
:=:=Example:
:=:=I sent a command to the modem with a printf string 'AT' and i must decode the answer 'OK'.
:=:=This message always end with 'return ascii char'
:=:=
:=:=How can i do?
:=:=Thank you
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516588
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Decode serial messages
PostPosted: Sat Aug 02, 2003 9:36 am     Reply with quote

Try this out:

enum message
{
MSG_OK,
MSG_TEST,
MSG_3,
MSG_4,
MSG_5,
MSG_6,
INVALID_MSG
}
gsm_rx_string()

//Rx string from gsm: 'CR' 'LF' 'XXXXXXXXXX' 'CR' 'LF'
{
MESSAGE_READ=0;
buffer_gsm[0]=0;

buffer_gsm[0] = getc();
if (buffer_gsm[0] != '\r') // Find Carriage Return.
return;

buffer_gsm[0] = getc();
if (buffer_gsm[0] != '\n') // Find Line Feed.
return;
i=0;
while(i<35)
{
buffer_gsm[i] = getc();
if (buffer_gsm[i] == '\r')
{
// terminate our string
buffer_gsm[i] = 0
MESSAGE_READ=1;
break;
}
i++;
}
return;
}

enum message Check_GSM_String(char *s1)
{
enum message msg;
char s2[10]; // This must be big enough including the NULL terminator

for (msg=0;msg {
switch (msg)
{
case MSG_OK:
strcpy(s2, "OK");
break;
case MSG_TEST:
strcpy(s2, "TEST");
break;
case MSG_MSG3:
strcpy(s2, "MSG3");
break;
case MSG_MSG4:
strcpy(s2, "MSG4");
break;
case MSG_MSG5:
strcpy(s2, "MSG5");
break;
case MSG_MSG6:
strcpy(s2, "MSG6");
break;
default:
// we shouldn't ever get here! But if we do return invalid message
return(INVALID_MSG);
}
if (stricmp(s1, s2) == 0)
{
//The strings are equal so return which one
return(msg);
}
}
// We didn't find a valid message so return an invalid one
return(INVALID_MSG);
}

:=ok Mark
:=i untherstood what do you mean, but i have a little difficoult to do this.
:=My strategy to decode message is this:
:=my modem message always is:
:='CR' 'LF' 'MESSAGE' 'CR' 'LF'.
:=With this information i'm working in this mode:
:=I'm looking for the CR and LF than i buffered the chars to next CR.
:=Now i have a buffer, how can i compare it?
:=
:=Tnx
:=
:=I'm attach the function:
:=
:==============================================================
:=gsm_rx_string()
:=//Rx string from gsm: 'CR' 'LF' 'XXXXXXXXXX' 'CR' 'LF'
:={
:= MESSAGE_READ=0;
:= buffer_gsm=0;
:=
:= buffer_gsm[0] = getc();
:= if (buffer_gsm[0] != 'CR') // Find Carriage Return.
:= goto exit;
:=
:= buffer_gsm[0] = getc();
:= if (buffer_gsm[0] != 'LF') // Find Line Feed.
:= goto exit;
:=
:=
:= i=0; // Buffered the string to next CR
:= do {
:= buffer_gsm[i] = getc();
:= ++i;
:= } while ((buffer_gsm[i-1] != 'CR') || (i<35));
:=
:=
:= if ((buffer_gsm[i-1] == 'CR') && (i>0))
:= {
:= MESSAGE_READ=1;
:= i=i-1; // Delete CR from string.
:= }
:=
:=exit:
:=
:=}
:=
:==============================================================
:=
:=
:=
:=
:=
:=:=Read the data into a buffer until you recieve the 'return char'. Then process the buffer using a string compare function:
:=:=
:=:=char string1[]="OK";
:=:=
:=:=if (stricmp(buffer, string1) == 0)
:=:={
:=:= //The strings are equal so do whatever
:=:=}
:=:=
:=:=or
:=:=
:=:=char string1[3]; // This must be big enough including the NULL terminator
:=:=
:=:=strcpy(string1, "OK");
:=:=if (stricmp(buffer, string1) == 0)
:=:={
:=:= //The strings are equal so do whatever
:=:=}
:=:=
:=:=:=Hi,
:=:=:=I'm searching a suggest to implement a decode serial ascii message on my firmware.
:=:=:=I have a modem connect on pic 16f877 uart and i have to decode the answer messages.
:=:=:=Example:
:=:=:=I sent a command to the modem with a printf string 'AT' and i must decode the answer 'OK'.
:=:=:=This message always end with 'return ascii char'
:=:=:=
:=:=:=How can i do?
:=:=:=Thank you
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516592
mmanisse
Guest







Re: Decode serial messages
PostPosted: Sun Aug 03, 2003 6:29 am     Reply with quote

Ok Mark,
I have introducted you suggest in my firmware, but the compiler tell me that there is an error on :
'enum message msg;'
It espect a {
This is the first time that i use enum, do you think that there is an error?

I have counted the {} but all opened are closed.
Can you help me?

:=Try this out:
:=
:=enum message
:={
:= MSG_OK,
:= MSG_TEST,
:= MSG_3,
:= MSG_4,
:= MSG_5,
:= MSG_6,
:= INVALID_MSG
:=}
:=gsm_rx_string()
:=
:=//Rx string from gsm: 'CR' 'LF' 'XXXXXXXXXX' 'CR' 'LF'
:={
:= MESSAGE_READ=0;
:= buffer_gsm[0]=0;
:=
:= buffer_gsm[0] = getc();
:= if (buffer_gsm[0] != '\r') // Find Carriage Return.
:= return;
:=
:= buffer_gsm[0] = getc();
:= if (buffer_gsm[0] != '\n') // Find Line Feed.
:= return;
:= i=0;
:= while(i<35)
:= {
:= buffer_gsm[i] = getc();
:= if (buffer_gsm[i] == '\r')
:= {
:= // terminate our string
:= buffer_gsm[i] = 0
:= MESSAGE_READ=1;
:= break;
:= }
:= i++;
:= }
:= return;
:=}
:=
:=enum message Check_GSM_String(char *s1)
:={
:= enum message msg;
:= char s2[10]; // This must be big enough including the NULL terminator
:=
:= for (msg=0;msg := {
:= switch (msg)
:= {
:= case MSG_OK:
:= strcpy(s2, "OK");
:= break;
:= case MSG_TEST:
:= strcpy(s2, "TEST");
:= break;
:= case MSG_MSG3:
:= strcpy(s2, "MSG3");
:= break;
:= case MSG_MSG4:
:= strcpy(s2, "MSG4");
:= break;
:= case MSG_MSG5:
:= strcpy(s2, "MSG5");
:= break;
:= case MSG_MSG6:
:= strcpy(s2, "MSG6");
:= break;
:= default:
:= // we shouldn't ever get here! But if we do return invalid message
:= return(INVALID_MSG);
:= }
:= if (stricmp(s1, s2) == 0)
:= {
:= //The strings are equal so return which one
:= return(msg);
:= }
:= }
:= // We didn't find a valid message so return an invalid one
:= return(INVALID_MSG);
:=}
:=
:=:=ok Mark
:=:=i untherstood what do you mean, but i have a little difficoult to do this.
:=:=My strategy to decode message is this:
:=:=my modem message always is:
:=:='CR' 'LF' 'MESSAGE' 'CR' 'LF'.
:=:=With this information i'm working in this mode:
:=:=I'm looking for the CR and LF than i buffered the chars to next CR.
:=:=Now i have a buffer, how can i compare it?
:=:=
:=:=Tnx
:=:=
:=:=I'm attach the function:
:=:=
:=:==============================================================
:=:=gsm_rx_string()
:=:=//Rx string from gsm: 'CR' 'LF' 'XXXXXXXXXX' 'CR' 'LF'
:=:={
:=:= MESSAGE_READ=0;
:=:= buffer_gsm=0;
:=:=
:=:= buffer_gsm[0] = getc();
:=:= if (buffer_gsm[0] != 'CR') // Find Carriage Return.
:=:= goto exit;
:=:=
:=:= buffer_gsm[0] = getc();
:=:= if (buffer_gsm[0] != 'LF') // Find Line Feed.
:=:= goto exit;
:=:=
:=:=
:=:= i=0; // Buffered the string to next CR
:=:= do {
:=:= buffer_gsm[i] = getc();
:=:= ++i;
:=:= } while ((buffer_gsm[i-1] != 'CR') || (i<35));
:=:=
:=:=
:=:= if ((buffer_gsm[i-1] == 'CR') && (i>0))
:=:= {
:=:= MESSAGE_READ=1;
:=:= i=i-1; // Delete CR from string.
:=:= }
:=:=
:=:=exit:
:=:=
:=:=}
:=:=
:=:==============================================================
:=:=
:=:=
:=:=
:=:=
:=:=
:=:=:=Read the data into a buffer until you recieve the 'return char'. Then process the buffer using a string compare function:
:=:=:=
:=:=:=char string1[]="OK";
:=:=:=
:=:=:=if (stricmp(buffer, string1) == 0)
:=:=:={
:=:=:= //The strings are equal so do whatever
:=:=:=}
:=:=:=
:=:=:=or
:=:=:=
:=:=:=char string1[3]; // This must be big enough including the NULL terminator
:=:=:=
:=:=:=strcpy(string1, "OK");
:=:=:=if (stricmp(buffer, string1) == 0)
:=:=:={
:=:=:= //The strings are equal so do whatever
:=:=:=}
:=:=:=
:=:=:=:=Hi,
:=:=:=:=I'm searching a suggest to implement a decode serial ascii message on my firmware.
:=:=:=:=I have a modem connect on pic 16f877 uart and i have to decode the answer messages.
:=:=:=:=Example:
:=:=:=:=I sent a command to the modem with a printf string 'AT' and i must decode the answer 'OK'.
:=:=:=:=This message always end with 'return ascii char'
:=:=:=:=
:=:=:=:=How can i do?
:=:=:=:=Thank you
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516620
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Decode serial messages
PostPosted: Sun Aug 03, 2003 8:20 am     Reply with quote

Typo, you need a ; after the }
enum message
{
MSG_OK,
MSG_TEST,
MSG_3,
MSG_4,
MSG_5,
MSG_6,
INVALID_MSG
};

:=Ok Mark,
:=I have introducted you suggest in my firmware, but the compiler tell me that there is an error on :
:='enum message msg;'
:=It espect a {
:=This is the first time that i use enum, do you think that there is an error?
:=
:=I have counted the {} but all opened are closed.
:=Can you help me?
:=
:=:=Try this out:
:=:=
:=:=enum message
:=:={
:=:= MSG_OK,
:=:= MSG_TEST,
:=:= MSG_3,
:=:= MSG_4,
:=:= MSG_5,
:=:= MSG_6,
:=:= INVALID_MSG
:=:=}
:=:=gsm_rx_string()
:=:=
:=:=//Rx string from gsm: 'CR' 'LF' 'XXXXXXXXXX' 'CR' 'LF'
:=:={
:=:= MESSAGE_READ=0;
:=:= buffer_gsm[0]=0;
:=:=
:=:= buffer_gsm[0] = getc();
:=:= if (buffer_gsm[0] != '\r') // Find Carriage Return.
:=:= return;
:=:=
:=:= buffer_gsm[0] = getc();
:=:= if (buffer_gsm[0] != '\n') // Find Line Feed.
:=:= return;
:=:= i=0;
:=:= while(i<35)
:=:= {
:=:= buffer_gsm[i] = getc();
:=:= if (buffer_gsm[i] == '\r')
:=:= {
:=:= // terminate our string
:=:= buffer_gsm[i] = 0
:=:= MESSAGE_READ=1;
:=:= break;
:=:= }
:=:= i++;
:=:= }
:=:= return;
:=:=}
:=:=
:=:=enum message Check_GSM_String(char *s1)
:=:={
:=:= enum message msg;
:=:= char s2[10]; // This must be big enough including the NULL terminator
:=:=
:=:= for (msg=0;msg :=:= {
:=:= switch (msg)
:=:= {
:=:= case MSG_OK:
:=:= strcpy(s2, "OK");
:=:= break;
:=:= case MSG_TEST:
:=:= strcpy(s2, "TEST");
:=:= break;
:=:= case MSG_MSG3:
:=:= strcpy(s2, "MSG3");
:=:= break;
:=:= case MSG_MSG4:
:=:= strcpy(s2, "MSG4");
:=:= break;
:=:= case MSG_MSG5:
:=:= strcpy(s2, "MSG5");
:=:= break;
:=:= case MSG_MSG6:
:=:= strcpy(s2, "MSG6");
:=:= break;
:=:= default:
:=:= // we shouldn't ever get here! But if we do return invalid message
:=:= return(INVALID_MSG);
:=:= }
:=:= if (stricmp(s1, s2) == 0)
:=:= {
:=:= //The strings are equal so return which one
:=:= return(msg);
:=:= }
:=:= }
:=:= // We didn't find a valid message so return an invalid one
:=:= return(INVALID_MSG);
:=:=}
:=:=
:=:=:=ok Mark
:=:=:=i untherstood what do you mean, but i have a little difficoult to do this.
:=:=:=My strategy to decode message is this:
:=:=:=my modem message always is:
:=:=:='CR' 'LF' 'MESSAGE' 'CR' 'LF'.
:=:=:=With this information i'm working in this mode:
:=:=:=I'm looking for the CR and LF than i buffered the chars to next CR.
:=:=:=Now i have a buffer, how can i compare it?
:=:=:=
:=:=:=Tnx
:=:=:=
:=:=:=I'm attach the function:
:=:=:=
:=:=:==============================================================
:=:=:=gsm_rx_string()
:=:=:=//Rx string from gsm: 'CR' 'LF' 'XXXXXXXXXX' 'CR' 'LF'
:=:=:={
:=:=:= MESSAGE_READ=0;
:=:=:= buffer_gsm=0;
:=:=:=
:=:=:= buffer_gsm[0] = getc();
:=:=:= if (buffer_gsm[0] != 'CR') // Find Carriage Return.
:=:=:= goto exit;
:=:=:=
:=:=:= buffer_gsm[0] = getc();
:=:=:= if (buffer_gsm[0] != 'LF') // Find Line Feed.
:=:=:= goto exit;
:=:=:=
:=:=:=
:=:=:= i=0; // Buffered the string to next CR
:=:=:= do {
:=:=:= buffer_gsm[i] = getc();
:=:=:= ++i;
:=:=:= } while ((buffer_gsm[i-1] != 'CR') || (i<35));
:=:=:=
:=:=:=
:=:=:= if ((buffer_gsm[i-1] == 'CR') && (i>0))
:=:=:= {
:=:=:= MESSAGE_READ=1;
:=:=:= i=i-1; // Delete CR from string.
:=:=:= }
:=:=:=
:=:=:=exit:
:=:=:=
:=:=:=}
:=:=:=
:=:=:==============================================================
:=:=:=
:=:=:=
:=:=:=
:=:=:=
:=:=:=
:=:=:=:=Read the data into a buffer until you recieve the 'return char'. Then process the buffer using a string compare function:
:=:=:=:=
:=:=:=:=char string1[]="OK";
:=:=:=:=
:=:=:=:=if (stricmp(buffer, string1) == 0)
:=:=:=:={
:=:=:=:= //The strings are equal so do whatever
:=:=:=:=}
:=:=:=:=
:=:=:=:=or
:=:=:=:=
:=:=:=:=char string1[3]; // This must be big enough including the NULL terminator
:=:=:=:=
:=:=:=:=strcpy(string1, "OK");
:=:=:=:=if (stricmp(buffer, string1) == 0)
:=:=:=:={
:=:=:=:= //The strings are equal so do whatever
:=:=:=:=}
:=:=:=:=
:=:=:=:=:=Hi,
:=:=:=:=:=I'm searching a suggest to implement a decode serial ascii message on my firmware.
:=:=:=:=:=I have a modem connect on pic 16f877 uart and i have to decode the answer messages.
:=:=:=:=:=Example:
:=:=:=:=:=I sent a command to the modem with a printf string 'AT' and i must decode the answer 'OK'.
:=:=:=:=:=This message always end with 'return ascii char'
:=:=:=:=:=
:=:=:=:=:=How can i do?
:=:=:=:=:=Thank you
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516623
mmanisse
Guest







Re: Decode serial messages
PostPosted: Mon Aug 04, 2003 1:51 pm     Reply with quote

There is another compiler error

enum message Check_GSM_String(char *s1)
{
enum message msg; //Compiler error espect {????

char s2[10];
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516661
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Decode serial messages
PostPosted: Mon Aug 04, 2003 5:33 pm     Reply with quote

Look at the for statement. This board doesn't like the less than sign it should read

for(msg=0;msg "less than" INVALID_MSG;msg++)


Also note that there are some typo's in the switch statement. For example, MSG_MSG3 should have been MSG_3 but it doesn't really matter since you are writing your own enums. There was another ';' missing in there also.

Regards
Mark



:=There is another compiler error
:=
:=enum message Check_GSM_String(char *s1)
:={
:= enum message msg; //Compiler error espect {????
:=
:= char s2[10];
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516663
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