cancel
Showing results for 
Search instead for 
Did you mean: 

No Printf output on UART before \n

Gui_STM
Visitor

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");
1 ACCEPTED SOLUTION

Accepted Solutions

@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

View solution in original post

6 REPLIES 6

@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


@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.

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

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

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

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


@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!