pyOCD icon indicating copy to clipboard operation
pyOCD copied to clipboard

RTT: Crash with `'utf-8' codec can't decode byte` when arrow key is pressed

Open Rodrigodd opened this issue 1 year ago • 1 comments

On Windows, on Powershell, if I press an arrow key while pyocd rtt is running, it crashes:

> pyocd rtt --target stm32c011f4px
0001518 I Target type is stm32c011f4px [board]
0001636 I DP IDR = 0x0bc11477 (v1 MINDP rev0) [dap]
0001636 I debugvar 'DbgMCU_APB_Fz1' = 0x0 (0) [pack_target]
0001636 I debugvar 'DbgMCU_APB_Fz2' = 0x0 (0) [pack_target]
0001636 I debugvar 'DbgMCU_CR' = 0x6 (6) [pack_target]
0001636 I AHB-AP#0 IDR = 0x04770031 (AHB-AP var3 rev0) [discovery]
0001636 I AHB-AP#0 Class 0x1 ROM table #0 @ 0xf0000000 (designer=020:ST part=466) [rom_table]
0001652 I [0]<e00ff000:ROM class=1 designer=43b:Arm part=4c0> [rom_table]
0001652 I   AHB-AP#0 Class 0x1 ROM table #1 @ 0xe00ff000 (designer=43b:Arm part=4c0) [rom_table]
0001652 I   [0]<e000e000:SCS v6-M class=14 designer=43b:Arm part=008> [rom_table]
0001652 I   [1]<e0001000:DWT v6-M class=14 designer=43b:Arm part=00a> [rom_table]
0001652 I   [2]<e0002000:BPU v6-M class=14 designer=43b:Arm part=00b> [rom_table]
0001652 I CPU core #0: Cortex-M0+ r0p1, v6.0-M architecture [cortex_m]
0001652 I   Extensions: [MPU] [cortex_m]
0001652 I Setting core #0 (Cortex-M0+) default reset sequence to ResetSystem [pack_target]
0001652 I 2 hardware watchpoints [dwt]
0001652 I 4 hardware breakpoints, 0 literal comparators [fpb]
0001704 I 3 up channels and 3 down channels found [rtt_cmd]
0001704 I Reading from up channel 0 ("Terminal") [rtt_cmd]
0001706 I Writing to down channel -1 ("") [rtt_cmd]
My micro output while I not press the arrow key...
and when I press an arrow key by accident:
0002058 C 'utf-8' codec can't decode byte 0xe0 in position 0: unexpected end of data [__main__]

When I press the arrows key, Powershell writes ANSI code to stdin:

> cat.exe > outptu.txt # press up, down, left, right, end, ctrl-c
> xxd .\outptu.txt
00000000: 1b5b 411b 5b42 1b5b 441b 5b43 0a         .[A.[B.[D.[C.

Version Info

> pyocd --version
0.36.0
> pwsh --version
PowerShell 7.4.6

Rodrigodd avatar Dec 19 '24 14:12 Rodrigodd

Hello, I find that the function getch() in kbhit.py crashes under the directory utility. The following is my solution:

    def getch(self):
        ''' Returns a keyboard character after kbhit() has been called.
            Should not be called in the same program as getarrow().
        '''
                
        if os.name == 'nt':
            c = msvcrt.getch()
            if c == b'\xe0' or c == b'\x00':
                msvcrt.getch() #discard
                return chr(0)
            else:
                return c.decode('utf-8')
        
        else:
            return sys.stdin.read(1)

MoHenHM avatar Apr 02 '25 00:04 MoHenHM