|
|
View previous topic :: View next topic |
Author |
Message |
NickStout
Joined: 09 Nov 2005 Posts: 12
|
Writing a function to check passwords |
Posted: Thu Nov 17, 2005 3:55 pm |
|
|
I've got a small problem with some code I'm writing. I've got all the rest of the function correct, but this code is getting stuck in the loop and never finishing.
Code: | int check_pw(void) {
int i=0;
char pw[]="abcdefgh";
char *rx_pw;
lcd_putc("\fGot here.");
gets(rx_pw);
lcd_putc("\fGot past gets.");
if (strcmp(rx_pw,pw)) {
<... checking code and return>;
} |
My problem is that I'm not getting a response from gets. I realize I might have an error in my later code (not including the \0 null for the string), but for some reason, my gets never responds. (I'm sending data via a terminal program... hyperterm )
Sorry this isn't complete code, but my other getc's and putc's work in my code... I'm just curious if you can see the problem at a glance. Is there something I need to do to use gets? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 17, 2005 4:19 pm |
|
|
You need to press the Enter key to make gets() accept the string.
However, you can easily blow past the end of the array if you use
gets(), because there's no limit on the number of characters the user
can enter. It's better to use the get_string() function, because it
has a parameter for the maximum number of chars to accept.
The get_string() function is in INPUT.C, which is in this folder:
c:\Program Files\PICC\Drivers |
|
|
NickStout
Joined: 09 Nov 2005 Posts: 12
|
|
Posted: Thu Nov 17, 2005 4:37 pm |
|
|
Hm, I'm trying to capture the data so I can check it. The get_string command only sends it back out, so that's not too useful here. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 17, 2005 4:49 pm |
|
|
Quote: | The get_string command only sends it back out, so that's not too useful here. |
If that is so, why does it have "get" in it's name ?
Example of using get_string() to get a string entered from a terminal.
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use RS232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <input.c>
//=============================
void main()
{
int8 data[10];
get_string(data, 10); // Get up to 9 characters
printf("\n\rGot: %s\n\r", data);
while(1);
} |
|
|
|
NickStout
Joined: 09 Nov 2005 Posts: 12
|
|
Posted: Thu Nov 17, 2005 5:10 pm |
|
|
I can't seem to get that to work either... hmm, I'll have to try a few other things to confirm that my hardware still works. :-p |
|
|
Eric Minbiole
Joined: 10 Jun 2004 Posts: 16 Location: USA
|
Re: Writing a function to check passwords |
Posted: Thu Nov 17, 2005 7:07 pm |
|
|
You've declared rx_pw as a char pointer, but you haven't allocated any storage for it. The call to gets() is likely stomping on some random location in RAM. I would change its declaration to something like
Code: | char rx_pw[MAX_PASSWORD_LEN] |
See if that takes care of your problem... |
|
|
NickStout
Joined: 09 Nov 2005 Posts: 12
|
|
Posted: Thu Nov 17, 2005 9:24 pm |
|
|
You're all right, but it's got to be something else... The PIC appears to be ignoring the RS232, even though the data is being sent.
At the end of my code, I added:
Code: | while (1) {
putc('a');
c = getc();
putc(c); |
I get the a, but I'm not getting any character I type back like I expect.
How is it possible that I can see one thing but not the other?
I tried doing a main and adding the simple code similar to above and I got the results I expect... do I need to reset the UART somehow? |
|
|
NickStout
Joined: 09 Nov 2005 Posts: 12
|
|
Posted: Fri Nov 18, 2005 4:44 pm |
|
|
Code: | int check_pw(int my_id) {
int i=0;
char pw[3][8]={{'a','b','c','d','e','f','g','h'},{'g','h','i','j','k','l','m','n'},{'o','p','q','r','s','t','u','v'}};
char rx_pw[8];
puts("\fPlease enter your password:");
do {
rx_pw[i]=getc();
i++;
} while (i<8);
for (i=0;i<8;i++) {
if (pw[my_id][i] != rx_pw[i])
break;
if (i==7) return 1;
}
return 0;
} |
I got the code working (stupid wiring problem). I have one more question, though...
If I wanted to add a timer to the above code, i.e. if no response for 8 seconds, return 0... how would I do that? I'm guessing this is going to be interrupts, since the getc() command is going to halt the program there.
Thanks for all of your help. |
|
|
Ttelmah Guest
|
|
Posted: Sat Nov 19, 2005 3:02 am |
|
|
There are a number of 'ways' of doing this, and a search in the archives here should find a few. I the past, somebody (I think it wwas Neutone?), published a 'timed getc', which implements exactly this, and it should be findable.
Best Wishes |
|
|
|
|
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
|