How Do I Redirect Stdout?

There are two ways to send data out the serial ports:

  1. Redirect stdout and use printf( ) or iprintf( )
  2. Use a file descriptor and write( )

Method 1, Redirect stdout and use printf( ) and iprintf( )

Redirecting Stdout:
-------------------

#define DEBUG_SERIAL_PORT  (1)
#define DEBUG_BAUDRATE     (115200)
#define DEBUG_STOP_BITS    (1)
#define DEBUG_DATA_BITS    (8)
   
int fdserial_debug=OpenSerial(DEBUG_SERIAL_PORT,
				DEBUG_BAUDRATE,
				DEBUG_STOP_BITS,
				DEBUG_DATA_BITS,
				eParityNone);

ReplaceStdio(1,fdserial_debug);  // 0=stdin, 1=stdout, 2=stderr
printf("No we're using printf()!\r\n");

NOTE: THIS REDIRECTS ALL printf() CALLS

 

Method 2, File descriptors and write( )

Using a file descriptor:
------------------------

#define DEBUG_SERIAL_PORT  (1)
#define DEBUG_BAUDRATE     (115200)
#define DEBUG_STOP_BITS    (1)
#define DEBUG_DATA_BITS    (8)
   
int fdserial_debug = OpenSerial(DEBUG_SERIAL_PORT,
                           DEBUG_BAUDRATE,
                           DEBUG_STOP_BITS,
                           DEBUG_DATA_BITS,
                           eParityNone);
char buf[80];
sprintf(buf, "Data sent to com2\n");
write(fdserial_debug, buf, strlen(buf));