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 Previous  1, 2
 
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

PostPosted: Sat Sep 12, 2009 1:04 pm     Reply with quote

You misunderstood me

I don't want this either. I search a lot before i post

That's why i asked for examples

The whole project is my idea and i m doing this alone, and when i stuck i m still alone Laughing

All i need is an example, ccs c manual is not much help

I don't understand what's a carriage return or how to count the number of digits
dyeatman



Joined: 06 Sep 2003
Posts: 1939
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat Sep 12, 2009 1:19 pm     Reply with quote

Look at decimal 13 and decimal 10 in the ASCII Character chart:
http://www.asciitable.com/

When the Enter key is pressed it sends a Carriage Return and the Line Feed
(new line) character one after the other. The gets() routine requires the
Carriage Return character to know when it is the end of the string otherwise
it just keeps waiting for the end.

If you do not use Enter you cannot use gets() or get_string(). You will have
to get each character one at a time and test it for what you designate as the
last character to be sent. As an example I look for the ~ character since it
is rarely used and I can include it as the terminator. So when I see it come in I know it is the last one and I then
process the ones I have received.
_________________
Google and Forum Search are some of your best tools!!!!


Last edited by dyeatman on Sat Sep 12, 2009 3:10 pm; edited 4 times in total
dbsjro



Joined: 31 Aug 2009
Posts: 17

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

PostPosted: Sat Sep 12, 2009 2:01 pm     Reply with quote

Thnx dyeatman for the ASCII Character chart, I now understand why we use \r\n.

But I use get_string not gets().

Since the purpose of this forum is not to write code but no help is provided on how to write code myself I'll try somewhere else.

Anyway thnx for your time.
dyeatman



Joined: 06 Sep 2003
Posts: 1939
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat Sep 12, 2009 3:07 pm     Reply with quote

OK. Both routines basically work the same way. Both functions put a null (zero) at the end of the string for you when they return.

The difference is that get_string() also allows you to set a maximum length.
Both require a decimal 13 character to exit and return from the routine.

You can see the entire get_string() routine in the file INPUT.C which can be found in the PICC\Drivers directory.
_________________
Google and Forum Search are some of your best tools!!!!
dbsjro



Joined: 31 Aug 2009
Posts: 17

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

PostPosted: Sat Sep 12, 2009 3:14 pm     Reply with quote

Yes but the thing is that I have no clue about how to make my pic read a string with get_string() without pressing Enter.

Don't know where to start from.
SherpaDoug



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

View user's profile Send private message

PostPosted: Sat Sep 12, 2009 4:06 pm     Reply with quote

dbsjro wrote:
Yes but the thing is that I have no clue about how to make my pic read a string with get_string() without pressing Enter.

Don't know where to start from.


I think the point is that you can't. Get-string() will wait forever until it receives the <CRLF>. You can not read a string with get_string() without pressing Enter.

You CAN use getc() and read the characters one at a time and build up the string in PIC memory. But somehow you must tell the PIC when the string is finished. You either must use some magical byte sequence (like that generated by Enter), or use a fixed number of characters, or use a time limit, or SOMETHING.
_________________
The search for better is endless. Instead simply find very good and get the job done.
dyeatman



Joined: 06 Sep 2003
Posts: 1939
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat Sep 12, 2009 4:11 pm     Reply with quote

Did you study the get_string() function I referenced and try to understand how it works?

Going back to what was said above, you will have to know either what
the length will be or what terminator character you will use. The easiest
way is to use a variation of get_string() and change the terminator char
it looks for.

Change the name of the routine AND the third line from the end " } while(c!=13); "


Code:

void get_new_string(char* s, unsigned int8 max) { <== change the name
   unsigned int8 len;
   char c;

   --max;
   len=0;
   do {
     c=getc();
     if(c==8) {  // Backspace
        if(len>0) {
          len--;
          putc(c);
          putc(' ');
          putc(c);
        }
     } else if ((c>=' ')&&(c<='~'))
       if(len<=max) {
         s[len++]=c;
         putc(c);
       }
    } while(c!='~'); <<=== change the desired terminator char like this
   s[len]=0;
}

_________________
Google and Forum Search are some of your best tools!!!!


Last edited by dyeatman on Sat Sep 12, 2009 7:21 pm; edited 1 time in total
dbsjro



Joined: 31 Aug 2009
Posts: 17

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

PostPosted: Sat Sep 12, 2009 4:55 pm     Reply with quote

Of course I tried to understand how it works, as I could that is.

I did copy/paste the code you gave me in the input.c.
I still need enter despite the fact that I'm using get_new_string.


When I finish it I'll tell you what my project is, if you want.
dyeatman



Joined: 06 Sep 2003
Posts: 1939
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat Sep 12, 2009 7:16 pm     Reply with quote

That's interesting because the routiine works perfectly for me...

Code:
#include <18F4520.h>
#fuses XT, NOWDT, NOPROTECT,PUT,BROWNOUT,NOLVP
#use delay(clock=8000000)

//-----< Serial Port Definition >-----
#use rs232(baud=9600, xmit=Pin_C6, rcv=Pin_C7, ERRORS, stream = PC)

char strg[5];

//====================================
void get_new_string(char* s, unsigned int8 max) { // <== change the name
   unsigned int8 len;
   char c;

   --max;
   len=0;
   do {
     c=getc();
     if(c==8) {  // Backspace
        if(len>0) {
          len--;
          putc(c);
          putc(' ');
          putc(c);
        }
     } else if ((c>=' ')&&(c<='~'))
       if(len<=max) {
         s[len++]=c;
         putc(c);
       }
    } while(c!='~'); // <<=== change the desired terminator char like this
   s[len]=0;
}

void main()
{
 fprintf(PC, "Starting Test...");
 
while(1)
  {
  printf(" waiting for string...\r\n\n");
  get_new_string(strg, sizeof(strg));
  fprintf(PC, "\r\n\n  String is: %S \n\n\r",strg);
   delay_ms(500);
  }

}

_________________
Google and Forum Search are some of your best tools!!!!
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 Previous  1, 2
Page 2 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