Go Down

Topic: printf - bug (missing output device?) or "feature" to promote String class? (Read 3180 times) previous topic - next topic

Vaclav


How come the following compiles without errors or warnings  and does not visibly execute printf function?
Is it a push to promote knowing bloated String class or Arduino not knowing what stdio output is?
If stdio is too simple for Arduino developers it should it at least fail to compile.
This is not good  software engineering by any measure.

Time to read "Inmates are running the asylum", again.
Or just fix it.


Code: [Select]


#include <stdio.h>
void setup()
{
  Serial.begin (115200); // 9600); //
  Serial.println("\nprintf test"); 
}

void loop()
{
  unsigned int address=0xABCD;
  printf("\nAddress=%u, (%Xh)",address,address);
}



Peter_n

I think you have a complaint  :smiley-roll:
Where would you want to make that message visible ? On a screen perhaps ? Which screen ?
I don't know where the data is going, I didn't look into the library.
On the computer the stdio is opened when the program starts running, but with the Arduino it is not even opened, so using it makes no sense.

johnwasser

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html

"Due to the nature of the underlying hardware, only a limited subset of standard IO is implemented. There is no actual file implementation available, so only device IO can be performed. Since there's no operating system, the application needs to provide enough details about their devices in order to make them usable by the standard IO facilities."

"No assignments to the standard streams will be performed by fdev_setup_stream(). If standard streams are to be used, these need to be assigned by the user."
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

Vaclav

Actually after small search found the solution here. I also realized that there is no "standard" output for the processor itself.
Still feel the Arduino compiler could do better job. After all it is cobbled from various resources, so why not adding another piece of code?
SOLVED
Case closed

bperrybap


Actually after small search found the solution here. I also realized that there is no "standard" output for the processor itself.
Still feel the Arduino compiler could do better job. After all it is cobbled from various resources, so why not adding another piece of code?
SOLVED
Case closed


There is no such thing as "the Arduino compiler".
You seem to be confusing the functionality of the IDE, vs the Arduino libraries vs the avr-gcc compiler
vs AVR libC.

What you are talking about here is a combination of interrelated capabilities and runtime startup responsibilities.
AVR libC provides a mechanism to redirect printf() output to an output routine.
The Arduino startup code and core libraries are not initializing the low level AVR libC library routines
to use  any sort of i/o for printf() so the printf() does exactly what it should: drops it on the floor since
it does not know who to call to output the characters.

The Arduino startup code could have initialized the AVR libC to re-direct
printf() to the default Serial class device.
But it doesn't.

For whatever reason, the Arduino team has not really ever embraced xxprintf() support.
It essentially re-invented the world with the Print class.


--- bill




AWOL

Quote
For whatever reason, the Arduino team has not really ever embraced xxprintf() support.

Probably because of the precious RAM it can gobble, and you'd really mostly want to use the printf_P implementation.

robtillaart

don't recall the original source...
Code: [Select]

int my_putc( char c, FILE *t)
{
  Serial.write( c );
}

unsigned long cnt=0;
float f = 3.4e5;

void setup()
{
  Serial.begin(115200);
  Serial.println("START");
  // change STDOUT
  fdevopen( &my_putc, 0);
}

void loop()
{
  printf("%d Hello World!\n", cnt++);

  unsigned int address=0xABCD;
  printf("\nAddress=%u, (%Xh)\n",address,address);
  delay(3142);
}


it doesn't do floats ....
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

AWOL

Code: [Select]
int my_putc( char c, FILE *t)
{
  return Serial.write( c );
}

{ cough }

Also
Code: [Select]
printf("\nAddress=%u, (%#X)\n",address,address);

bperrybap

There are many ways to work around the lack of printf() support in Arduino.
You can even use some clever macros to create functions that will automagically use the
AVR progmem kludge to avoid extra RAM use.
Or you can add printf() support to the Print class, which is what I prefer since it allows using printf
formatting on any device that uses the Print class.
i.e. Serial.printf(), lcd.printf(), etc...

I added details to the Arduino Printf page for how to get xxprintf() support with Arduino
and how to modify your Print class for this.
http://playground.arduino.cc/Main/Printf

When you modify your Print class for printf() it will also automagically support the F() macro
so printf() format strings will not use up extra RAM on the AVR processor.


Attached is another example of what can be done with macros, if you don't want to modify
your Print class.

--- bill

liuzengqiang

Learn to use sprintf and sscanf. Chances are that you are sending debug info to serial port. Then you should also send them to sd card to log them or an lcd panel (such as phi-panel) to display in case you don't have computers around. You will be better off sprint to a buffer and then send to all output devices you want to use.
SDI-12 USB Adapter, custom designs
Routinely doing USB host and device stuff
http://liudr.wordpress.com
https://www.tindie.com/stores/liudr/

Go Up