Python Forum
waiting for barcode scanner output, while main program continues to run
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
waiting for barcode scanner output, while main program continues to run
#1
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.

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.
Reply


Messages In This Thread
waiting for barcode scanner output, while main program continues to run - by lightframe109 - Aug-27-2020, 07:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Waiting for input from serial port, then move on KenHorse 3 1,152 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  pip stops waiting for python walker 6 1,089 Nov-28-2023, 06:55 PM
Last Post: walker
  Use Barcode Scanner With Python Extra 2 1,646 Jul-14-2022, 09:52 AM
Last Post: Gribouillis
  Waiting for heavy functions question philipbergwerf 14 3,440 Apr-29-2022, 07:31 PM
Last Post: philipbergwerf
  port scanner Than999 0 1,210 Feb-01-2022, 12:24 PM
Last Post: Than999
  How to create waiting process? samuelbachorik 4 2,010 Sep-02-2021, 05:41 PM
Last Post: bowlofred
  Python continues after CTRL-C kjbolhuis 2 1,915 Aug-06-2021, 04:28 PM
Last Post: kjbolhuis
  Python BLE Scanner not detecting device alexanderDennisEnviro500 0 2,026 Aug-01-2021, 02:29 AM
Last Post: alexanderDennisEnviro500
  Waiting and listening test 2 2,166 Nov-13-2020, 04:43 PM
Last Post: michael1789
  Windows Python Memory Scanner Awesometech 1 22,717 Oct-14-2020, 07:44 AM
Last Post: badengagen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020