Skip to main content
Explorer
November 10, 2024
Solved

scanf over VCP on nucleo-C071RB

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

 

This topic has been closed for replies.
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
November 10, 2024

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..

2 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
November 10, 2024

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
November 10, 2024
Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..