Aug-27-2020, 07:40 AM
Hi,
I am trying to accomplish the following situation.
I have a barcodescanner connected by USB to a raspberry pi 4 and I am able to capture the output of the barcode scanner.
How I would like to have the program behave is: the scanner should wait for a barcode that gets scanned, while the main program continues to run and performs other tasks.
My thoughts were to use threading for this, but when I run the code below, the main program is also waiting for the input from the scanner. I think I am missing something, but I can't figure out how to do this.
I hope somebody can help me or guide me in the right direction.
I am trying to accomplish the following situation.
I have a barcodescanner connected by USB to a raspberry pi 4 and I am able to capture the output of the barcode scanner.
How I would like to have the program behave is: the scanner should wait for a barcode that gets scanned, while the main program continues to run and performs other tasks.
My thoughts were to use threading for this, but when I run the code below, the main program is also waiting for the input from the scanner. I think I am missing something, but I can't figure out how to do this.
I hope somebody can help me or guide me in the right direction.
import threading
import serial
import queue
q = queue.Queue()
ser = serial.Serial("/dev/ttyACM0",9600)
def thread_scanner(q):
try:
ser.open()
except IOError:
ser.close()
ser.open()
barcode = ser.readline()
q.put(barcode)
if __name__ == "__main__":
scanner_thread = threading.Thread(target=thread_scanner, args=(q,))
scanner_thread.start()
while True:
if q.qsize > 0:
barcode = q.get()
q.task_done()
print(barcode)
barcode = ""
else:
print("1")
scanner_thread.join()Thank you in advance.
