| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| cchappyboy 
 
 
 Joined: 03 Dec 2008
 Posts: 45
 
 
 
			    
 
 | 
			
				| Question about kbhit() and fgetc(stream) |  
				|  Posted: Thu Apr 30, 2009 10:54 am |   |  
				| 
 |  
				| 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
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Apr 30, 2009 12:37 pm |   |  
				| 
 |  
				|  	  | 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
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Apr 30, 2009 6:22 pm |   |  
				| 
 |  
				| #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
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri May 01, 2009 2:05 am |   |  
				| 
 |  
				| Shouldn't this line 
 if(kbhit(direct))
 
 be
 if(kbhit(d))
 ?
 Where is direct defined ?
 |  | 
	
		|  | 
	
		| cchappyboy 
 
 
 Joined: 03 Dec 2008
 Posts: 45
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri May 01, 2009 6:42 am |   |  
				| 
 |  
				| yes, direct should be d. 
 it is just mistake when i post in here.
 
 Thanks
 |  | 
	
		|  | 
	
		| Wayne_ 
 
 
 Joined: 10 Oct 2007
 Posts: 681
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri May 01, 2009 6:50 am |   |  
				| 
 |  
				| 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.
 |  | 
	
		|  | 
	
		|  |