'class Uart' has no member named 'printf'

Hello,
I use the Arduino DUE board. There is wrong with Serial.printf in my code. How can I get the code run? here is the code.

        Serial.println(F("Message received:"));
        Serial.printf("Payload length:  %d\n", downlinkMessage.payloadLength);
        Serial.printf("Addressee ID:    0x%07X\n", downlinkMessage.addresseeIdentification);
        Serial.printf("ADCS:            0x%X\n", downlinkMessage.ADCS);
        Serial.printf("Service:         0x%02X\n", downlinkMessage.service);
        Serial.printf("FCS:             0x%04X\n", downlinkMessage.FCS);
        Serial.print(F("Payload buffer:  0x"));

image

Arduino using print or println
there is no printf
but there is sprintf

printf can be very easily added to most/all Arduino boards:

Arduino Playground - Printf

char printBuffer[40];
Serial.println(F("Message received:"));
snprintf(printBuffer, sizeof printBuffer, "Payload length:  %d\n", downlinkMessage.payloadLength);
Serial.println(printBuffer);
snprintf(printBuffer, sizeof printBuffer, "Addressee ID:    0x%07X\n", downlinkMessage.addresseeIdentification);
Serial.println(printBuffer);
snprintf(printBuffer, sizeof printBuffer, "ADCS:            0x%X\n", downlinkMessage.ADCS);
Serial.println(printBuffer);
snprintf(printBuffer, sizeof printBuffer, "Service:         0x%02X\n", downlinkMessage.service);
Serial.println(printBuffer);
snprintf(printBuffer, sizeof printBuffer, "FCS:             0x%04X\n", downlinkMessage.FCS);
Serial.println(printBuffer);
Serial.print(F("Payload buffer:  0x"));

Serial.printf() is supported by ESP and Teensy (the ARM-based ones) boards. But, apparently not the Due.

It would not be compatible with other Arduino boards like AVR based boards. But I know that you know that :wink:

Related discussion:

It is unfortunate that there has been divergence in the core APIs of the 3rd party boards platforms at such a fundamental level. It harms the portability that is so important in the Arduino project.

Ideally, the community would let Arduino set the standards for the API, making changes via proposals such as the one I linked above rather than unilateral action. But of course Arduino has not always held up their end of that arrangement by making a conclusive decision on such proposals in a timely manner and the nature of open source is that each 3rd party project is managed according to the whims of its maintainer.

1 Like

If I'm using an ESP or ARM board, it's likely because I need features or capabilities beyond that of a basic ARV-based Arduino. So, in that case, portability is not a factor.

However, I was surprised to see that Arduino's ARM-based boards don't support printf() while those from PJRC and Adafruit do.

"portability" and "works on AVR boards" are two different things.

But even that is not relevant. The MCUdude and DrAzzy/SpenceKonde AVR cores have printf:

My understanding is that Arduino's concern about printf is that its use results in beginner unfriendly code. A series of multiple print calls is very intuitive, while the equivalent string sprinkled with cryptic printf format specifiers is not.

But they do, with exactly the same minor change that is required to enable it for the AVR board. Enabling printf in Due takes about 2 minutes...

Very well, I'll rephrase it .... printf() not supported out of the box. Better?

it will take longer than that just to get a link from you explaining how?

I already posted the link, in post #3.

All that's required is doing a cut/paste of about a page of code from the link into print.h for the architecture in question.

You can also take a look at the diff from MCUdude's proposal:

my StreamLib has printf in BufferedPrint and co. (btw the printf implementation doesn't need a buffer)
https://github.com/JAndrassy/StreamLib#formatted-printing

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.