View previous topic :: View next topic |
Author |
Message |
JamesW
Joined: 23 Apr 2007 Posts: 91 Location: Rochester, England
|
Can a stream ID be passed to a function? |
Posted: Thu Dec 18, 2014 5:50 am |
|
|
Hi folks,
I'm writing an application which is going to have a local RS232 menu/diagnostic system on it, but also a remote one accessed by modem.
Ideally I'd like to write a single set of routines for it, that I pass the stream ID of the serial port to, which then automatically squirts data out the relevant port.
However I can't find any documentation on how to pass stream id's to functions.
something along the lines of
Code: |
void PrintSomething (FILE * StreamId)
{
fprintf(StreamId, "\r\n HELLO WORLD);
} |
Can anyone shed any light on how to do this please?
Cheers
James |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Dec 18, 2014 6:46 am |
|
|
You can't. That's why there's no documentation to say that you can.
"Streams" in CCS are not really streams, but just ID constants that differentiate between ports and devices at compile time only. They must be constants, they cannot be variables and they cannot be passed at runtime as parameters.
Even printf() and its relatives are not runtime functions, they are indications to the compiler to replace them by code that provides the relevant functionality. Indeed relatively few Cs actually have true runtime callable printf functions. The only one that I've ever encountered that actually implemented them as C functions (as opposed to assembler, or as CCS C as inline/called code) was a TI 320C6200 compiler.
So, instead you can assign some other ident, e.g numbers, and pass those with a switch in your "universal" IO routines. |
|
|
jgschmidt
Joined: 03 Dec 2008 Posts: 184 Location: Gresham, OR USA
|
|
Posted: Thu Dec 18, 2014 12:19 pm |
|
|
I've handled this in the past by preparing the data I want to output with sprintf() to a string. This handles all the complicated work in one place.
Then I have multiple fputs() functions to output the prepared data, one for each stream. An if or switch statement then decides which one to use.
Maybe not very pretty but it works, and that's what counts. _________________ Jürgen
www.jgscraft.com |
|
|
JamesW
Joined: 23 Apr 2007 Posts: 91 Location: Rochester, England
|
|
Posted: Thu Dec 18, 2014 12:44 pm |
|
|
Thanks guys,
Thought I was being over optimistic
Cheers
James |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Thu Dec 18, 2014 2:53 pm |
|
|
Have a look at the answer I posted only a couple of days ago, in the thread 'passing variables in kbhit'. It explains 'why', but also how you can work round it. |
|
|
|