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 support@ccsinfo.com

Problem in using "fgets()" for UART

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



Joined: 03 Mar 2008
Posts: 8

View user's profile Send private message

Problem in using "fgets()" for UART
PostPosted: Fri Mar 14, 2008 3:57 am     Reply with quote

Here, 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()"):

Code:

#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 () 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.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Mar 14, 2008 7:38 am     Reply with quote

Here is the example right out of the help files:
Code:
char string[30];

printf("Password: ");

gets(string);

if(strcmp(string, password))

   printf("OK");

 

You only need fgets() instead of gets() if you are using streams.
_________________
The search for better is endless. Instead simply find very good and get the job done.
EricKoh1985



Joined: 03 Mar 2008
Posts: 8

View user's profile Send private message

PostPosted: Sun Mar 16, 2008 10:02 am     Reply with quote

Since my LED will only turn ON when password is '1111'. So, is it i need to replace 'password' with '1111'?

i have modified the code, as below

Code:

#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()
{
char string[30];

printf("Password: ");
gets(string);

if (strcmp(string,1111))
{
output_b(0b00000001);
   printf("OK");
}
   else
{
   printf("Password Wrong");
}
}



Why when the password is wrong, the RB0 still can turn ON and program display "OK"? (Password command seem likes is not working)
How can i make my program will only display "OK" when password is '1111', other password will display "Password Wrong"?
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Mar 17, 2008 2:25 am     Reply with quote

1111 in your code is not a string but a number. To change it to a string so that strcmp will work put " around it.
Also, strcmp returns 0 for a match so your code will produce the wrong result.

Change to

if (strcmp(string, "1111") == 0)

I also had the following problem, this may not be an issue with your version of the compiler but.

strcmp does not work with string litterals "abcde" but will only work with pointers so I had to do the following.

char match[80];

strcpy(match, "1111"); // Copy "1111" to match array
if (strcmp(string, match) == 0) {


hope this helps.
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