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 support@ccsinfo.com

KBHIT PROBLEM

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







KBHIT PROBLEM
PostPosted: Tue Apr 04, 2006 8:47 am     Reply with quote

I have this code for my RS-232 receiver. My transmitter program sends
( A,10| ) with no braket but when I use kbhit I receive only ( A, ). If I remove ( if kbhit()==1 ) I receive ( A,10| ). Can anyone help finding out why there is only ( A, ) in the buffer when I use kbhit?

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

if(kbhit()==1)
{
for(i=0;Lecture[i-1]!='|';i++)
{
Lecture = getc();
}
Lecture[i]='\0';
lcd_gotoxy(21,1);
printf(LCD_PUTC,"%s",Lecture);
}


Thanks.
mpfj



Joined: 09 Sep 2003
Posts: 95
Location: UK

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Apr 05, 2006 7:47 am     Reply with quote

The kbhit() function returns the following ...
Quote:
0 (or FALSE) if getc() will need to wait for a character to come in,
1 (or TRUE) if a character is ready for getc()

Note that it states *a* character, not *some* characters.

Therefore, you will need to perform a kbhit() check for each incoming character.
Ttelmah
Guest







PostPosted: Wed Apr 05, 2006 8:33 am     Reply with quote

Though true, that is not the problem. Once he is 'inside' the fetch loop, he stays there till the required character is detected for the end of string, and getc, will wait for each character in turn.
As I have posted to another thread, the problem is probably that the test fails on the first 'for' loop, when testing Lecture[0-1], which is outside the array, and could return anything. The addition of kbhit, probably results in some of the RAM containing different values, and 'shows up' this logic fault.

Best Wishes
mpfj



Joined: 09 Sep 2003
Posts: 95
Location: UK

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Apr 05, 2006 8:36 am     Reply with quote

Code:
for(i=0;Lecture[i-1]!='|';i++)

Nice one ... can't believe I didn't spot it !! Embarassed
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