Skip to main content
Graduate
November 27, 2024
Solved

No Printf output on UART before \n

  • November 27, 2024
  • 2 replies
  • 1614 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");
    This topic has been closed for replies.
    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

    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

    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.

    Gui_STMAuthor
    Graduate
    November 27, 2024

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

    Graduate II
    November 27, 2024

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

    Gui_STMAuthor
    Graduate
    November 27, 2024

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

    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!