Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 14 of 14

Thread: fprintf with Teensy3.1 hangs

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    11

    fprintf with Teensy3.1 hangs

    Hi,
    I am wondering if there is something special that I need to do to get fprintf to work on the Teensy3.1. If I use fprintf anywhere in the program in hangs the program. e.g. fprintf(stderr, "Test"); Do I need to redirect the stderr to the Teensy Serial port? The code base I am using was ported from an Arduino Due and it worked there. Any thoughts most welcome.
    Cheers /DD

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,609
    I can't test this right now, but have you tried fprintf(stdout, "Test"); ?

    Pete

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,123
    Use Serial.printf("Test\n");

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Hi Pete,
    if I change it to sdtout, it does not hang, but it does not print either. I was expecting that it would default to Serial device as it does for the Arduino Due. Cheers David.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Hi Paul,
    I can do this but it means I have to change other peoples ported C code, Cheers David D.

  6. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    11

    I tried this too to connect the serial device to the USB UART

    Quote Originally Posted by DD1965 View Post
    Hi,
    I am wondering if there is something special that I need to do to get fprintf to work on the Teensy3.1. If I use fprintf anywhere in the program in hangs the program. e.g. fprintf(stderr, "Test"); Do I need to redirect the stderr to the Teensy Serial port? The code base I am using was ported from an Arduino Due and it worked there. Any thoughts most welcome.
    Cheers /DD
    I tried using this example from the Arduino site and this will compile with the Teensy2.0 but not with Teensy 3.0 or 3.1. This attaches the file output to the serial device. While the hardware is quite different not sure why it cant find the [FONT=Arial fdev_setup_stream function, Cheers /DD

    // we need fundamental FILE definitions and printf declarations
    #include <stdio.h>


    // create a FILE structure to reference our UART output function

    static FILE uartout = {0} ;

    // create a output function
    // This works because Serial.write, although of
    // type virtual, already exists.
    static int uart_putchar (char c, FILE *stream)
    {
    Serial.write(c) ;
    return 0 ;
    }

    void setup(void)
    {
    // Start the UART
    Serial.begin(9600) ;

    // fill in the UART file descriptor with pointer to writer.
    fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);

    // The uart is the standard output device STDOUT.
    stdout = &uartout ;
    }

    void loop(void)
    {
    float seconds ;

    // wait 1000 milliseconds
    delay(1000) ;

    // calculate seconds as a floating point value
    seconds = (float) millis() /1000.0 ;

    // report seconds since starting
    printf("Alive %.3f sec", seconds ) ;

    /*
    // without printf(), you would do this:
    Serial.print("Alive ") ;
    Serial.print(seconds,3) ;
    Serial.print("sec") ;
    */

    #if 0
    // you can explicitly use a FILE structure like this:
    fprintf( &uartout, "Alive %.3f sec", seconds ) ;
    #endif
    }

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,123
    Of course that doesn't work on Teensy 3.1. The fdev_setup_stream stuff is a non-standard feature of the avr-libc library.

    Serial.printf() works on both Teensy 2.0 and 3.1. Best to use that.

    If you want other printf stuff to work, you'll have to change the _write() function. Look in Print.cpp. It's a weak function, so you can just create your own copy and load it up with whatever code you like.

  8. #8
    Junior Member
    Join Date
    Dec 2013
    Posts
    11

    Thanks

    Quote Originally Posted by PaulStoffregen View Post
    Of course that doesn't work on Teensy 3.1. The fdev_setup_stream stuff is a non-standard feature of the avr-libc library.

    Serial.printf() works on both Teensy 2.0 and 3.1. Best to use that.

    If you want other printf stuff to work, you'll have to change the _write() function. Look in Print.cpp. It's a weak function, so you can just create your own copy and load it up with whatever code you like.
    Shall do, Thank-you, Cheers /DD

  9. #9
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    what about sprintf()?

  10. #10
    Junior Member
    Join Date
    Dec 2013
    Posts
    11

    Sprintf

    Quote Originally Posted by stevech View Post
    what about sprintf()?
    Seems to work here without any problems, cheers David

  11. #11
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    good... so sprintf() is an alternative.
    char buffer1[128];
    sprintf(buffer1, "my format %d %s", 1234, "some text");
    Serial.print(buffer1);

    or better, using snprintf() to prevent buffer overrun.

  12. #12
    Junior Member
    Join Date
    Feb 2014
    Location
    San Jose, CA
    Posts
    1
    For those interested in using printf() here's a replacement for _write() that directs output to the Serial object. Drop it in your sketch file (along with a call to Serial.begin() in your setup function), and code that calls printf() and other standard I/O functions will output to the serial monitor.

    Code:
    extern "C" { 
    __attribute__((weak))
    int _write(int file, char *ptr, int len)
    {
      // STDIN: do nothing
      if (file == 0)
        ; 
        
      // STDOUT/STDERR: Output to Serial
      else if (file == 1 || file == 2)
        Serial.write((uint8_t *)ptr, len);
    
      // Otherwise assume file is actually a pointer to a Print object and output to that.
      else
        ((class Print *)file)->write((uint8_t *)ptr, len);
    
      return 0;
    }
    }

  13. #13
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    Jay - this _write() printf and sprintf topic is discussed in other postings is discussed in other places here. Try a search.

  14. #14
    Quote Originally Posted by Jay View Post
    For those interested in using printf() here's a replacement for _write() that directs output to the Serial object.
    Still works great, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •