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

}

11 REPLIES 11

It might help others to describe what the problem / solution was.

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