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

fgets & suart : EZY Plz Help ~

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







fgets & suart : EZY Plz Help ~
PostPosted: Mon Jan 14, 2008 11:38 am     Reply with quote

Searching more & more in forum about fgets & software uart but my program still not work ... Plz Help Me ~

1. I use suart = pin j4 & j5 link to HW buffer or HW line driver (Max232) and send data to PIC18F8722 by Hyperterminal
2. This program work with fgetc but fgets isn't.

/******************************************************************** Include */
#include <18F8722.h> // PIC18F8722

/************************************************ Setting Configuration Fuses */
#fuses H4,NOLVP,NOWDT,NOPROTECT,NOSTVREN // PIC18F8722
#use delay(clock=40000000) // OSC
#use rs232(baud=9600, parity=N, bits=8, stop=1, xmit=PIN_J5, rcv=PIN_J4, timeout=5000, stream=COM1)

/******************************************************************** Include */
#include <stdio.h> // Printf
#include <stdlib.h> // Standard Library
#include <string.h> // String Library
#include "input.c" // fgets

/*********************************************************** Declare Variable */
char strRF[5];

/************************************************************* Main Functions */
void main(void){
strRF[4]='\0';
while (TRUE){
fprintf(COM1,"\r\nWait");
fgets(strRF,COM1); \\Send data via hypertermianl end with ENTER, 5 s time out
fprintf(COM1,"\r\n%x",strRF);
fprintf(COM1,"\r\nNext");
delay_ms(500);
}
}
/*************************** End of $Workfile: $ ******************************/
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jan 14, 2008 12:26 pm     Reply with quote

When posting code please use the 'code' buttons to preserve formatting and make for easier reading.

What is your compiler version number?

Note that fgets() does not check for overflow of the receive buffer. If you are sending more than 4 characters to the PIC then strRF will overflow and you are corrupting other variables in RAM with resulting unspecified behaviour. Maybe this is what is happening?

Try using a larger buffer or use get_string() from input.c which is more versatile.
Newbie2008
Guest







Thankyou
PostPosted: Mon Jan 14, 2008 12:58 pm     Reply with quote

Quote:
When posting code please use the 'code' buttons to preserve formatting and make for easier reading.


Sorry for this, I'm newbie ... & i will test get_string() tonight.

thank you for answer.
Newbie2008
Guest







Work Perfectly !!!
PostPosted: Mon Jan 14, 2008 3:29 pm     Reply with quote

I swap MCU from 18F8722 to 18F4550 and change char strRF[5] to
char strRF[100], then after enter I received all data return to hyperterminal. Almost sure, the problem is buffer overflow.

Code:
/******************************************************************** Include */
#include <18F4550.h>

/************************************************ Setting Configuration Fuses */
#fuses HS,NOWDT,NOPROTECT,NOLVP  //Configuration Word
#use delay(clock=20000000)  //Change OSC Here
#use rs232(baud=9600, parity=N, bits=8, stop=1, xmit=PIN_D5, rcv=PIN_D4, timeout=5000, stream=COM1)

/******************************************************************** Include */
#include // Printf
#include // Standard Library
#include // String Library
#include "input.c" // fgets

/*********************************************************** Declare Variable */
//char strRF[5];
char strRF[100];          //<=== Change from 5 to 100, Gaurantee Work !!!

/************************************************************* Main Functions */
void main(void){
strRF[4]='\0';
while (TRUE){
fprintf(COM1,"\r\nWait");
fgets(strRF,COM1); \\Send data via hypertermianl end with ENTER, 5 s time out
fprintf(COM1,"\r\n%x",strRF);
fprintf(COM1,"\r\nNext");
delay_ms(500);
}
}
/*************************** End of $Workfile: $ ******************************/


Can u roughly explain get_string() ? Searching along the forum, I got a little bit of detail. Does it has parameters string, stream ... likes fgets(string,stream) ... or something like that ?.

PS. Thank You
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jan 14, 2008 4:38 pm     Reply with quote

Quote:
Can u roughly explain get_string() ?
Get_string() is a kind of CCS provided 'bonus' function. It is in the file input.c which contains more useful but poorly documented functions.

Get_string() reads characters (using GETC()) into the string until a RETURN (value 13) is encountered or the end of the buffer is reached. The string is terminated with a 0. The buffer length specified should include the terminating zero.
There is also some 'line editing' code present: when the user presses backspace the last character is deleted from the buffer and a character sequence is send to the terminal to erase the last visible character. Very user friendly when a person is typing the input but might give problems when a computer is sending the data.

It is not possible to specify a stream by parameter, but what you can do:
1) Copy the code and change the call to getc() to include your stream.
or,
2) Get_string() uses STDIN, this is the last specified #rs232 line. So the quick-and-dirty way is to make sure there are no other #rs232 lines between your COM1 stream definition and the include of input.c (it looks like this is already the situation in your code).
Newbie2008
Guest







18F4550 & 18F8722
PostPosted: Tue Jan 15, 2008 3:56 am     Reply with quote

First, thank you to ckielstra for his kindly support in giving useful suggestion of get_string().

Next, Fixed a typo in a previous code (18F4550):

Code:
 /******************************************************************** Include */
#include <18F4550.h>

/************************************************ Setting Configuration Fuses */
#fuses HS,NOWDT,NOPROTECT,NOLVP  //Configuration Word
#use delay(clock=20000000)  //Change OSC Here
#use rs232(baud=9600, parity=N, bits=8, stop=1, xmit=PIN_D5, rcv=PIN_D4, timeout=5000, stream=COM1)

/******************************************************************** Include */
#include <stdio.h>   // Printf
#include <stdlib.h>  // Standard Library
#include <string.h>  // String Library
#include "input.c"

/*********************************************************** Declare Variable */
//char strRF[5];
char strRF[100];          //<=== Change from 5 to 100, Gaurantee Work !!!

/************************************************************* Main Functions */
void main(void){
strRF[4]='\0';
while (TRUE){
fprintf(COM1,"\r\nWait");
fgets(strRF,COM1); \\Send data via hypertermianl end with ENTER, 5 s time out
fprintf(COM1,"\r\n%s",strRF);
fprintf(COM1,"\r\nNext");
delay_ms(500);
}
}
/*************************** End of $Workfile: $ ******************************/


and I have tested the command "Fgetc" in 18F8722 & the result is correct :

Code:
/******************************************************************** Include */
//#include <18F4550.h>
#include <18F8722.h>  // PIC18F8722

/************************************************ Setting Configuration Fuses */
#fuses H4,NOLVP,NOWDT,NOPROTECT,NOSTVREN    // PIC18F8722
#use delay(clock=40000000)    // OSC
//#fuses HS,NOWDT,NOPROTECT,NOLVP  // PIC18F4550
//#use delay(clock=20000000)       // OSC
#use rs232(baud=9600, parity=N, bits=8, stop=1, xmit=PIN_D5, rcv=PIN_D4, timeout=5000, stream=COM1)

/******************************************************************** Include */
#include <stdio.h>   // Printf
#include <stdlib.h>  // Standard Library
#include <string.h>  // String Library
#include "input.c"

/*********************************************************** Declare Variable */
char strRF;

/************************************************************* Main Functions */
void main(void){
   fprintf(COM1,"\r\nStart");
   //strRF[5]='\0';
   while (TRUE){
      fprintf(COM1,"\r\nWait");
      strRF=fgetc(COM1);   // Waiting for 1 charactor, 5 s time out
      fprintf(COM1,"\r\n%c",strRF);
      fprintf(COM1,"\r\nNext");
      delay_ms(500);
   }
}
/*************************** End of $Workfile: $ ******************************/


Unfortunately, the command "Fgets" doesn't work in 18F8722. The tested code is following ...

Code:
/******************************************************************** Include */
//#include <18F4550.h>
#include <18F8722.h>  // PIC18F8722

/************************************************ Setting Configuration Fuses */
#fuses H4,NOLVP,NOWDT,NOPROTECT,NOSTVREN    // PIC18F8722
#use delay(clock=40000000)    // OSC
//#fuses HS,NOWDT,NOPROTECT,NOLVP  // PIC18F4550
//#use delay(clock=20000000)       // OSC
#use rs232(baud=9600, parity=N, bits=8, stop=1, xmit=PIN_D5, rcv=PIN_D4, timeout=5000, stream=COM1)

/******************************************************************** Include */
#include <stdio.h>   // Printf
#include <stdlib.h>  // Standard Library
#include <string.h>  // String Library
#include "input.c"

/*********************************************************** Declare Variable */
char strRF[100];

/************************************************************* Main Functions */
void main(void){
   fprintf(COM1,"\r\nStart");
   strRF[50]='\0';
   while (TRUE){
      fprintf(COM1,"\r\nWait");
      fgets(strRF,COM1);   // Waiting for 1 charactor, 5 s time out
      fprintf(COM1,"\r\n%s",strRF);
      fprintf(COM1,"\r\nNext");
      delay_ms(500);
   }
}
/*************************** End of $Workfile: $ ******************************/


How could i do with the lastest code (18F8722 & fgets) ? or Start the new one with the new command ... get_string() ?
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