Arm Mbed OS support forum

No member named 'printf' in 'mbed:BufferSerial'

Hi there.
I am pretty new to Mbed Online Compiler, so far I have been using it successfully with a LPC1768 board as a target. Recently I experienced some problems with my firmware, so I wanted to debug it in Mbed Studio. Exporting my project from the Online IDE and importing it in Mbed Studio, I realized that I was not using the right version of mbed-os, so I updated it to the latest version.

Before, I was using a serial communication defined as

Serial pc(USBTX, USBRX);

pc.printf((“starting the program \n\r”);

but with the newest version of mbed-os I changed this to BufferSerial (as I read elsewhere in the forum). So now my code looks like:

BufferSerial pc(USBTX, USBRX);

pc.printf((“starting the program \n\r”);

Well, now when I try to compile my project in the IDE I get this error
“Error: No member named ‘printf’ in ‘mbed::BufferedSerial’ in “main.cpp”, Line: 56, Col: 12”.

I am pretty sure that the documentation for BufferedSerial class lists also a printf function, and that I am using it correclty, but apparently there are problems with what I am doing. I tried to downgrade the mbed-os to a previous version, such as MbedOs 6.2.1 as mentioned in other posts (like this one) but the problem persits.

Can you tell me where the problem is?

Thank you very much,
Italo

Hello,

in the past the printf came from Stream

In MbedOS 6+ the Stream is not used

So, that is right behavior, but exist some workaround.

SerialStream - Bringing MBed serial ports back like it’s 1999…… | Mbed
Hitchhiker’s Guide to Printf in Mbed 6 - Mbed OS - Arm Mbed OS support forum

BR, Jan

Hello Italo,

As explained by Jan the BufferedSerial class does not have a printf member function in Mbed 6.11 anymore. However, read here how to send debugging or other serial messages to the PC connected to the Mbed board over a USB cable using the global (no object, like pc.printf, is used) printf or debug functions. The advantage of debug over the printf function is that any call to it is automatically optimized away (removed) if you compile your program using the release profile.

The global printf and debug functions use Mbed’s console (stdio) for output and you can change the bit rate as follow:

  • Ether add the following function into your main.cpp:
FileHandle* mbed::mbed_override_console(int)
{
    static BufferedSerial   myConsole(CONSOLE_TX, CONSOLE_RX, 115200);
    return &myConsole;
}

NOTE: This method allows you to change the serial port used for the stdio (console) too.

  • or add the following to your mbed_app.json file:
{
       "target_overrides": {
           "*": {
            "platform.stdio-baud-rate": 115200
        }
    }
}

Hello,
thank you very much for your answers, and for pointing me to the right documentation. I tried what you suggested and indeed using just printf() alone works just fine.
Anyway, I was not able to run the example that @JohnnyK was pointing at. If I do something like:

BufferedSerial serial(USBTX, USBRX);
SerialStream<BufferedSerial> pc(serial);

...
pc.printf("Hello world!\n");
...

I get the error " Error: No template named ‘SerialStream’ in “main.cpp”, Line: 21, Col: 1"
Why is it not working?

Thank you very much,
Italo

For that you need to import and also include SerialStream.h, it is one file library.
So just copy the link and import it into the project.

BR, Jan

Thank you!

Hi,
sorry for bothering again… Today I was trying to use the function BufferedSerial::write() to write a float value (coming from the reading of the status of a pin on my LPC1768 board). I was not able to do it, so I tried to resort to the global printf("%f", pin_read), but this did not work as well. Eventually, I realize that I am not able to print anything with printf now, even chars or strings are ignored and the program just continues its flow as if the instruction was not there…

I remember that simple printf used to work (at least for strings), but I don’t know why this is not true anymore.
Do you have any hint on the reason?

Best,
Italo

Hello,

floats are disabled in default configuration.
For better understanding and activate it, please see - mbed-os/README.md at master · ARMmbed/mbed-os (github.com)

BR, Jan

Hello @JohnnyK,
thank you and sorry for the trivial question (right after posting I started to suspect that). I created the mbed_app.json file inside my Online IDE (as explained in this other topic) and now I am able to print my float values!
Just as a side comment, I was also having problems with classical printf("Hello"), that was not printing at all on my serial monitor. I reduced my code, and at a certain point it started to work… I am not sure why, but now I added back other parts of the code as before and everything works fine.
Again, thank you for your kind support!

Best,
Italo

Hello,

that is ok.
I usually met a problem with printf, when I not used \n.

printf("Hello")
// vs
printf("Hello\n")

BR, Jan