处理输入的USB消息而不进入循环

0 投票
0 回答
26 浏览
提问于 2025-04-12 09:49

我正在尝试为一个Windows应用程序编写代码,这个程序会让一个STM32设备在随机的时间间隔内发送USB消息。收到这些消息后,程序会处理它们并改变窗口中的某些内容。但是,在等待这些消息的同时,程序还需要继续运行并执行其他操作,所以不能简单地用一个循环来一直等待消息。

我找到的唯一解决方案就是使用一个像这样的循环:

while 1:
    print("----")
    while output != "":
        output = serialInst.readline()
        print(output)
    output = " "

或者使用一个等待函数

def usb_wait():
    loop = 5
    for x in range (loop):
        time.sleep(1)
        if serialInst.in_waiting:
            packet = serialInst.readline()
            output_packet = packet.decode('utf').rstrip('\n').rstrip('\r')
            encode = output_packet
            receiveLabel = tk.Label(window, text = output_packet, background= 'white', fg= 'green', font= ('helvetica', 12, 'bold'))
            receiveLabel.place(x=725, y=550)
            print(output_packet)
            output(output_packet)

0 个回答

暂无回答

撰写回答