cancel
Showing results for 
Search instead for 
Did you mean: 

Hello my friends, Does anyone know why scanf doesn't work here? i checked many things, but only the first printf works and when i write sth in putty scanf doesn't work! i use Nucleo STM32F303K8 Thank you

HA.5
Associate III

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

#include <stdio.h>

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART2_UART_Init(void);

/* USER CODE BEGIN PFP */

#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(&huart2, (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(&huart2);

 /* Wait for reception of a character on the USART RX line and echo this

  * character on console */

 HAL_UART_Receive(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);

 HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);

 return ch;

}

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 setvbuf(stdin, NULL ,_IONBF, 0);

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_USART2_UART_Init();

 /* USER CODE BEGIN 2 */

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

 char str[80];

 int i;

 printf("Enter your family name: ");

 scanf("%79s", str);

 printf("Enter your age: ");

 scanf("%d", &i);

 printf("Mr. %s, %d years old.\n", str, i);

 printf("Enter a hexadecimal number: ");

 scanf("%x", &i);

 printf("You have entered %#x (%d).\n", i, i);

}

1 ACCEPTED SOLUTION

Accepted Solutions
HA.5
Associate III

Thanks LCE for your message,

Actually i use Nucleo-STM32F303K8, so i used UART2 to communicate between PC and Nucleo. I used many ways to receive sth from pc like scanf or HAL_UART_Receive function, but it didn't work, although CUBE-IDE generate automatically PA2 as USART2-TX and PA3 as as USART2_RX, but only TX has the connection to ST-LINK. It means when i press a Key on my PC-keyboard, i had the Signal on board(checked whit oscilloscope), however not on Processor, so i had to define another PIN(PA15) as my UART2-RX and then when i checked with Multi meter the connection were there(connection between ST_LINK and Processor).

if you look at page 11 on this user manual you can clearly know what i mean.

https://www.st.com/content/ccc/resource/technical/document/user_manual/e3/0e/88/05/e8/74/43/a0/DM00231744.pdf/files/DM00231744.pdf/jcr:content/translations/en.DM00231744.pdf

happy to hear any comments from you

View solution in original post

11 REPLIES 11
Foued_KH
ST Employee

the correct code would be:

scanf("%79s", &str);

Scanf did not consume the \n character that stayed in the buffer from the first scanf call.

So the second scanf call did.

You have to clear the stdin before reading again or just get rid of the newline.

The second call should be.

scanf(" %d",&i);

Foued

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.

HA.5
Associate III

i used exactly the same what you wrote here, it doesn't work, i don't know, maybe i need interrupt here for scanf, otherwise i have no more idea

Foued_KH
ST Employee

Hello @HA.5​ ,

 

I have tested your code, It worked correctly :


_legacyfs_online_stmicro_images_0693W00000bka3lQAA.pngI think you should entre your inputs when debug :

0693W00000bka4AQAQ_image.png

Foued

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.

Make sure your UART functions actually work.

Make sure your tools have pulled in the right libraries.

>>it doesn't work..

Yeah, that's really helpful to debug the situation. What *does* happen? Any data read into the buffer? Any data received by the routines?

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

Hello Foued, did you try my hole code? it is really funny that it doesn't work here with me and only the first printf works. I thought maybe the problem is from serial monitor(putty) and i changed to Arduino IDE but still doesn't work. here i send you the whole program file, maybe you can tell me why it doesn't work here with me.

HA.5
Associate III

Hi Tesla, Thanks for your help!

it is really funny that it doesn't work here with me and only the first printf works. I thought maybe the problem is from serial monitor(putty) and i changed to Arduino IDE but still doesn't work. here i send you the whole program file, maybe you can tell me why it doesn't work here with me.

i use Nucleo-STM32F303K8.

HA.5
Associate III

here is the whole code, if someone has an idea maybe, as i said only first printf works and no more. it means that it becomes no input, when i check with oscilloscope i can see that there is no input. but the question is why!!! i used getcahr before scasnf, so normally when i press my keyboard and then enter it should get the inputs and go to next printf, but it doesn't go!!!:((. And as said i thought maybe the problem is from serial monitor(putty) and i changed to Arduino IDE but still doesn't work.

Thank you for your help!

and why in your output you have the first two printf behind each other? "Enter your Familyname" and "Enter your age", because for me works only the firs printf

HA.5
Associate III

Thanks dear friends, i found what the problem was, maybe it was a bug on Platform Cube IDE or maybe on Nucleo-Board.