View previous topic :: View next topic |
Author |
Message |
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
stream as function argument |
Posted: Thu Jun 28, 2007 4:01 pm |
|
|
Does CCS permit to pass a stream (or a pointer to a stream) to a function as an argument? Like this for example
Code: |
#use rs232(..., stream=one232)
#use rs232(..., stream=two232)
int8
check_send(stream m, // stream to send to
char* s) // null terminated string to send
{
fprintf(m, s);
return check_sum(s);
}
// calling code
void main()
{
//...
iChkSum = check_send(one232, "hello");
//...
iChkSum = check_send(two232, "world");
//...
}
|
Unfortunately, I couldn't make it work? _________________ Read the label, before opening a can of worms. |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: stream as function argument |
Posted: Thu Jun 28, 2007 10:17 pm |
|
|
kender wrote: | Does CCS permit to pass a stream (or a pointer to a stream) to a function as an argument? Like this for example
Code: |
#use rs232(..., stream=one232)
#use rs232(..., stream=two232)
int8
check_send(stream m, // stream to send to
char* s) // null terminated string to send
{
fprintf(m, s);
return check_sum(s);
}
// calling code
void main()
{
//...
iChkSum = check_send(one232, "hello");
//...
iChkSum = check_send(two232, "world");
//...
}
|
Unfortunately, I couldn't make it work? |
I think you could probably write your own using a pointer to the hardware shift buffers. CCS does not support this because it can not work with a software serial port. At the end of the day I think it would be better (easier) to just pass a bit in the function call to switch between ports. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
Re: stream as function argument |
Posted: Thu Jun 28, 2007 10:21 pm |
|
|
Neutone wrote: | CCS does not support this because it can not work with a software serial port. |
But somehow fputc() and fprintf() take a stream as an argument. _________________ Read the label, before opening a can of worms. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 28, 2007 10:54 pm |
|
|
Quote: |
But somehow fputc() and fprintf() take a stream as an argument.
|
They do, but it's only used at compile-time. If you look at the compiled
code in the .LST file, there is no code inserted which checks a variable
to see which stream to use. The decision is made entirely at compile-
time and fputc() is hard-coded to use the selected stream at that time. |
|
|
|