STM32H753: Receiving Messages via UART
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-10 4:49 AM
I'm working with an STM32H753 MCU and trying to receive messages from a PC via UART to implement a simple state machine. The idea is that the MCU should switch to different states based on the received message.
Initially, I attempted to use fgets(message, sizeof(message), stdin);, but the buffer named message seems to be empty after receiving, and I get nothing when printing it using printf("\r\nThe sentence is: %s", message);. Is this the correct way to do so?
Solved! Go to Solution.
- Labels:
-
STM32H7 series
-
UART-USART
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-10 6:56 AM
Hi @doronzzz
I think BSP redefines PUTCHAR service as based on HAL UART Transmit API. But GETCHAR service is not routed to HAL UART Receive API in BSP.
You might have a look at some UART examples as "UART_Console". (Not available in H7 package, but available on other series. you might find code here for instance)
Available code in main.c should be easy to adapt to your case. The goal of this project is to redefine PUTCHAR and GETCHAR to HAL_UART_Transmit()/HAL_UART_Receive() services.
In same way, you could add your definition of fgets().
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-10 4:54 AM
Hello,
Did you first validate a simple receive example using HAL before going ahead with fgets?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-10 4:59 AM
Before adding the complications of stdin, printf, fgets, etc have you made sure that your basic UART IO is working?
https://wiki.st.com/stm32mcu/wiki/Getting_started_with_UART
https://wiki.st.com/stm32mcu/wiki/STM32StepByStep:Step3_Introduction_to_the_UART
If you have an ST board, CubeMX (standalone, or via CubeIDE) should have ready-to-go examples.
Note that GCC defaults to stdout being line-buffered:
I haven't tried it, but that may also apply to stdin ?
@doronzzz wrote:Is this the correct way ?
Very often, handling character-by-character is a better approach ...
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-10 5:11 AM - edited ‎2025-03-10 5:17 AM
Yes, UART is configured in the Bsp and the code is automatically generated. printf works fine, and I can read the message using a serial monitor. However, fgets doesn't seem to be blocking, causing 'The sentence is:' to repeatedly print empty string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-10 6:42 AM
@doronzzz wrote:Yes, UART is configured in the Bsp and the code is automatically generated. printf works fine
That shows that TX works - but have you also demonstrated that basic RX works - without the stdin & fgets overheads?
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-10 6:56 AM
Hi @doronzzz
I think BSP redefines PUTCHAR service as based on HAL UART Transmit API. But GETCHAR service is not routed to HAL UART Receive API in BSP.
You might have a look at some UART examples as "UART_Console". (Not available in H7 package, but available on other series. you might find code here for instance)
Available code in main.c should be easy to adapt to your case. The goal of this project is to redefine PUTCHAR and GETCHAR to HAL_UART_Transmit()/HAL_UART_Receive() services.
In same way, you could add your definition of fgets().
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-10 7:26 AM - edited ‎2025-03-10 7:26 AM
@Guenael Cadier wrote:The goal of this project is to redefine PUTCHAR and GETCHAR to HAL_UART_Transmit()/HAL_UART_Receive() services.
Actually, the stated goal was, "to receive messages from a PC via UART to implement a simple state machine".
I would suggest just using HAL_UART_Receive - without all the added complexities & gotchas! of redirecting stdin ...
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-10 8:05 AM
Yes, you're right! I used HAL_UART_Receive and achieved the desired result. Thanks! The initial issue was that I set up BSP and used automatic code generation, which made direct UART invocation inaccessible.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-03-10 8:12 AM
@doronzzz wrote:I set up BSP and used automatic code generation, which made direct UART invocation inaccessible.
No, that shouldn't make direct access inaccessible.
A complex system designed from scratch never works and cannot be patched up to make it work.
