serial.print and printf

Personally, I prefer printf (or sprintf) because I can see exactly what I am getting.

If I do something like this:

printf("Sensor %d reads %5.2f degrees C\n", x, sensor[x]);

It's crystal clear. What could be easier?

1 Like

Hi,
I have a post for adding printf to Serial class, for which I've been given a couple work around's but nothing is as comprehensive or straight forward to use as printf.

Bill you've made a reply to this post and attached a processing file. I've tried to use this w/o success. Are there other libraries I need to add? Missing File and others. I'm not sure how you'e intended the use of serialprintf.pde
Thanks
Keith

krupski:
Make yourself these two files and #include them in your sketch:

stdinout.cpp

#if ARDUINO >= 100

#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <stdio.h>
#include <stdinout.h>

// Function that printf and related will use to print
static int serial_putchar(char c, FILE *f)
{
       if(c == '\n') {
               serial_putchar('\r', f);
       }

return Serial.write(c) == 1 ? 0 : 1;
}
// Function that scanf and related will use to read
static int serial_getchar(FILE *)
{
       // Wait until character is avilable
       while(Serial.available() <= 0) { ; }

return Serial.read();
}

static FILE serial_stdinout;

static void setup_stdin_stdout()
{
       // Set up stdout and stdin
       fdev_setup_stream(&serial_stdinout, serial_putchar, serial_getchar, _FDEV_SETUP_RW);
       stdout = &serial_stdinout;
       stdin  = &serial_stdinout;
       stderr = &serial_stdinout;
}

// Initialize the static variable to 0
size_t initializeSTDINOUT::initnum = 0;

// Constructor that calls the function to set up stdin and stdout
initializeSTDINOUT::initializeSTDINOUT()
{
       if(initnum++ == 0) {
               setup_stdin_stdout();
       }
}




**stdinout.h**


#ifndef _STDINOUT_H
#define _STDINOUT_H

// no need to make an instance of this yourself
class initializeSTDINOUT
{
       static size_t initnum;
public:
       // Constructor
       initializeSTDINOUT();
};

// Call the constructor in each compiled file this header is included in
// static means the names won't collide
static initializeSTDINOUT initializeSTDINOUT_obj;

#endif




This gives you stdin, stdout and stderr.

So, now "printf" and "fprintf(stdout, ..." will work, as will getc, putc, gets, puts, etc...

Be aware though that the %f format will not work because the Arduino IDE doesn't include floating point support to save sketch size. If you want to add the ability to enable or disable floating point support (via a checkbox in File / Preferences), grab this file: https://github.com/krupski/arduino-1.0.3/blob/master/app/pde.jar (click on "view raw" to download it), then REPLACE your arduino-1.0.x/lib/pde.jar with the downloaded version. Be sure to make a backup copy of your original pde.jar file, just in case!

Note that enabling floating point support will make your sketch about 1.5K larger, so leave it disabled unless you need it.

Hope this helps!

It works, thank you!