Process incoming USB message without sitting in a while loop

29 Views Asked by At

I'm trying to write a program for a windows app where an STM32 will send a USB message at random intervals which will then be processed and change something in the window, but while waiting for these messages the program will need to be running and doing other things so it can't be done by waiting for messages in a while loop.

The only solution I've found is either a while loop like:

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

or a wait function

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

There are 0 best solutions below