Skip to main content
CR72
Associate II
May 4, 2022
Question

How do I print floats, fixed point numbers, integers, arrays full of some data type, etc. to a serial monitor?

Hi, I would like to print floating point, fixed point values, integers, arrays of values, etc. to a serial monitor of some kind for debugging purposes. I want to be able to see exactly what my code is doing (I know floats are HUGE in embedded code so I really won't use them, but the principle is what counts). It seems that using CDC_Transmit_FS(&buff, sizeof(buff)) from the USB VCOM class doesn't work. I've even tried to connect my stm32 to an arduino via uart to maybe send the data to the arduino and have the arduino print to the serial monitor (yeah, I know that's a really dumb way to do it), but that hasn't worked out so far. I've researched a lot to try and figure out how to do this. All I can seem to find has me make a uint8_t variable[] = "Text\r\n" (not sure why the empty square brackets are used), and I can't figure out how this might or might not apply to those fixed point, integer, and array, etc. data types.

I also wonder what the maximum fixed point or integer integer value, or array size, etc. that I can print. I'm not sure what I need to understand in order to know the answer to that question though.

I can get this to work and that's about it:

uint8_t buffer[] = "Hello, World!\r\n";

CDC_Transmit_FS(buffer, sizeof(buffer));

I don't want to only print strings, however, but that's all I can do at the moment.

I'm also pretty new to stm32.

Thank you in advance for taking the time to read and respond :smiling_face_with_smiling_eyes:

This topic has been closed for replies.

14 replies

Tesla DeLorean
Guru
May 4, 2022

can you use things like itoa() and dtoa() ?

uint8_t buffer[64];

CDC_Transmit_FS(buffer, sprintf((char *)buffer, "%ld\r\n", dbl));

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
CR72
CR72Author
Associate II
May 4, 2022

@Community member​ 

"can you use things like itoa() and dtoa()"

 I'm not sure, but I'll check

"uint8_t buffer[64];

CDC_Transmit_FS(buffer, sprintf((char *)buffer, "%ld\r\n", dbl));"

I'll give this a go and get back to you

Guillaume K
ST Employee
May 4, 2022

Are you using an ST board like a Nucleo or Discovery board ? the ST boards have a STLINK processor used to program and debug the STM32, but also it can output one of the STM32 UART to USB virtual COM port. On STM32 side it looks like you output characters to serial UART port and on PC side it looks like a serial COM port on USB. You just have to know which serial port to use on STM32 side. On PC side, if you install STM32CubeIDE or STM32CubeProgrammer the USB driver are installed. You can use tera term or others for serial terminal emulator.

Andrew Neil
Super User
May 4, 2022

@Guillaume K​ - "On STM32 side it looks like you output characters to serial UART"

It doesn't just look like it - that is exactly what you are doing!

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Andrew Neil
Super User
May 4, 2022

@CR72​ - "I don't want to only print strings"

Use sprintf() to create formatted strings from all sorts of different data types.

"I'm also pretty new to stm32."

This is not specific to STM32 - it's general C stuff.

Are you familiar with any other microcontroller(s)?

Are you familiar with C programming in general?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
CR72
CR72Author
Associate II
May 5, 2022

@Andrew Neil​ 

Thank you, I'll try the sprintf() function.

"Are you familiar with any other microcontroller(s)?"

I have used Arduino and that's about it, and I thought STM32 was a good way to improve my embedded programming skills and knowledge

"Are you familiar with C programming in general?"

I am also pretty new to C, only used what aspects of Arduino programming qualify as C (which I think it technically is)

KKIM.6
Senior
May 5, 2022

Your situation is very similar with me. I also started aduino and I'm working on stm32 from 1 month ago.

I also had a problem printing float number and I found that we have to activate an option before use printf() commend for float number.

follow is photo.0693W00000NpH2rQAF.pngFirst, right click of your project and enter the property. next go to c/c++ build/tool setting/MCU setting/ and check "Use float with printf from newlib-nano (-u _printf_float)".

Otherwise, you will see "?" massge only when you print float number.

To see the massage using UART, you can use printf() like below.

float abc = 3.123456

printf("massaga %.3f", abc);

result: massage 3.123

Visitor II
July 15, 2023

i am not getting float value only getting zeros even though i have changed the properties

RhSilicon
Lead
July 15, 2023

@Ram_satya wrote:

i am not getting float value only getting zeros even though i have changed the properties


Try to clean the project (Project Menu, Clean) and see if any warning occurs.

To operate with these ASCII character manipulation functions, it must be necessary to include the STDIO file:

#include <stdio.h>

The C <stdio.h> sprintf() function writes the C string pointed by format to a character string buffer. If format includes format specifiers (sub-sequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.

After the format parameter, the function expects additional arguments at least as many as the number of format specifiers in the format string.

The size of the buffer should be large enough to contain the resulting string. A terminating null character is automatically appended after the content.

Ref.: https://www.alphacodingskills.com/c/notes/c-stdio-sprintf.php

KKIM.6
Senior
May 5, 2022

and use fflush(stdout); followed by printf().

Otherwise, there will be a delay to print the massage.