View previous topic :: View next topic |
Author |
Message |
Lykos1986
Joined: 26 Nov 2005 Posts: 68
|
Bridging two RS232 ports… |
Posted: Sat Sep 20, 2008 9:51 am |
|
|
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
|
|
Posted: Sat Sep 20, 2008 11:50 am |
|
|
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
|
|
Posted: Sun Sep 21, 2008 2:47 am |
|
|
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! |
|
|
|