fprintf with Teensy3.1 hangs

Status
Not open for further replies.

DD1965

Member
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
 
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.
 
I tried this too to connect the serial device to the USB UART

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 [U]fdev_setup_stream[/U] 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
}
 
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.
 
Thanks

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
 
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.
 
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;
}
}
 
Jay - this _write() printf and sprintf topic is discussed in other postings is discussed in other places here. Try a search.
 
Status
Not open for further replies.
Back
Top