Skip to main content
Associate II
November 27, 2024
Solved

No Printf output on UART before \n

  • November 27, 2024
  • 2 replies
  • 1626 views

Good morning, I am trying to print a string on STM32F401RBT6 using vs code and Cmake, but I keep getting an issue, when I use '\n' the UART doesn't print nothing beyond the '\n', can somebody help me with this issue, please?

 

printf("Hello\n World");
Best answer by Andrew Neil

@Gui_STM wrote:

 when I use '\n' the UART doesn't print nothing beyond the '\n',


I presume you mean, "doesn't print anything beyond the \n" ?

This is defined behaviour: the printf output is line-buffered; so the output is not sent until a line is complete - ie, until it reaches a \n

https://community.st.com/t5/stm32-mcus/how-to-redirect-the-printf-function-to-a-uart-for-debug-messages/ta-p/49865#:~:text=Line%2Dbuffering%3A,sent%3B%20e.g.%2C

 

PS:

stderr is not buffered:

https://community.st.com/t5/stm32cubeide-mcus/retarget-printf-on-stm32g070rbxx-cortex-m0/m-p/721027/highlight/true#M30725

 

PPS:

Disable the buffering with:

 

setvbuf(stdout, NULL, _IONBF, 0);

 

https://community.st.com/t5/stm32-mcus-products/stm32h747-getchar-prototype-strangeness/m-p/618009/highlight/true#M229551

 

#PrintfLineBuffered #StdoutLineBuffered #LineBuffered

2 replies

Andrew Neil
Andrew NeilBest answer
Super User
November 27, 2024

@Gui_STM wrote:

 when I use '\n' the UART doesn't print nothing beyond the '\n',


I presume you mean, "doesn't print anything beyond the \n" ?

This is defined behaviour: the printf output is line-buffered; so the output is not sent until a line is complete - ie, until it reaches a \n

https://community.st.com/t5/stm32-mcus/how-to-redirect-the-printf-function-to-a-uart-for-debug-messages/ta-p/49865#:~:text=Line%2Dbuffering%3A,sent%3B%20e.g.%2C

 

PS:

stderr is not buffered:

https://community.st.com/t5/stm32cubeide-mcus/retarget-printf-on-stm32g070rbxx-cortex-m0/m-p/721027/highlight/true#M30725

 

PPS:

Disable the buffering with:

 

setvbuf(stdout, NULL, _IONBF, 0);

 

https://community.st.com/t5/stm32-mcus-products/stm32h747-getchar-prototype-strangeness/m-p/618009/highlight/true#M229551

 

#PrintfLineBuffered #StdoutLineBuffered #LineBuffered

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
November 27, 2024

@Andrew Neil wrote:
printf output is line-buffered

To be more precise, the GCC default for stdout is line-buffered - so fprintf  (or anything else) to stdout would, by default, show the same effect.

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.
Gui_STMAuthor
Associate II
November 27, 2024

Thank you, I will try this solution on my code.

Tesla DeLorean
Guru
November 27, 2024

Or sprintf() into HAL_UART_Transmit(), it returns a length

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Gui_STMAuthor
Associate II
November 27, 2024

But how does this solve my problem with printing on my UART?

Andrew Neil
Super User
November 27, 2024

@Gui_STM wrote:

But how does this solve my problem with printing on my UART?


because your "problem" is due to the buffering performed by printf (strictly, by stdout).

By not using printf, you avoid that problem!

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.