|
|
View previous topic :: View next topic |
Author |
Message |
Darren Logan Guest
|
Is it possible to pass a stream to a function? |
Posted: Sun Feb 23, 2003 4:33 am |
|
|
Hello,
It is possible to pass a Stream identifier to a function?
It would save a lot of code space for me if this was possible.
Currently, I have to determine which serial port (i have 2) I need to send data out of thus (example):
void sendit(int whichport) {
if(whichport==1) {
fprintf(port1,"calling from port 1");
}
if(whichport==2) {
fprintf(port2,"calling from port 2");
}
}
what i'd really like to do is:
void sendit(int whichport) {
fprintf(whichport,"sending from port \%u",whichport);
}
What i've done doesn't work, im wondering whether a) I've done something wrong or b) You simply can't do it this way?
Answers on a postcard to "Save the ROM competition", FrustratesVille, UK
Cheers,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12031 |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: Is it possible to pass a stream to a function? |
Posted: Sun Feb 23, 2003 10:04 am |
|
|
You could you sprintf to put the data in a string and then write out to the port of choice
or
the printf command allows you to specify a function as the first parameter. The default is putc(), however, you could make your own function in which you specify the stream and then that function outputs accordingly.
:=Hello,
:=
:=It is possible to pass a Stream identifier to a function?
:=
:=It would save a lot of code space for me if this was possible.
:=
:=Currently, I have to determine which serial port (i have 2) I need to send data out of thus (example):
:=
:=void sendit(int whichport) {
:=
:=if(whichport==1) {
:=fprintf(port1,"calling from port 1");
:=}
:=
:=if(whichport==2) {
:=fprintf(port2,"calling from port 2");
:=}
:=
:=}
:=
:=what i'd really like to do is:
:=
:=void sendit(int whichport) {
:=
:=fprintf(whichport,"sending from port \%u",whichport);
:=
:=}
:=
:=What i've done doesn't work, im wondering whether a) I've done something wrong or b) You simply can't do it this way?
:=
:=Answers on a postcard to "Save the ROM competition", FrustratesVille, UK
:=
:=Cheers,
:=Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12034 |
|
|
Darren Logan Guest
|
Re: Is it possible to pass a stream to a function? |
Posted: Sun Feb 23, 2003 11:04 am |
|
|
Mark,
Thanks. I like the sound of 2nd idea (printf) but admit to be a little under-skilled in this area.
Could you post typical code please ????
Thanks,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12036 |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: Is it possible to pass a stream to a function? |
Posted: Sun Feb 23, 2003 2:04 pm |
|
|
void myputc(char somedata)
{
if(whichport==1)
{
fputc(port1,somedata);
}
else
{
fputc(port2,somedata);
}
}
void main
{
whichport = 1;
printf(myputc,"sending from port \%u",whichport);
while(1);
}
whichport is a global in this case.
Regards,
Mark
:=Mark,
:=
:=Thanks. I like the sound of 2nd idea (printf) but admit to be a little under-skilled in this area.
:=
:=Could you post typical code please ????
:=
:=Thanks,
:=Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12039 |
|
|
Darren Logan Guest
|
Re: Is it possible to pass a stream to a function? |
Posted: Mon Feb 24, 2003 6:59 am |
|
|
Thanks Marks, much appreciated. I'll give it a whirl.
This should save a fair bit of space as currently I've got duplicate fprintf's sending the same data only out different ports.
Cheers,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12051 |
|
|
|
|
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
|