Aug-27-2020, 08:57 AM
I haven't run the code. If you do not need access to thread and q from other functions, you can creates them inside a function.
I think your problem comes from scanner_thread.join() inside the loop. This blocks your code until the thread ends, but the thread does never end.
If it's binary data, you've to convert it into data you can work with.
I think your problem comes from scanner_thread.join() inside the loop. This blocks your code until the thread ends, but the thread does never end.
import threading import serial import queue def thread_scanner(q): # serial.Serial should support context managers with serial.Serial("/dev/ttyACM0", 9600) as ser: while True: barcode = ser.readline() q.put(barcode) def mainloop(): """ Starts the mainloop to print barcodes Everything else must be done inside this function or you put it on module level, if it's required. """ q = queue.Queue() scanner_thread = threading.Thread(target=thread_scanner, args=(q,)) scanner_thread.start() while True: barcode = q.get() # get blocks until it has an element available q.task_done() print(barcode) if __name__ == "__main__": mainloop()Since Python 3 serial.read* return bytes. You can decode them, if it's ASCII or some other kind of encoding.
If it's binary data, you've to convert it into data you can work with.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!