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

Bridging two RS232 ports…

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



Joined: 26 Nov 2005
Posts: 68

View user's profile Send private message

Bridging two RS232 ports…
PostPosted: Sat Sep 20, 2008 9:51 am     Reply with quote

Hi! I have got two serial ports in my PIC. The first one is:
Code:
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7,stream=Module)

And the second one is:
Code:
#use rs232(baud=9600, xmit=PIN_C0,rcv=PIN_C1,stream=PC)


Now I want to connect (with software) the two serial ports together. For example, when I am sending command from my PC using the one serial port (stream=PC) then the data I want to be transferred to the other serial port (stream=module) and vice versa. How can I do that?

I was trying to make something like that:
Code:

   while(1)
   {
   i=fgetc(PC);
   fprintf(module,i);
   i=fgetc(module);
   fprintf(PC,i);
   }

but without success
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Sat Sep 20, 2008 11:50 am     Reply with quote

fgetc() waits forever if there's nothing received, you should use kbhit() to test if the port has received something. Instead of fprintf, use fputc which sends one character only.
Try something like this
Code:
while(1)
 {
   if (kbhit(PC))
   {
     i=fgetc(PC);
     fputc(i,module);
   }
   
   if (kbhit(module))
   {   
     i=fgetc(module);
     fputc(i, PC);
   }
 }

Caveat: If one of your ports is a software UART make sure that its speed is set fast enough that the loop does not spend too much time servicing it (both during receive and transmit) to miss data on the hardware port. I suggest the software port to be set minimum 4 times faster than hw one.

Note: If you want to be sure not to miss any data on the software port and still have some remaining processing power for other things I suggest another approach: use some port capable of interrupt-on-edge (like B0) and write the UART receive function as an interrupt service.

What is your application ? Do you need to process the data or modify it on the fly ?
Lykos1986



Joined: 26 Nov 2005
Posts: 68

View user's profile Send private message

PostPosted: Sun Sep 21, 2008 2:47 am     Reply with quote

Well I build my code using your approach and it seems that it works fine! I have to say Thank You!!!

The funny think is that I was knew the command kbhit(PC) and that the fgetc() waits forever but I was thinking that the best solutions is only the fgetc(). And that because I will not have simultaneously transmissions... and the first transmission will be only from the PC.

Thanks again!
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