0

I'm writing a program to accept input from multiple barcode scanners, and differentiate between individual scanners. I'm using pyserial, and I can differentiate between scanners, and get input from each, but the problem is that it's way too unreliable. It seems like when my code is waiting for input from one scanner, the input from the other is lost.

In the following code, assume ports is a list of the ports the scanners occupy:

import serial

while True:
    for port in ports:
        scanner = serial.Serial(port=port, baudrate=9600, bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE)
        string = scanner.readline()
        scanner.close()
        if not string.decode() == '': print(string.decode())

The problem here is that when scanner.readline() executes, it pauses my code for how ever long I specified in the timeout portion of the serial.Serial() line. So what I think is happening is that my code is only looking at one scanner at a time, and misses any imputs from the other during that period. I've also tried timeout=.01, but that only seems to make things worse. I also tried serial.Serial(port=ports, ...) which throws up an error, and I'm unaware of any other method to create an object which contains every scanner, and manipulate them all as a group.

I was also thinking I might be able to tell a scanner to wait, or buffer any inputs from it while I'm reading one, but I don't know where I'd even begin to do that. I'm using scanners from Netum, if that's important. They're pretty opaque when it comes to customer support, so I'm not even certain they can accept input from anything outside of the barcodes they scan. But I do know they have a buffer in case they get disconnected from the computer temporarily, so I might be able to finagle that into what I need.

Any advice? I'm more than willing to switch to a different approach or library. I just need my code to talk to multiple scanners simultaneously (or close to it), as well as differentiate between each of them.

I also saw this post, which should solve my problem on paper, but I didn't even know the subprocess module existed until just now. No matter how much I fiddle with it, I can't get it to work. Even if I swap in my program name in the Popen() line, and add a timeout value, it can't see my scanner no matter what I do. All I get out of p1.stdout.readline() is a 'bytes' object which reads b'' If this is the solution, that's okay, but I'm hoping for something simpler. I'm already pushing the limit of my abilities to the max, so the idea of needing to learn yet another library is pretty daunting.

  • For gosh sakes, don't open and close the port during every loop!! Open all the ports once and leave them open You can spawn a thread for each port. Python multithreading works great when the threads are mostly blocked. – Tim Roberts Dec 30 '22 at 00:11
  • The simplest approach is to, within the loop, check which devices already have data waiting, and handle the data from each of those (and add a small sleep time to avoid hanging the CPU). This is called *polling* multiple devices. Please see the linked duplicate for an illustration of polling a single device (so as not to have to wait for the entire `timeout` whenever the data is absent); extending this `for port in ports:` is trivial. The problem can also be addressed using threads (also covered by the linked duplicate). – Karl Knechtel Dec 30 '22 at 00:13
  • Anyway, welcome to Stack Overflow (and thanks for helping find other duplicate versions of the question - `subprocess` is a totally inappropriate tool for the job, so I linked those questions to the same place as this one). For future reference, please read [ask], and note well that this is **not a discussion forum**. We want questions to be direct and to the point - and please don't include "tags" in your question title; the actual tags work fine. – Karl Knechtel Dec 30 '22 at 00:17
  • @Tim Roberts I'm a total newbie when it comes to professional coding, so I'm sorry!! I do that because it threw up an error when I added a second scanner. I'll fiddle with this more to see if it solves the problem (or at least helps). Thanks so much for the tip! – Spencer Wakefield Dec 30 '22 at 01:55
  • @KarlKnechtel Thank you so much. I'm really sorry for failing to meet the standards set by stack overflow. I'm just barely dipping my toes into the professional world, and this seems to be **the** place for such endeavors. I'll check out the links you provided, including the basics on how to post. – Spencer Wakefield Dec 30 '22 at 02:04

0 Answers0