View previous topic :: View next topic |
Author |
Message |
Zulander Guest
|
kbhit problem |
Posted: Mon Nov 13, 2006 5:50 pm |
|
|
Hi, i have a problem with kbhit(), for some reason my function get stuck in a
loop when kbhit becomes true and it is not exiting anyone has a suggestion ?
thank you
while(1)
{
printf("NO HIT");
if (kbhit()==1) {
printf("HIT");
//call function when hit
rxtx();
} |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 13, 2006 5:58 pm |
|
|
1. Call the getc() function immediately after kbhit() == 1.
2. Also add the ERRORS directive to your #use rs232() statement. |
|
![](templates/subSilver/images/spacer.gif) |
Zulander Guest
|
gets ? |
Posted: Mon Nov 13, 2006 7:58 pm |
|
|
Hi
Thank you for the quick response, can i use gets or do i have to use getc ? thank you |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 13, 2006 8:28 pm |
|
|
If you expect more than one character and if the user will end the
string by pressing the Enter key, then you can use gets().
However, gets() is not really safe to use, because the user could type
in too many characters and write beyond the end of your buffer array.
It's better to use the get_string() function. It allows you to specify
the maximum number of characters that you will accept. See the
CCS example file, EX_STR.C, which shows how to use get_string().
It's in this folder:
c:\program files\picc\examples\ex_str.c |
|
![](templates/subSilver/images/spacer.gif) |
Zulander Guest
|
gets problem |
Posted: Mon Nov 13, 2006 9:16 pm |
|
|
well i will know exactly what a user will enter..
i have been working on this program for a long time now, since i am a new to C it's hard to isolate the problem.
do i have to break to exit the kbhit ? because when the user sends a <CR> it is printing the mystring but it does
not exit the if. shouldn't in theory the gets (part of getc) clear the buffer to null and trun kbhit == 0 ??
char mystring[30];
while(1)
{
printf("NO HIT");
if (kbhit()==1) {
gets(mystring);
printf("%s",mystring);
} |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 13, 2006 9:41 pm |
|
|
Quote: | well i will know exactly what a user will enter. |
You may know what they will enter, but all they have to do is type in
more characters than your array can hold and your program will crash.
What if the user types this:
Quote: |
aklsdfjaoiodru-03924812984218fasnvzxmfoasuhf9qryhqowrjhowqeh |
What will happen to any variables that are allocated after your buffer ?
Code: |
char buffer[30];
int8 result;
int i;
char message[10];
|
Quote: |
Because when the user sends a <CR> it is printing the mystring
but it does it is printing the mystring but it does not exit the if. |
It's probably re-entering the if() statement. You haven't shown all the
code in your while() loop, so it's difficult to know what your program is
doing. |
|
![](templates/subSilver/images/spacer.gif) |
Zulander Guest
|
gets problem |
Posted: Mon Nov 13, 2006 9:57 pm |
|
|
The reason that I will know what user are entering is very simple. Consider the fallowing diagram:
[User] <-------------> [GUI application in vb.net)] <-------------> [my PIC CONTROLLER]
All the data is control thought vb.net which is static and predefined. Which means that ones the software is done it will not be cannot be changed by the user.
Everything work’s fine but when I send a msg that’s were it doesn’t leave the if
p.S: thank you for taking your time and helping me ...
Code: |
VOID main()
{
int read_temp ();
long int_ds1721 ();
void rxtx();
char super[51];
setup_spi (FALSE);
set_adc_channel (2);
setup_timer_0 (RTCC_intERNAL|RTCC_DIV_1);
setup_timer_1 (T1_DISABLED);
setup_timer_2 (T2_DISABLED, 0, 1);
setup_comparator (NC_NC_NC_NC);
setup_oscillator (OSC_4MHZ);
printf("start:PIC_READY_START\r\n");
//initialize IC
printf("msg:Starting\r\n");
//temp1 = int_ds1721 (Sensor1_Add);
printf("msg:Initializing Sensor1\r\n");
//temp2 = int_ds1721 (Sensor2_Add);
printf ("msg:Initializing Sensor2\r\n");
//temp3 = int_ds1721 (Sensor3_Add);
printf ("msg:Initializing Sensor3\r\n");
//temp4 = int_ds1721 (Sensor3_Add);
printf ("msg:Initializing Sensor4\r\n");
//temp5 = int_ds1721 (Sensor5_Add);
printf ("msg:Initializing Sensor5\r\n");
SendSetting();
while(1)
{
tcounter = tcounter+1;
//Affiche toute les températures.
if (tcounter==Set_ms_delay){
//temp1 = read_temp(Sensor1_Add);
//temp2 = read_temp(Sensor2_Add);
//temp3 = read_temp(Sensor3_Add);
//temp4 = read_temp(Sensor4_Add);
//temp5 = read_temp(Sensor5_Add);
printf ("Temp: %s|%s|%s|%s|%s| \r\n",temp1,temp2,temp3,temp4,temp5);
tcounter=0;
}
delay_ms (1);
//Quand Rohan appuis sur une touche!
if (kbhit()==1) {
gets(super);
printf("msg: %s",super);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Zulander Guest
|
gets problem |
Posted: Mon Nov 13, 2006 10:23 pm |
|
|
i think i've sloved the problem... |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 13, 2006 10:34 pm |
|
|
My guess is that your VB program is sending an extra character after
the string and the carriage return. It's probably sending a Linefeed
character.
The CCS manual says that gets() waits for a carriage return to
determine the end of the string. From the manual:
Quote: |
Gets() --
Reads characters (using GETC()) into the string until a
RETURN (value 13) is encountered. The string is terminated
with a 0. Note that INPUT.C has a more versatile GET_STRING
function.
|
What if VB is sending "Hello World" 0x0D 0x0A ?
Then kbhit() will be true, and the program will "hang" in gets().
This could explain your problem. |
|
![](templates/subSilver/images/spacer.gif) |
|