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 CCS Technical Support

how to fgets()
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dbsjro



Joined: 31 Aug 2009
Posts: 17

View user's profile Send private message Send e-mail

how to fgets()
PostPosted: Sun Sep 06, 2009 4:56 am     Reply with quote

Hello everyone!

Can someone provide any help?
I am using pic16f628 and can't make fgets() work.

Any example would be great.
thnx a lot Wink
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Sep 06, 2009 1:45 pm     Reply with quote

This thread has a discussion of fgets() and the get_string() function.
See my sample program for get_string():
http://www.ccsinfo.com/forum/viewtopic.php?t=36796


The #use rs232() statement requires that specific pin numbers
must be used for the 16F628 (or 16F628A). These pins are different
than the 16F877. See this post for an example:
http://www.ccsinfo.com/forum/viewtopic.php?t=37869&start=3
dbsjro



Joined: 31 Aug 2009
Posts: 17

View user's profile Send private message Send e-mail

PostPosted: Mon Sep 07, 2009 11:37 am     Reply with quote

Ok first of all thnx for the reply

I am able to sent/read characters but I had no luck with strings.

I've read these threads but considering I am a newbie I'm kinda lost..

Is there any simple code?(or not that simple but one that is already working)

Arrow THNX Exclamation
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 07, 2009 6:27 pm     Reply with quote

Here's a sample program for get_string(). Just type in a short string
such as "Hello" (without the quotes) and this program will send it back
to the terminal window:
http://www.ccsinfo.com/forum/viewtopic.php?t=36796&start=7
dbsjro



Joined: 31 Aug 2009
Posts: 17

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 09, 2009 11:28 am     Reply with quote

If only i knew as much as you i would be 1000000... times happier Laughing

This seems to work but i have a difficult one now

I wanna sent a string from my PC (can hyperterminal send strings?If not how can i?) and when it is the one i want, lets say "1234",
output_bit(PIN_B0)
else
output_bit(PIN_B1)

Man i hope i wont bother you again
Wink
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 09, 2009 1:05 pm     Reply with quote

Just type in some characters and then press the Enter key. The fgets()
function will detect the Enter key as the end of the input string.


It's better to use the get_string() function, instead of fgets(), because
get_string() allows you to set a limit on the input string size. With fgets()
you can easily type in more characters than the size of your input array.
Then you will overwrite RAM locations beyond the end of the array and
probably cause the program to crash. Example for get_string():
http://www.ccsinfo.com/forum/viewtopic.php?t=17563&start=6
dbsjro



Joined: 31 Aug 2009
Posts: 17

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 09, 2009 1:27 pm     Reply with quote

Why it prints only " You are mistaken "
Can i fix it?

Code:
#include <16F628.h>
#fuses XT,NOWDT,NOPUT,NOPROTECT,NOLVP,BROWNOUT,HS
#use delay(clock=4000000,RESTART_WDT)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, parity = N, bits = 8,STOP=1)
#include <input.c>
#include <stdlib.h>


void main()
{ while(true){
char temp[5];

printf("Enter string: ");

get_string(temp, sizeof(temp));


printf("\n\rReceived: %s \n\r", temp);  //please explain this line


 
    if ( temp == "0609")
         {
         printf(" You found it ");
         }
      else {printf(" You are mistaken ");}
     
     
}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 09, 2009 1:44 pm     Reply with quote

Quote:
if ( temp == "0609")

You can't compare the buffer contents to a literal string in this way.
There are special functions to do this. One of them is called strcmp().
Another useful one is strncmp().

Google for tutorials:
Quote:
strcmp in C

or
Quote:
strncmp in C

or
Quote:
string handling functions in C
dbsjro



Joined: 31 Aug 2009
Posts: 17

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 09, 2009 2:07 pm     Reply with quote

Shocked Shocked Shocked You are amazing!!!!!

I won't trouble you....for the moment at least Wink
dbsjro



Joined: 31 Aug 2009
Posts: 17

View user's profile Send private message Send e-mail

PostPosted: Sat Sep 12, 2009 8:27 am     Reply with quote

More questions Sad

Can i do the same without having to press Enter?
I am sending data but matlab dosen't support keystroke, meaning the program stucks waiting for Enter...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Sep 12, 2009 11:34 am     Reply with quote

Can Matlab send some other character to mark the end of the string ?
(Some character that you can choose). If so, then you can re-write
the get_string() function to look for that character instead of a carriage
return.

If not, then does Matlab send a constant number of digits ? If so, then
you can count the number of digits to tell when the string has been
received.
dbsjro



Joined: 31 Aug 2009
Posts: 17

View user's profile Send private message Send e-mail

PostPosted: Sat Sep 12, 2009 12:29 pm     Reply with quote

If i get what you are saying right then it's yes to both

If it is possible give me examples or anything for both

Thnx
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Sep 12, 2009 12:37 pm     Reply with quote

It's better if you try to code it and debug it. If you still can't make it
work, then ask for help.
dbsjro



Joined: 31 Aug 2009
Posts: 17

View user's profile Send private message Send e-mail

PostPosted: Sat Sep 12, 2009 12:39 pm     Reply with quote

i m lost again Shocked

Code and debug what?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Sep 12, 2009 12:52 pm     Reply with quote

I'm referring to your request for code, here:
Quote:
If it is possible give me examples or anything for both


The purpose of this forum is not to write code for you. We may seem
to do that a lot, but we really shouldn't. If you don't learn how to write
code by yourself, you will forever dependent on some forum to do it
for you. It's far better to be able to do it yourself.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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