Page 1 of 1

scanf() rush through

Posted: Thu Jun 16, 2022 10:36 am
by rdson@esp
Hi there,

I thought to use the scanf() function to get the user's choice from a displayed menu over the UART0 but it doesn't stop at the scanf() function to wait for a user input... and simply rushes endlessly the while(1) loop executing the default from the switch case.

Why? I went through this interessting post http://sekrit.de/webdocs/c/beginners-gu ... scanf.html but couldn't fix my issue though...

Code: Select all


while(1){
	
	int choice = 0;
	
	... some printf() ...
	
	printf("Your choice:");
        scanf("%1d", &choice);

        switch(choice){

            case 1:						// Lists files and folders of SD card
                list_sd_data();
                printf("Hit any key to go back");
                scanf( %*d");
                break;

            default:
                printf("Yet not implemented\n");
                break;

        }
}
        
   

Re: scanf() rush through

Posted: Fri Sep 20, 2024 5:52 am
by cistern
I have the same problem. I also tried fgets() with the same result. Does anybody know the reason for that?

This is the code:

Code: Select all

#include <stdio.h>
#include "freeRTOS/freeRTOS.h"
#include "FreeRTOS/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "driver/uart.h"

#define TX_PORT_NUM GPIO_NUM_43
#define RX_PORT_NUM GPIO_NUM_44
#define RTS_PORT_NUM GPIO_NUM_15
#define CTS_PORT_NUM GPIO_NUM_16

#define BUF_SIZE (128)


void app_main(void)
{    
    // UART-Konfiguration
    const uart_port_t uart_num = UART_NUM_0;         

    uart_config_t uart_config = {                  
        .baud_rate = 19200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
    };

    
    ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config)); 
    ESP_ERROR_CHECK(uart_set_pin(uart_num, TX_PORT_NUM, RX_PORT_NUM, RTS_PORT_NUM, CTS_PORT_NUM)); 
    ESP_ERROR_CHECK(uart_driver_install(uart_num, 2*BUF_SIZE, 0, 0, NULL, 0));
    
    while(1)
    {

        char name[40];
        printf("What's your name? ");
        if (fgets(name, 40, stdin))
        {
            printf("Hello %s!\n", name);
        }
        
    }
}
And this is the serial output of my ESP32-S2-SOLO (I didn't copy all the repetitions):
What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your nameE (5278) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
E (5278) task_wdt: - IDLE (CPU 0)
E (5278) task_wdt: Tasks currently running:
E (5278) task_wdt: CPU 0: main
E (5278) task_wdt: Print CPU 0 (current core) backtrace


Backtrace: 0x40089BC3:0x3FFC1650 0x40089F54:0x3FFC1670 0x400264B5:0x3FFC16A0 0x40084805:0x3FFC4420 0x400856C3:0x3FFC4450 0x4008799D:0x3FFC4470 0x4008BA2E:0x3FFC4490 0x40087509:0x3FFC44B0 0x4008799D:0x3FFC44D0 0x4001A193:0x3FFC44FGuru Meditation Error: Core 0 panic'ed (Interrupt wdt timeout on CPU0).

Core 0 register dump:
PC : 0x4001276a PS : 0x00060934 A0 : 0x80012b1f A1 : 0x3ffc1470
A2 : 0x00000000 A3 : 0x0000007f A4 : 0x3f00583c A5 : 0x00000000
A6 : 0x00000014 A7 : 0xfffffffb A8 : 0x3f40001c A9 : 0x00000080
A10 : 0x0000005c A11 : 0x3ffc1620 A12 : 0x00000000 A13 : 0x80089f57
A14 : 0x0000005b A15 : 0x00000000 SAR : 0x00000005 EXCCAUSE: 0x00000005
EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x80089f57 LCOUNT : 0x4002517c
Core 0 was running in ISR context:
EPC1 : 0x40082117 EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x4001276a


Backtrace: 0x40012767:0x3ffc1470 0x40012b1c:0x3ffc14a0 0x4000f9b3:0x3ffc14c0 0x4000f985:0x3ffc14e0 0x4000fda5:0x3ffc1500 0x4000ff21:0x3ffc1580 0x400253d2:0x3ffc15d0 0x400255bc:0x3ffc15f0 0x40025629:0x3ffc1620 0x40089bc3:0x3ffc1650 0x40089f54:0x3ffc1670 0x400264b5:0x3ffc16a0 0x40084805:0x3ffc4420 0x400856c3:0x3ffc4450 0x4008799d:0x3ffc4470 0x4008ba2e:0x3ffc4490 0x40087509:0x3ffc44b0 0x4008799d:0x3ffc44d0 0x4001a193:0x3ffc44f0 0x4001a6c5:0x3ffc4510 0x40001b0e:0x3ffc4530 0x40001b94:0x3ffc4550 0x4008b33a:0x3ffc4580 0x4008f3a7:0x3ffc45b0 0x4008ed85:0x3ffc45d0 0x4008b40b:0x3ffc48f0 0x40088147:0x3ffc4940 0x40096220:0x3ffc49b0 0x4002a3b5:0x3ffc49e0




ELF file SHA256: c5c622cc7

Rebooting...
)¦ĆŤa˙Ż9Ś˝§ë!zc~­˝cćŕyÜöł-c#Ú
c3Z§ŚĚ­'ŤŚçFZ´X@+!cAjô!ió…¤#+C…® !J‘FJ®z
‰”+YIŃ!)`iHóŢWĽk$#Z?i
c!c’Z „ńńÓN
i!! q$#!„c!¬!!äőa!"ÇhÚ¤k)!B!a÷7â)+Ţ(Ú„+!##„a+ŢWHk*!B!„
G%I#!¤„Ą„Ĺ"ˇ!!ŹýŤ¤ń´Ě/ë‘1«Z„h„ń´Ě/ë‘1«áo«·‘c…Úo»÷·ő‘1+Úo9!14b)+ˇ˝/ŁýĄ§cŚ3Ś
I!!…!}-ě!écc+AaXg9
H Ę
aiĄŤ#C©“i…a#UVÇ4ÂyIÚ…!)¤?R#ZXŚý!!i
CĺĚĚ!}#Ť)!ˇ$y3'íŤí „Xm©
¬íĺOm~3c…!­!+‰‰!io3O9#Ś!·!ę­#Ż¶îŃ˙What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name? What's your name?