Problem in using "fgets()" for UART

Thread Starter

erickoh1985

Joined Feb 28, 2008
7
i would like to do a password system to turn ON RB0's LED. For example, the password is "1111".
My problem is i don't know how to use "fgets()" for UART.
Below is my code (with missing code for "fgets()"):


#include <16F628A.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP,HS
#include <stdio.h>
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1)

void main()
{
printf("Please enter password\n");
while(1)
{

if (fgets( )) //inside the "fgets( )" should be the password code "1111"
output_a(0b00000001);

else
printf("Password Wrong\n");
}
}



Picture of Cirucit Design: http://www.imagehosting.com/show.php/1629177_UART.JPG.html

Thanks.
 

Mark44

Joined Nov 26, 2007
628
You're not using fgets() correctly, but it looks like you already know that.
Rich (BB code):
void main()
{  
   char input[20]; /* You can adjust the size of the array */
   char * result;
   printf("Please enter password\n");
   while(1)
   {
       result = fgets(input, 80, stdin);
       /* check for bad input not shown */
       if (strcmp(input, "1111") == 0)
         output_a(0b00000001);
       else
          printf("Password Wrong\n");
   }
}
Something like the above should work. You should check the value returned by fgets(). If it's EOF, an error occurred. If it's 0, no error occurred.

You probably don't need an array that can hold 19 characters + NULL. If not, change to what you need. Also, the hard-coded 80 in the call to fgets is the maximum line length, which you might want to adjust.

Also, I didn't include the #includes etc.-- just main()

Mark
 
Top