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 CCS Technical Support

Can a stream ID be passed to a function?

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



Joined: 23 Apr 2007
Posts: 91
Location: Rochester, England

View user's profile Send private message Visit poster's website

Can a stream ID be passed to a function?
PostPosted: Thu Dec 18, 2014 5:50 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 18, 2014 6:46 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Dec 18, 2014 12:19 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Thu Dec 18, 2014 12:44 pm     Reply with quote

Thanks guys,

Thought I was being over optimistic

Cheers

James
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Thu Dec 18, 2014 2:53 pm     Reply with quote

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.
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