cancel
Showing results for 
Search instead for 
Did you mean: 

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

CR72
Associate II

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 😊

14 REPLIES 14

@Community member​ 

I tried your suggestion and it works. Interestingly it works both as you've written and the way I will show below:

uint8_t buffer[64];

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

It's interesting because the CDC_Transmit_FS only takes two arguments, a pointer to the buffer and the size of the buffer.

Here's what I did using a library called "libfixmath":

fix16_t c = fix16_from_float(-6.1455);
fix16_t d = fix16_from_float(-1.7);
fix16_t length2 = calc_length16(c,d); // root sum of squares
 
uint8_t buffer[64];
CDC_Transmit_FS(buffer,sprintf((char *)buffer, "%ld\r\n", length2), sizeof(buffer));

I can't get CDC_Transmit_FS to print doubles or floats on my little controller (at least not what I've tried). But it will print the data type that is defined in the "libfixmath" library, fix16_t, which is defined as

typedef int32_t fix16_t;

So for the code above the calc_length returns the root sum of the squares of "c" and "d", and the result is 6.376297, but what is printed to the screen is 417877, because 417877/65536 = 6.376297 (to some fixed number of decimal places). It also prints the negative values as well.

Why pass 3 parameters?

sprintf() reports the string length generated

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

i think length2 is the "long" parameter he is trying to send over USB.

Confusing generic name choices

we dont need to firmware by ourselves, lets talk

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


@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