2022-05-03 05:17 PM
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:
2022-05-03 07:03 PM
can you use things like itoa() and dtoa() ?
uint8_t buffer[64];
CDC_Transmit_FS(buffer, sprintf((char *)buffer, "%ld\r\n", dbl));
2022-05-04 12:45 AM
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.
2022-05-04 01:57 AM
@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?
2022-05-04 01:57 AM
@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!
2022-05-04 04:59 PM
@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
2022-05-04 05:00 PM
@Guillaume K I am using the STM32F072B-DISCO board
2022-05-04 05:08 PM
@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)
2022-05-04 05:30 PM
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.First, 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
2022-05-04 05:34 PM
and use fflush(stdout); followed by printf().
Otherwise, there will be a delay to print the massage.