Problem in using "fgets()" for UART

Discussion in 'Embedded Systems and Microcontrollers' started by erickoh1985, Mar 14, 2008.

  1. erickoh1985

    Thread Starter New Member

    Feb 28, 2008
    7
    0
    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.
     
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    You're not using fgets() correctly, but it looks like you already know that.
    Code ( (Unknown Language)):
    1.  
    2. void main()
    3. {  
    4.    char input[20]; /* You can adjust the size of the array */
    5.    char * result;
    6.    printf("Please enter password\n");
    7.    while(1)
    8.    {
    9.        result = fgets(input, 80, stdin);
    10.        /* check for bad input not shown */
    11.        if (strcmp(input, "1111") == 0)
    12.          output_a(0b00000001);
    13.        else
    14.           printf("Password Wrong\n");
    15.    }
    16. }
    17.  
    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
     
Loading...