View previous topic :: View next topic |
Author |
Message |
Lykos1986
Joined: 26 Nov 2005 Posts: 68
|
String compare problem... |
Posted: Sun Oct 19, 2008 1:16 am |
|
|
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
|
|
Posted: Sun Oct 19, 2008 1:38 am |
|
|
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
|
|
Posted: Sun Oct 19, 2008 2:53 am |
|
|
Your parameter order is backwards in strcpy!
Ray |
|
|
RayJones
Joined: 20 Aug 2008 Posts: 30 Location: Melbourne, Australia
|
|
Posted: Sun Oct 19, 2008 2:56 am |
|
|
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
|
|
Posted: Sun Oct 19, 2008 3:44 am |
|
|
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
|
|
Posted: Wed Nov 19, 2008 12:14 pm |
|
|
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
|
|
Posted: Sun Nov 23, 2008 2:49 am |
|
|
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
|
|
Posted: Sun Nov 23, 2008 2:53 am |
|
|
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 |
|
|
|