2024-08-28 12:57 AM
I am trying to use scanf() in STM32H743, referring a webpage (here). More specifically, after setting up USART1 in STM32CubeMX, I add some function prototypes like:
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define GETCHAR_PROTOTYPE int __io_getchar(void)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
GETCHAR_PROTOTYPE
{
uint8_t ch = 0;
/* Clear the Overrun flag just before receiving the first character */
__HAL_UART_CLEAR_OREFLAG(&huart1);
/* Wait for reception of a character on the USART RX line and echo this
* character on console */
HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
// HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
and add a scanf() in the main function before infinite while loop:
scanf("%c", &c);
where c is a char.
I found that if I read one character (e.g. a char variable) every time, the function works well. But if I try to read more like integers and strings, the function seems being stuck. I don't know where it is stuck and why it happened. Please help. Any help is appreciated.
2024-08-28 01:42 AM
Try deactivating the buffering before scanf():
setvbuf(stdin, NULL, _IONBF, 0);
Regards
/Peter
2024-08-28 02:17 AM
Did you add the config indicated in the link you gave ?
setvbuf(stdin, NULL, _IONBF, 0);
If not could you try it ?
If you did, could you show us the rest of your code ?
S. Marie
2024-08-28 02:25 AM - edited 2024-08-28 02:43 AM
@LittleYe233 wrote:But if I try to read more like integers and strings, the function seems being stuck.
How, exactly, are you testing that?
@Peter BENSCH and @SMarie have suggested disabling buffering - so does your input work as-is if you terminate entry with CR and/or LF ?
EDIT:
See this thread on the buffering of stdin, etc: