首页 > 解决方案 > 如何在 python 中使用 ESCape 键与串口交互?

问题描述

我正在尝试使用 ESCape 键返回菜单页面,但似乎无法将其实现到 if 语句中。一旦按下 ESCape 键,它应该返回到主菜单。我正在使用 Python 3.7

import serial
import Keyboard
ser = serial.Serial ('COM18', baudrate = 115200, timeout = 1) #USB Serial Device

def getMenuData():
    PhoenixMainMenu = ser.readline().decode('ascii')
    return PhoenixMainMenu   

def printScreen():
    n=20
    while n <= 20: #as long as 
        print(getMenuData()) 
        n-=1 
        if n >= 0: 
            print (getMenuData())
            n-=1 
        else:
            break     

while True:
    print (printScreen())
    print ('Escape Key brings you back to main menu')
    UserInput = input('To access option type its coresponding number: ')
    if UserInput == '1':
        ser.write(b'1') #HW test
        print ('first is the worst')
        secondInput = input('To access the function type letter: ')
        if secondInput == 'a':
            ser.write(b'a') 
        else:   
            print ('Invalid entry. Please Try again.')
        break

    elif UserInput == keyboard.wait('esc')
        ser.write(b'0x1b') 
        print ('Main MEnu')
        print (printScreen())        
        break

    else:
        print ('Invalid entry. Please Try again.')

标签: python-3.xkeyboardpyserial

解决方案


推荐阅读