passing variable argument to printf()

Discussion in 'Programmer's Corner' started by bug13, Jun 11, 2018.

  1. bug13

    Thread Starter Senior Member

    Feb 13, 2012
    1,507
    53
    Hi guys

    I have multiple UART in my pic18f65k40, and I want to wrap the printf() in another function so I can print to different UART. How do you guy usually do that? I am using XC8 compiler.

    Thanks for your help!

    Code (Text):
    1.  
    2. // XC8 function to determine the destination of the standard output stream
    3. void putch(char data){
    4.     switch(output_uart){
    5.         case DEBUG:
    6.           UART1Put(data);
    7.           break;
    8.         case COM:
    9.           UART2Put(data);
    10.           break;
    11.         case LCD:
    12.           UART3Put(data);
    13.           break;
    14.     }
    15. }
    16.  
    17. // print to debug output
    18. int debut_printf(const char *str, ...){
    19.   // set flag to direct output to debug uart
    20.   output_uart = DEBUG;
    21.    return printf(str, /* how do I pass ... to here */);
    22. }
    23.  
    24. // print to LCD
    25. int lcd_printf(const char *str, ...){
    26.   // set flag to direct output to LCD uart
    27.   output_uart = LCD;
    28.   ...
    29. }
    30.  
    31. // print to communication module.
    32. int com_printf(const char *str, ...){
    33.   // set flag to direct output to com. uart
    34.   output_uart = COM;
    35.   ...
    36. }
     
    Last edited: Jun 11, 2018
  2. bug13

    Thread Starter Senior Member

    Feb 13, 2012
    1,507
    53
    I have tried this, but it doesn't work.

    Compiler warning:
    Code (Text):
    1. int debug_printf(const char *str, ...){
    2.     int result;
    3.     va_list arg;
    4.    
    5.     va_start(arg, str);
    6.     result = vprintf(str, arg);
    7.     va_end(arg);
    8.     return result;
    9. }
     
  3. WBahn

    Moderator

    Mar 31, 2012
    23,093
    6,946
    While a variadic function can take a variable number of arguments, the number must be known at compile time for each call so that the compiler can set up, and later clean up, the call stack.
     
  4. xox

    Member

    Sep 8, 2017
    307
    63
    You need a vprintf "polyfill" like this oldy-but-goody.
     
  5. Picbuster

    Active Member

    Dec 2, 2013
    813
    101
    You do have more then one uart port.

    one way of doing it is to modify the putchar() which is used by printf().
    in this case when Portnum=0 then data goes to port 1 for all other values to port2.
    like
    void putch(unsigned char byte)
    {
    if ( Port_Num)

    {
    while(!TX1IF)
    continue;
    TXREG1 = byte;
    }
    else
    {
    while(!TX2IF)
    continue;
    TXREG2 = byte;
    }

    You have to modify it for your application use the switch statement to select different ports.

    disadvantage you can not modify putch hence it is used by printf()


    But it works

    Picbuster
     
    bug13 likes this.
Loading...