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.