View previous topic :: View next topic |
Author |
Message |
dbsjro
Joined: 31 Aug 2009 Posts: 17
|
how to fgets() |
Posted: Sun Sep 06, 2009 4:56 am |
|
|
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 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
dbsjro
Joined: 31 Aug 2009 Posts: 17
|
|
Posted: Mon Sep 07, 2009 11:37 am |
|
|
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)
THNX |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
dbsjro
Joined: 31 Aug 2009 Posts: 17
|
|
Posted: Wed Sep 09, 2009 11:28 am |
|
|
If only i knew as much as you i would be 1000000... times happier
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
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 09, 2009 1:05 pm |
|
|
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
|
|
Posted: Wed Sep 09, 2009 1:27 pm |
|
|
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
|
|
Posted: Wed Sep 09, 2009 1:44 pm |
|
|
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:
or
or
Quote: | string handling functions in C |
|
|
|
dbsjro
Joined: 31 Aug 2009 Posts: 17
|
|
Posted: Wed Sep 09, 2009 2:07 pm |
|
|
You are amazing!!!!!
I won't trouble you....for the moment at least |
|
|
dbsjro
Joined: 31 Aug 2009 Posts: 17
|
|
Posted: Sat Sep 12, 2009 8:27 am |
|
|
More questions
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
|
|
Posted: Sat Sep 12, 2009 11:34 am |
|
|
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
|
|
Posted: Sat Sep 12, 2009 12:29 pm |
|
|
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
|
|
Posted: Sat Sep 12, 2009 12:37 pm |
|
|
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
|
|
Posted: Sat Sep 12, 2009 12:39 pm |
|
|
i m lost again
Code and debug what? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Sep 12, 2009 12:52 pm |
|
|
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. |
|
|
|