View previous topic :: View next topic |
Author |
Message |
s.jaglowski
Joined: 14 Nov 2005 Posts: 14
|
Stream Selection |
Posted: Thu Sep 14, 2006 2:40 pm |
|
|
Hello.
I'm running a programme on an 18F8720. The programme receives input from a port - it doesn't care which as they will be mutually exclusive, but needs to output to the port it received the input from.
I know I can use fprintf(stream,.....); etc. But this means duplicating the fprintf for each response. i.e.
if(commsA)
fprintf(streamA, "output answer");
else
fprintf(streamB, "output answer");
I believe it may be possible to have code which selects the stream thus
if(commsA&&!commsB)
#use rs232(xmit=PIN_C6, rcv=PIN_C7)
;
if(commsB&&!commsA)
#use rs232(xmit=PIN_G1, rcv=PIN_G2,)
;
.
.
.
.
printf("output answer to correct stream");
Assuming it can be done, does anyone know of any problems that might follow from this approach?
Has anyone successfully used this approach or something similar?
Regards,
Steve |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Sep 14, 2006 3:22 pm |
|
|
Those are hardware USARTS. I would use streams.
Your second idea looks like your redefining the #use rs232.
But why risk losing data while switching?
I would use the first idea.
Set them once and use IRQ's and 2 sets of buffers.
TX1,RX1,TX2,RX2 buffers.
and have the duplicate printf. But your just moving data to the tx
buffers, so its more like a memcpy.(or like LCD_putc)
I don't think it will be as bad as you think. |
|
|
s.jaglowski
Joined: 14 Nov 2005 Posts: 14
|
|
Posted: Mon Sep 18, 2006 8:44 am |
|
|
I have tried my proposed approach and it seems to function well enough.
However, I think your caution is probably wise. If I can simplify the streamed approach (using a little more string pointer passing) and do it using streams I will.
Regards,
Steve Jaglowski |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Mon Sep 18, 2006 9:04 am |
|
|
treitmey probably has it right.
1) Use a printf(myfunction,"...");
2) In myfunction ( myfunction is called for each char in the formatted output printf produces so direct the passed chars to the output buffer desired.
Let the Tx interrupt isr transmit the char. Or just select the stream and
write the char out with printf(STREAM,"%c",c) fputc(c,STREAM) etc. |
|
|
Ttelmah Guest
|
|
Posted: Mon Sep 18, 2006 10:17 am |
|
|
I'd go slightly further.
'Encapsulate' the putc function, and have a global 'stream' selector.
So:
Code: |
my_putc(char c) {
if (COMMSA) fputc(c,STREAMA);
else fputc(c,STREAMB);
}
//Then to send data to a selected stream, just use:
printf(my_putc,"Sending data to currently selected stream/n/r");
|
The only duplication, is one fputc call, and at any point, for 'putc', just use 'my_putc', and for printf, use it as shown with the output routine.
Personally I would use serial buffers for both input and output as well.
Best Wishes |
|
|
s.jaglowski
Joined: 14 Nov 2005 Posts: 14
|
|
Posted: Mon Sep 18, 2006 12:51 pm |
|
|
Excellent proposals.
Thanks.
Regards,
Steve Jag |
|
|
|