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
#2
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.

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!
Reply
#3
Hi Dead_Eye,

Thank you for your answer.
I will run the code you have suggested.
For the moment I have fixed it by using the asyncio and serial_asyncio libraries.

Kind regards,
lightframe109
Reply
#4
In many cases, asyncio is better than threads. Specially if it's IO related.

Actually, the state of the implementation of async pyserial is experimental.
Quote:Warning

This implementation is currently in an experimental state. Use at your own risk.

Just a warning, if something does not work, then expect also bugs from PySerial.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Waiting for input from serial port, then move on KenHorse 3 1,055 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  pip stops waiting for python walker 6 1,056 Nov-28-2023, 06:55 PM
Last Post: walker
  Use Barcode Scanner With Python Extra 2 1,603 Jul-14-2022, 09:52 AM
Last Post: Gribouillis
  Waiting for heavy functions question philipbergwerf 14 3,372 Apr-29-2022, 07:31 PM
Last Post: philipbergwerf
  port scanner Than999 0 1,202 Feb-01-2022, 12:24 PM
Last Post: Than999
  How to create waiting process? samuelbachorik 4 1,973 Sep-02-2021, 05:41 PM
Last Post: bowlofred
  Python continues after CTRL-C kjbolhuis 2 1,889 Aug-06-2021, 04:28 PM
Last Post: kjbolhuis
  Python BLE Scanner not detecting device alexanderDennisEnviro500 0 2,010 Aug-01-2021, 02:29 AM
Last Post: alexanderDennisEnviro500
  Waiting and listening test 2 2,146 Nov-13-2020, 04:43 PM
Last Post: michael1789
  Windows Python Memory Scanner Awesometech 1 21,693 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