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 with strings

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



Joined: 10 Apr 2006
Posts: 22

View user's profile Send private message

Problem with strings
PostPosted: Tue May 09, 2006 8:13 am     Reply with quote

Hello,
I am interfacing with VB and code is not consistent, I am sending word "hello" followed by 13 and it is being accepted by the PIC and prints on lcd and sends back to PC. The problem is if i change the "hello" from PC to something else it will still print wrong password on lcd and writes back to PC as if it compared the correct password.
For whatever reason i cant see it does not compare properly or something.


Thanks!

Code:
#include "PIC16F917.h"
#include <input.c>
#include <LCD.C>
#include <string.h>
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES PUT                    //Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOCPD                    //No EE protection
#FUSES NOBROWNOUT               //Brownout reset
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOFCMEN                  //Fail-safe clock monitor disabled
#FUSES NODEBUG                  //No Debug mode for ICD
#use delay(clock=8000000)
#int_timer0
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7)

char inputin[10];
char password[10]="hello";

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(FALSE);
   setup_lcd(LCD_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
   set_timer0(0);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16,125,16);
   setup_ccp1(CCP_PWM);
   set_pwm1_duty(0);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   setup_oscillator(OSC_8MHZ);
   lcd_init();
   printf(lcd_putc,"\fSECURITY\nINMOTION");
   delay_ms(3000);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
   set_timer0(0);

while (true)
{
            printf(lcd_putc,"\f");
            if (kbhit())
            {
            gets(inputin);
            printf(lcd_putc,"\n%s",inputin);
            if(strcmp(inputin,password))
            {
             printf("\Password OK");
             delay_ms(5000);
            }
            }
            }
}


Ttelmah
Guest







PostPosted: Tue May 09, 2006 9:16 am     Reply with quote

The core reason for your problem, is that STRCMP, does not just return true/false.
STRCMP, returns _0_, if the strings are identical, '1' if one is 'greater' than the other, and '-1' for the other way round. In C, 'true' is any non zero value, so in fact as written, the message will only 'not print', if the strings are identical!.
So what is happening, is that the strings are _never_ being seen as matching. You need to change the test, so that you look for:
if(strcmp(inputin,password)==0)
Now a common reason for this, is that the PC, may be automatically sending LF/CR, rather than just LF. The extra character then gets attached to the front of the incoming string, and the comparison fails. Try echoing back the ascii value of the characters, and see what you are really receiving.

Best Wishes
janiga



Joined: 10 Apr 2006
Posts: 22

View user's profile Send private message

Thanks!
PostPosted: Tue May 09, 2006 9:31 am     Reply with quote

I have tried and will now leave the if(strcmp(inputin,password)==0)
however it has always done the same thing, it still accepts other passwords! Any other suggestions.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Tue May 09, 2006 10:26 am     Reply with quote

Quote:

For whatever reason i cant see it does not compare properly or something.


Yes you can.

Code:

         if(strcmp(inputin,password)==0)
           {
             printf("\Password OK");
             delay_ms(5000);
           }
         else       
           {
             printf("%s",inputin);
           }


Humberto
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