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

Question about kbhit() and fgetc(stream)

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



Joined: 03 Dec 2008
Posts: 45

View user's profile Send private message

Question about kbhit() and fgetc(stream)
PostPosted: Thu Apr 30, 2009 10:54 am     Reply with quote

I use pic18f458 and set 2 uart port
Code:
#use rs232(baud=2400,xmit=tx_1,rcv=rx_1,stream=g)
#use rs232(baud=9600,xmit=tx_2,rcv=rx_2,stream=d)

I want to use stream d to receive data
Code:

int timed_getc(long m,int n)
{
   
  long timeout=0;
  while(!kbhit(d)&&(++timeout<m)) //if m=100 n=10 wait untill 1ms
   {   
      delay_us(n);
   }

   if(kbhit(direct))
         return(fgetc(d));
      else
      {
     
       return(0);
      }
}

void main()
{   
   while(true)
   {
      rec=timed_getc(100,10);
   
           fprintf(direct,"%C\n\r",rec);
      
        }

}

The input stream is like
Quote:
AB-DEFG123.123 ASGDS123.12345crlf

I can not get right output.

I appreciate help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 30, 2009 12:37 pm     Reply with quote

Quote:
#use rs232(baud=2400,xmit=tx_1,rcv=rx_1,stream=g)
#use rs232(baud=9600,xmit=tx_2,rcv=rx_2,stream=d)

What pins are used for "tx_1", etc.

Post those two lines with the actual CCS pin number constants.
cchappyboy



Joined: 03 Dec 2008
Posts: 45

View user's profile Send private message

PostPosted: Thu Apr 30, 2009 6:22 pm     Reply with quote

#define tx_1 pin_c6
#define rx_1 pin_c7
#define tx_2 pin_c0
#define rx_2 pin_c1

In main()

indicate

int rec=0;
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri May 01, 2009 2:05 am     Reply with quote

Shouldn't this line

if(kbhit(direct))

be
if(kbhit(d))
?
Where is direct defined ?
cchappyboy



Joined: 03 Dec 2008
Posts: 45

View user's profile Send private message

PostPosted: Fri May 01, 2009 6:42 am     Reply with quote

yes, direct should be d.

it is just mistake when i post in here.

Thanks
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri May 01, 2009 6:50 am     Reply with quote

If there is a gap of 1sec in the data stream your function will return 0 which you still print out. This char may mess up your output so it looks like something is wrong when it isn't.

Try putting a check in

Code:

rec=timed_getc(100,10);
if (rec != 0)
  fprintf(direct,"%c\n\r",rec);


I noticed you are using an upper case C in the printf statement. Case does matter and the normal form displaying a char is a lowercase c. Not sure how CCS would handle this.
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