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 support@ccsinfo.com

String compare problem...

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



Joined: 26 Nov 2005
Posts: 68

View user's profile Send private message

String compare problem...
PostPosted: Sun Oct 19, 2008 1:16 am     Reply with quote

Hi! I am trying to compare two strings but without success! From the debugger is seems that the code stops in the fgets() command but I know that the serial communication works fine… so, there is no any hardware problem. May I have any help or any suggestion?

PS:
I have read some other posts about the string compare functions but I haven’t taken too much help.



Code:

...
char char_out[5],char_in[30];
strcpy(char_out,"\r\nOK");

...
fprintf(module,"AT\r\n");
fgets(char_in,module);   
      
   if(stricmp(char_in,char_out)==0)
   {      
   output_high(led2);      
   delay_ms(500);
   }

output_low(led2);

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 19, 2008 1:38 am     Reply with quote

Create a #use rs232() statement for debug output. Use a software
UART if you have to. Send the output to HyperTerminal, or some other
terminal program running on your PC. Add code to display both strings
before just before the stricmp line is executed. Now you can see what
you are sending to that function.

Don't run it in the debugger. Run it in stand-alone mode.
RayJones



Joined: 20 Aug 2008
Posts: 30
Location: Melbourne, Australia

View user's profile Send private message Visit poster's website

PostPosted: Sun Oct 19, 2008 2:53 am     Reply with quote

Your parameter order is backwards in strcpy!

Ray
RayJones



Joined: 20 Aug 2008
Posts: 30
Location: Melbourne, Australia

View user's profile Send private message Visit poster's website

PostPosted: Sun Oct 19, 2008 2:56 am     Reply with quote

Also, fgets will return once it sees the \r. You won't get to see the \n.
It is possible the \r is also replaces with a \0, and you wont even see the \r

Ray
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Oct 19, 2008 3:44 am     Reply with quote

You're right regarding the control character handling.

The answer from modem is usually "\r\nOK\r\n"

The first call of gets() stops at \r and returns an empty string, the second returns "\nOK", which can't match "OK\r\n".

It may work, to compare for "\nOK", but to my opinion, it's better to define specific
string processing routines that are more tolerant against control character occurance and order.
If you are going to process more complex modem responses than OK, you should be aware,
that the response codes are not exactly specified. They may contain e.g. extra white spaces,
numeric parameters may have leading zeros, string parameters are quoted only with
some products and more stuff like this.
Lykos1986



Joined: 26 Nov 2005
Posts: 68

View user's profile Send private message

PostPosted: Wed Nov 19, 2008 12:14 pm     Reply with quote

FvM wrote:
it's better to define specific
string processing routines that are more tolerant against control character occurance and order.


Specific string processing... an example!
Lykos1986



Joined: 26 Nov 2005
Posts: 68

View user's profile Send private message

PostPosted: Sun Nov 23, 2008 2:49 am     Reply with quote

Hi! I really need help about that! I have tried many different codes but nothing.

May I have any sample code that describes how I will read the OK command that sent the GSM module? The final project will understand more responses from the GSM module but I just want to understand what happens with the code.
I am using the Telis GM862 module. And I have closed the echo response from the module.

Please… a little bit of help, I am despaired…
RayJones



Joined: 20 Aug 2008
Posts: 30
Location: Melbourne, Australia

View user's profile Send private message Visit poster's website

PostPosted: Sun Nov 23, 2008 2:53 am     Reply with quote

simplest method is to delimit your strings by converting all control characters (< 0x20) to nulls.

Then simply search for "OK" and forget about CR LF etc.

Ray
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