I'm hoping to connect two USB barcode scanners to a single computer, allowing two people to scan ticket barcodes simultaneously. If both people scan at the same time, however, the input from 开发者_如何学运维STDIN will get messy.
Is there a way to set up different STDIN streams, each tied to a specific USB port? The console script is in Ruby, though pointers in any language would be much appreciated.
Try connecting 2 USB barcode scanners first and test if what you are afraid of happens. I suspect all barcode digits are sent in a single burst while interrupts from other keyboard-like devices are on hold - so you won't get two barcodes "interlaced" together.
It's impossible to have more than one stdin, not literally. Other options will depend based on the OS you are on - on linux maybe the drivers provide you different character devices for different scanners. On Windows i believe you will have to learn talking the USB HID protocol... best if you don't have to do any of that.
To answer your question directly, there is no way to have more than one STDIN. You're best bet would be to read the data from a file (I would think you should have a character device or something for this scanner)
What if you use a Mutex to synchronize them so only one scan can be processed at a time?
Here's a tutorial on working with threads in Ruby
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html
精彩评论