|
|
View previous topic :: View next topic |
Author |
Message |
janiga
Joined: 10 Apr 2006 Posts: 22
|
Problem with strings |
Posted: Tue May 09, 2006 8:13 am |
|
|
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
|
|
Posted: Tue May 09, 2006 9:16 am |
|
|
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
|
Thanks! |
Posted: Tue May 09, 2006 9:31 am |
|
|
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
|
|
Posted: Tue May 09, 2006 10:26 am |
|
|
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 |
|
|
|
|
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
|