2024-11-10 06:22 AM - last edited on 2024-11-10 11:34 AM by SofLit
I am able to use the demo code as is to send characters to TeraTerm but when I try to use scanf to get inputs from TeraTerm, i get nothing. when i debug the code, i see that the function doesn't for user entries and just go to next line in the code.
below is my C code snippet:
/* Initialize COM1 port (115200, 8 bits (7-bit data + 1 stop bit), no parity */
BspCOMInit.BaudRate = 115200;
BspCOMInit.WordLength = COM_WORDLENGTH_8B;
BspCOMInit.StopBits = COM_STOPBITS_1;
BspCOMInit.Parity = COM_PARITY_NONE;
BspCOMInit.HwFlowCtl = COM_HWCONTROL_NONE;
if (BSP_COM_Init(COM1, &BspCOMInit) != BSP_ERROR_NONE)
{
Error_Handler();
}
/* USER CODE BEGIN BSP */
/* -- Sample board code to send message over COM1 port ---- */
printf("Welcome to STM32 world !\n\r");
/* Example code to use scanf for receiving input */
char user_input[100];
printf("Enter a message: ");
scanf("%99s", user_input); // Reads a string from COM port into user_input
printf("You entered: %s\n\r", user_input);
Solved! Go to Solution.
2024-11-10 07:16 AM
At the very least you'd need to ensure the plumbing works, so __io_getchar() fetches data from the USART, and syscalls.c/_read() supports that.
Realistically you want buffering, and background interrupting for the USART RX stuff to work concurrently/transparently, rather than just bothering to pay attention in the instant you want input.
And perhaps more formally handle the input, and use sscanf(). This is embedded, and you don't have an OS carrying water.
2024-11-10 07:16 AM
At the very least you'd need to ensure the plumbing works, so __io_getchar() fetches data from the USART, and syscalls.c/_read() supports that.
Realistically you want buffering, and background interrupting for the USART RX stuff to work concurrently/transparently, rather than just bothering to pay attention in the instant you want input.
And perhaps more formally handle the input, and use sscanf(). This is embedded, and you don't have an OS carrying water.
2024-11-10 07:20 AM
https://community.st.com/t5/stm32cubemx-mcus/stm32-use-scanf-with-usart/td-p/268868
https://forum.digikey.com/t/easily-use-scanf-on-stm32/21103
A quick Google might pick up additional coverage