Skip to main content
VYoun
Associate III
March 26, 2020
Question

stm32 use scanf with usart

  • March 26, 2020
  • 3 replies
  • 9625 views

Hello,

There is an example to use printf with usart. However, I need to find a way to use scanf with usart. I am using STM32H7, STM32Cubemx, and Atollic Truestudio.

Your help is much appreciated.

This topic has been closed for replies.

3 replies

Olivier GALLIEN
Technical Moderator
March 26, 2020

Hi @VYoun​ ,

I know there is this exemple on MP1 Firmware package :

STM32Cube_FW_MP1_V1.2.0\Projects\STM32MP157C-DK2\Examples\UART\UART_Receive_Transmit_Console

Don't know if it has been ported to over MCU but might be fast forward to adapt to H7.

Hope it help,

Olivier

Olivier GALLIEN In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
VYoun
VYounAuthor
Associate III
March 26, 2020

Hello @Community member​ 

thank you very much for your answer, I will check right now.

Best regards,

Vouria

Tesla DeLorean
Guru
March 26, 2020

See

STM32Cube_FW_H7_V1.7.0\Projects\STM32H743I-EVAL\Examples\BSP\SW4STM32\syscalls.c

Implementation of plumbing using __io_putchar() __io_getchar() which would support putchar(), getchar(), scanf(), etc

You'd need to a) initialize the UART you plan on using and b) implement the __io_put/getchar functions

In a general GNU/GCC sense the functionality comes out of the _write() / _read() implementation you provide

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
VYoun
VYounAuthor
Associate III
March 26, 2020

Thank you very much for your answer Clive,

I will check right now.

Best regards,

Vouria

KnarfB
Super User
March 26, 2020

Keep in mind, that there is no stream buffering in the hardware or HAL driver, execpt you implement it.

When scanf is implemented straightforward using _read, you must always call scanf *before* there is any input on the RX line or you will get overrun errors.

KnarfB
Super User
March 27, 2020

... and don't forget to remove the stdio internal buffering. A plain call

int i;
 scanf("%d",i);

ends up a in call to _read with a 1024 (BUFSIZ) char buffer! I.e. _read won't return before the UART supplied 1024 chars of input. This is not what you typically want for an interactive comm. You may remove that buffering by using

setvbuf( stdin, NULL, _IONBF, 0 );
 scanf("%d",i);

which results in calls to _read single char by single char.

Anyway, I would prefer collecting a string manually using USART functions and then using sscanf on it.

VYoun
VYounAuthor
Associate III
March 27, 2020

Hello @KnarfB​ ,

Thank you very much for your reply. I tried the code that you provided but unfortunately I got no results.

I am coming to believe that it is not possible to use scanf with usart. I think I should use usart interrupt and then parse the received string. I am really lost in using scanf with usart.