View previous topic :: View next topic |
Author |
Message |
boham
Joined: 12 Mar 2008 Posts: 5
|
Error using strcmp() |
Posted: Wed Apr 09, 2008 7:34 am |
|
|
I am having an error whenever I use the strcmp function. Any help would be appreciated.
Code: | #include <18F4520.h>
#fuses HS,NOLVP, NOWDT
#use delay(clock = 20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h> // for strcmp()
#define PUSH_BUTTON PIN_A4
void wait_for_one_press(){
while(input(PUSH_BUTTON));
delay_ms(100); // switch de-bounce
while(!input(PUSH_BUTTON));
}
void BR_get_string(char *s){ //blue radio get string
char input;
input = getc(); // <CR>
getc(); // <LF>
input = getc(); // message<CR> NOTE: <CR> is not put into the array.
input = getc();; // <LF>
}
void main(){
char myStr[25]; // make this only as big as the longest message plus one for the null character '\0'
while(TRUE)
{
wait_for_one_press();
printf("ATSW25,1,1,0,0\r");
BR_get_string(myStr);
if (strcmp(myStr, &"OK"))
continue;
printf("ATDM,00A096141292,1101\r");
//BR_get_string(myStr);
//if (strcmp(myStr, &"CONNECT,00A096141292"))
// continue;
//printf("+++\r");
//BR_get_string(myStr);
//if (strcmp(myStr, &"OK"))
// continue;
//printf("ATEDSCO\r\n");
}
}
|
At the line Code: | if (strcmp(myStr, &"OK")) |
and the other places in the comments above, I get the error:
Quote: | Expecting an identifier |
|
|
|
meereck
Joined: 09 Nov 2006 Posts: 173
|
|
Posted: Wed Apr 09, 2008 7:47 am |
|
|
hi, you need to have a string in RAM if you want to use STRCMP function on it.
I usually do smthg like this:
Code: |
char tempbuf[10];
sprintf(tempbuf,"OK");
if(strcmp(receivecharacterbuffer,tempbuf)!=0) printf("different"); |
see this to understand what strcmp returns : http://www.elook.org/programming/c/strcmp.html |
|
|
boham
Joined: 12 Mar 2008 Posts: 5
|
|
Posted: Wed Apr 09, 2008 7:53 am |
|
|
No errors! Thank you very much. I'll see later on how it runs in the PIC |
|
|
Matro Guest
|
|
Posted: Wed Apr 09, 2008 8:01 am |
|
|
Another way to make it work...
I take the following "line" as an example but all similar lines have the same problem :
Code: |
(strcmp(myStr, &"OK"))
|
First, to use constant strings, you need to add in your code :
Code: |
#device PASS_STRINGS = IN_RAM
|
And then the '&' shall not be present :
Code: |
(strcmp(myStr, "OK"))
|
Now it should work.
Matro. |
|
|
boham
Joined: 12 Mar 2008 Posts: 5
|
|
Posted: Wed Apr 09, 2008 11:02 am |
|
|
Allrighty... The code looks just to me but I think the direction I am going is not right.
The radio I am controling receives a command and replies with an OK status if everything is good.
I can command this thing all day long but it seems that I cannot check for that OK reply.
Quote: | Sent : ATSW25,1,1,0,0<cr> // Set ConnectMode Master & data mode
Reply:<cr_lf>OK<cr_lf> |
Is what I am trying to accomplish.
The above code works if I do not try to invoke the
BR_get_string(myStr);
command.
What I want to do is take that reply and continue if the reply is
<cr_lf>OK<cr_lf>
and not continue otherwise. |
|
|
meereck
Joined: 09 Nov 2006 Posts: 173
|
|
Posted: Wed Apr 09, 2008 11:21 am |
|
|
Matro wrote: | Another way to make it work...
First, to use constant strings, you need to add in your code :
Code: |
#device PASS_STRINGS = IN_RAM
|
|
It probably doesnt work with v3 compilers, does it? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 09, 2008 11:40 am |
|
|
That's right. It requires vs. 4 to work.
In vs. 3, you have to copy the string to a RAM array with explicit code. |
|
|
ftrax
Joined: 24 Sep 2013 Posts: 22 Location: EspaƱa
|
|
Posted: Fri May 08, 2015 5:02 am |
|
|
thank you very much Matro, I've been looking for any instruction like this for a long time, thank you very much !!
|
|
|
|