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

Use STREAM 'type' as function parameter

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



Joined: 30 Oct 2007
Posts: 566
Location: Ottawa, Ontario, Canada

View user's profile Send private message

Use STREAM 'type' as function parameter
PostPosted: Wed Sep 07, 2016 12:22 pm     Reply with quote

Compiler: 5.026
Device: PIC24HJ256GP206

Quick question...

I am using two UARTS on my PIC: UART1 talks to a serial modem. UART2 displays stuff to a puTTY serial console. The UART configuration is like this:

#use rs232( UART1, baud=115200, parity=N, bits=8, STREAM=MODEM_SERIAL, ERRORS )
#use rs232( UART2, baud=115200, parity=N, bits=8, STREAM=CONSOLE_SERIAL, ERRORS )

UART2 (puTTY console) is used for when I am locally connected to my unit using an FTDI USB-to-SERIAL cable.

UART1 (modem) sends and receives data through the modem's TCP channel when I am connected to the modem via a remote PC.

I am trying to do this:
Code:

   if( IPClient.IsConnected == TRUE )
   {
      unsigned int8 const Stream = MONITOR_SERIAL;
   }
   else
   {
      unsigned int8 const Stream = MODEM_SERIAL;
   }

   fprintf( Stream, "SYSTEM MENU:" );

and I am getting this error at the fprintf line: <unidentified identifier Stream>

So.... How can I pass a 'stream' to the fprintf function?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 07, 2016 1:27 pm     Reply with quote

Stream is not a system variable. You can't do it.
Ttelmah



Joined: 11 Mar 2010
Posts: 19554

View user's profile Send private message

PostPosted: Wed Sep 07, 2016 2:32 pm     Reply with quote

It's worth understanding in some ways what a stream 'is'. It's effectively a pre-processor command to say 'substitute the code for this I/O device into the command'. As such it can't be a variable, since it is changing the code that is actually compiled.

However you can cheat perfectly effectively for this:
Code:

#use rs232( UART1, baud=115200, parity=N, bits=8, STREAM=MODEM_SERIAL, ERRORS )
#use rs232( UART2, baud=115200, parity=N, bits=8, STREAM=CONSOLE_SERIAL, ERRORS )

#define MODEM 0
#define CONSOLE 1
int8 Target=MODEM;

void routed_putc(char chr)
{
   if (Target==MODEM)
      fputc(chr,MODEM_SERIAL);
   else
      fputc(chr,CONSOLE_SERIAL);
}


    //Then
    if (IPClient.IsConnected==TRUE)
       Target=CONSOLE;
    else
       Target=MODEM;

   //When you want to print
   printf(routed_putc, "SYSTEM MENU:" );


This way two actual putc routines are generated. One for each of the two targets, selected by the value stored in 'Target'. A single printf, is generated, that sends each character to the subroutine that decides which one to routes to. Obviously big saving in not requiring two sets of printf code.
In fact the 'routed_putc', could internally test the IPClient.IsConnected, so the routing becomes completely automatic, and without the need for another variable Very Happy
benoitstjean



Joined: 30 Oct 2007
Posts: 566
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Fri Sep 09, 2016 6:16 am     Reply with quote

That's what I thought also. Not possible. But using a flag based on if the TCP client is connected or not was also the other solution.

Thanks for all your help! It's appreciated, as always!

Ben
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