cancel
Showing results for 
Search instead for 
Did you mean: 

scanf over VCP on nucleo-C071RB

Ali_C
Associate

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);

 

1 ACCEPTED SOLUTION

Accepted Solutions

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..