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

Problem using strcmp

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







Problem using strcmp
PostPosted: Mon Apr 22, 2002 8:13 am     Reply with quote

Hi,
Can anyone come up with any suggestions for the followng task. I thought I could achieve it by using strcmp, but so far I
haven't been able to.
I want to send a short string, which when received will toggle a pin, ie when the code is being received a line will go high
then drop low once the code has stopped.
Strcmp doesn't appear to work as I anticipated, perhaps there is another way to do this.

At the TX end I have;

#include <16f627.h>
#include <stdio.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP,NOMCLR
#use delay (clock=4000000)
#use rs232(baud=9600,parity=N,enable=PIN_A1,xmit=PIN_A3,rcv=PIN_A2)
#define p printf


main()
{
int i;
int j;

while(true)
{
if(input(PIN_B0))
{
p("AB\r");
if(!input(PIN_B0))
{
for(i = 0; i < 5; i++)
p("ST\r");
}
}
else if(input(PIN_B1))
{
p("CD\r");
if(!input(PIN_B1))
{
for(j = 0; j < 5; j++)
p("ST\r");
}
}

else;

}//while end
}//main end



AB and CD are the short strings, ST is sent in an attempt get the RX end to drop the line low again because the string
compare is not true anymore.


At the RX end I have;

#include <16f627.h>
#include <string.h>
#include <stdio.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP,NOMCLR
#use delay (clock=4000000)
#use rs232(baud=9600,parity=N,enable=PIN_A1,xmit=PIN_A3,rcv=PIN_A2)


main()
{

char code[3];
char code_1[3] = "AB";
char Code_2[3] = "CD";

output_low(PIN_B1);
output_low(PIN_B2);

while(true)
{
gets(code);
while(strcmp(code,code_1))
{
gets(code);
output_high(PIN_B1);
}

if(!strcmp(code,code_1))
output_low(PIN_B1);

while(strcmp(code,code_2))
{
gets(code);
output_high(PIN_B2);
}

if(!strcmp(code,code_2))
output_low(PIN_B2);


}//while end
}//main end

I'm not sure about the Char string lengths, should 'char code' be [3] to take into account the \r?

I appreciate my code may look messy or unorthadox but I have been chopping it about to try to get it working.

Any help much appreciated.
___________________________
This message was ported from CCS's old forum
Original Post ID: 3932
Todd C. Micallef
Guest







Re: Problem using strcmp
PostPosted: Mon Apr 22, 2002 9:32 am     Reply with quote

Is the project already hardwired? If not try using the built in
USART in the chip for the communications, unless you are using RS-485 as indicated by your use of "enable" in the #use_rs232 statement. That is not necessary for regular serial communications.

Also, try using a single character instead of a string. This will eliminate the strcmp() functions. At least for temporary troubleshooting to minimize the possible programming errors.



:=Hi,
:=Can anyone come up with any suggestions for the followng task. I thought I could achieve it by using strcmp, but so far I
:=haven't been able to.
:=I want to send a short string, which when received will toggle a pin, ie when the code is being received a line will go high
:=then drop low once the code has stopped.
:=Strcmp doesn't appear to work as I anticipated, perhaps there is another way to do this.
:=
:=At the TX end I have;
:=
:=#include <16f627.h>
:=#include <stdio.h>
:=#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP,NOMCLR
:=#use delay (clock=4000000)
:=#use rs232(baud=9600,parity=N,enable=PIN_A1,xmit=PIN_A3,rcv=PIN_A2)
:=#define p printf
:=
:=
:=main()
:={
:= int i;
:= int j;
:=
:= while(true)
:= {
:= if(input(PIN_B0))
:= {
:= p("AB\r");
:= if(!input(PIN_B0))
:= {
:= for(i = 0; i < 5; i++)
:= p("ST\r");
:= }
:= }
:= else if(input(PIN_B1))
:= {
:= p("CD\r");
:= if(!input(PIN_B1))
:= {
:= for(j = 0; j < 5; j++)
:= p("ST\r");
:= }
:= }
:=
:= else;
:=
:= }//while end
:=}//main end
:=
:=
:=
:=AB and CD are the short strings, ST is sent in an attempt get the RX end to drop the line low again because the string
:=compare is not true anymore.
:=
:=
:=At the RX end I have;
:=
:=#include <16f627.h>
:=#include <string.h>
:=#include <stdio.h>
:=#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP,NOMCLR
:=#use delay (clock=4000000)
:=#use rs232(baud=9600,parity=N,enable=PIN_A1,xmit=PIN_A3,rcv=PIN_A2)
:=
:=
:=main()
:={
:=
:= char code[3];
:= char code_1[3] = "AB";
:= char Code_2[3] = "CD";
:=
:= output_low(PIN_B1);
:= output_low(PIN_B2);
:=
:= while(true)
:= {
:= gets(code);
:= while(strcmp(code,code_1))
:= {
:= gets(code);
:= output_high(PIN_B1);
:= }
:=
:= if(!strcmp(code,code_1))
:= output_low(PIN_B1);
:=
:= while(strcmp(code,code_2))
:= {
:= gets(code);
:= output_high(PIN_B2);
:= }
:=
:= if(!strcmp(code,code_2))
:= output_low(PIN_B2);
:=
:=
:= }//while end
:=}//main end
:=
:=I'm not sure about the Char string lengths, should 'char code' be [3] to take into account the \r?
:=
:=I appreciate my code may look messy or unorthadox but I have been chopping it about to try to get it working.
:=
:=Any help much appreciated.
___________________________
This message was ported from CCS's old forum
Original Post ID: 3939
Ian H
Guest







Re: Problem using strcmp
PostPosted: Mon Apr 22, 2002 10:18 am     Reply with quote

I have already tried the single character route thus eliminating strcmp and that works. However I thought that a longer string would make the code more rugged and less prone to errors etc. I will look at the harware USART as way round it but I thought strcmp would be quicker and neater.
I used enable in the #use rs232 statement to control a TXing led.
:=Is the project already hardwired? If not try using the built in
:=USART in the chip for the communications, unless you are using RS-485 as indicated by your use of "enable" in the #use_rs232 statement. That is not necessary for regular serial communications.
:=
:=Also, try using a single character instead of a string. This will eliminate the strcmp() functions. At least for temporary troubleshooting to minimize the possible programming errors.
:=
:=
:=
:=:=Hi,
:=:=Can anyone come up with any suggestions for the followng task. I thought I could achieve it by using strcmp, but so far I
:=:=haven't been able to.
:=:=I want to send a short string, which when received will toggle a pin, ie when the code is being received a line will go high
:=:=then drop low once the code has stopped.
:=:=Strcmp doesn't appear to work as I anticipated, perhaps there is another way to do this.
:=:=
:=:=At the TX end I have;
:=:=
:=:=#include <16f627.h>
:=:=#include <stdio.h>
:=:=#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP,NOMCLR
:=:=#use delay (clock=4000000)
:=:=#use rs232(baud=9600,parity=N,enable=PIN_A1,xmit=PIN_A3,rcv=PIN_A2)
:=:=#define p printf
:=:=
:=:=
:=:=main()
:=:={
:=:= int i;
:=:= int j;
:=:=
:=:= while(true)
:=:= {
:=:= if(input(PIN_B0))
:=:= {
:=:= p("AB\r");
:=:= if(!input(PIN_B0))
:=:= {
:=:= for(i = 0; i < 5; i++)
:=:= p("ST\r");
:=:= }
:=:= }
:=:= else if(input(PIN_B1))
:=:= {
:=:= p("CD\r");
:=:= if(!input(PIN_B1))
:=:= {
:=:= for(j = 0; j < 5; j++)
:=:= p("ST\r");
:=:= }
:=:= }
:=:=
:=:= else;
:=:=
:=:= }//while end
:=:=}//main end
:=:=
:=:=
:=:=
:=:=AB and CD are the short strings, ST is sent in an attempt get the RX end to drop the line low again because the string
:=:=compare is not true anymore.
:=:=
:=:=
:=:=At the RX end I have;
:=:=
:=:=#include <16f627.h>
:=:=#include <string.h>
:=:=#include <stdio.h>
:=:=#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP,NOMCLR
:=:=#use delay (clock=4000000)
:=:=#use rs232(baud=9600,parity=N,enable=PIN_A1,xmit=PIN_A3,rcv=PIN_A2)
:=:=
:=:=
:=:=main()
:=:={
:=:=
:=:= char code[3];
:=:= char code_1[3] = "AB";
:=:= char Code_2[3] = "CD";
:=:=
:=:= output_low(PIN_B1);
:=:= output_low(PIN_B2);
:=:=
:=:= while(true)
:=:= {
:=:= gets(code);
:=:= while(strcmp(code,code_1))
:=:= {
:=:= gets(code);
:=:= output_high(PIN_B1);
:=:= }
:=:=
:=:= if(!strcmp(code,code_1))
:=:= output_low(PIN_B1);
:=:=
:=:= while(strcmp(code,code_2))
:=:= {
:=:= gets(code);
:=:= output_high(PIN_B2);
:=:= }
:=:=
:=:= if(!strcmp(code,code_2))
:=:= output_low(PIN_B2);
:=:=
:=:=
:=:= }//while end
:=:=}//main end
:=:=
:=:=I'm not sure about the Char string lengths, should 'char code' be [3] to take into account the \r?
:=:=
:=:=I appreciate my code may look messy or unorthadox but I have been chopping it about to try to get it working.
:=:=
:=:=Any help much appreciated.
___________________________
This message was ported from CCS's old forum
Original Post ID: 3940
magic
Guest







Re: Problem using strcmp
PostPosted: Mon Apr 22, 2002 12:25 pm     Reply with quote

Okay Man...
change strcmp-->stricmp (diffrent is only "i")
note that when "equal" result is 0... not 1
example:

char a[3]="ab";
char b[3]="ab";

if(!stricmp(a,b))// NOW result is zero okay?
{
printf("a=b");
}


good luck its not magic






:=Hi,
:=Can anyone come up with any suggestions for the followng task. I thought I could achieve it by using strcmp, but so far I
:=haven't been able to.
:=I want to send a short string, which when received will toggle a pin, ie when the code is being received a line will go high
:=then drop low once the code has stopped.
:=Strcmp doesn't appear to work as I anticipated, perhaps there is another way to do this.
:=
:=At the TX end I have;
:=
:=#include <16f627.h>
:=#include <stdio.h>
:=#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP,NOMCLR
:=#use delay (clock=4000000)
:=#use rs232(baud=9600,parity=N,enable=PIN_A1,xmit=PIN_A3,rcv=PIN_A2)
:=#define p printf
:=
:=
:=main()
:={
:= int i;
:= int j;
:=
:= while(true)
:= {
:= if(input(PIN_B0))
:= {
:= p("AB\r");
:= if(!input(PIN_B0))
:= {
:= for(i = 0; i < 5; i++)
:= p("ST\r");
:= }
:= }
:= else if(input(PIN_B1))
:= {
:= p("CD\r");
:= if(!input(PIN_B1))
:= {
:= for(j = 0; j < 5; j++)
:= p("ST\r");
:= }
:= }
:=
:= else;
:=
:= }//while end
:=}//main end
:=
:=
:=
:=AB and CD are the short strings, ST is sent in an attempt get the RX end to drop the line low again because the string
:=compare is not true anymore.
:=
:=
:=At the RX end I have;
:=
:=#include <16f627.h>
:=#include <string.h>
:=#include <stdio.h>
:=#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP,NOMCLR
:=#use delay (clock=4000000)
:=#use rs232(baud=9600,parity=N,enable=PIN_A1,xmit=PIN_A3,rcv=PIN_A2)
:=
:=
:=main()
:={
:=
:= char code[3];
:= char code_1[3] = "AB";
:= char Code_2[3] = "CD";
:=
:= output_low(PIN_B1);
:= output_low(PIN_B2);
:=
:= while(true)
:= {
:= gets(code);
:= while(strcmp(code,code_1))
:= {
:= gets(code);
:= output_high(PIN_B1);
:= }
:=
:= if(!strcmp(code,code_1))
:= output_low(PIN_B1);
:=
:= while(strcmp(code,code_2))
:= {
:= gets(code);
:= output_high(PIN_B2);
:= }
:=
:= if(!strcmp(code,code_2))
:= output_low(PIN_B2);
:=
:=
:= }//while end
:=}//main end
:=
:=I'm not sure about the Char string lengths, should 'char code' be [3] to take into account the \r?
:=
:=I appreciate my code may look messy or unorthadox but I have been chopping it about to try to get it working.
:=
:=Any help much appreciated.
___________________________
This message was ported from CCS's old forum
Original Post ID: 3945
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