2

Please refrain from calling this a duplicate, I am completely new to idea of accessing USB devices via Python.

The other questions and answers were often too high level for me to comprehend.

I have a qr code scanner that is USB plug and play.

I can't find it on the command line for whatever reason and it has me stumped.

When the scanner scans a QR code I want its data to be sent to my python script so I can set it to a variable for comparison against a database.

I don't understand how to access a USB device and retrieve the information with Python.

I have read quite a bit about it and still nothing.

Is there a some-what simple way of doing this?

Fendec
  • 367
  • 1
  • 5
  • 23

2 Answers2

1

QR scanner are either USB HID (Human Interface Device - http://www.usb.org/developers/hidpage) devices or more rarely implement virtual com ports (USB CDC ACM device class)

for a HID QR scanners relevant is http://www.usb.org/developers/hidpage/pos1_02.pdf

a USB HID POS device sends the data (i.e. scanned QR codes) in HID reports the structure of these reports is given by USB standard, in HID report descriptors, for QR and barcode scanners see http://www.usb.org/developers/hidpage/pos1_02.pdf page 37,38 : Scanned Data Report, Raw Scanned Data Report when the device sends the HID report containing the data the driver triggers an OS event. these event are captured by an event handler that you have to implement:

Add event handlers

Barcode scanner applications use two important event handlers, DataReceived and ReleaseDeviceRequested.

The DataReceived event occurs when the device scans a barcode and allows your app to receive and process the scanned data. Its The args parameter contains the BarcodeScannerReport, which in turn contains the data obtained from the scan.

source: https://msdn.microsoft.com/en-us/library/dn792060%28v=winembedded.81%29.aspx (translate this to python)

to handle the HID windows events you can use pywinusb

Using pywinusb.hid

View the ./examples directory for some (ok, few right now) scripts. These show, for instance, how to use pywinusb.hid to handle events from HID class devices usages events.

source: https://pypi.python.org/pypi/pywinusb/

maybe also useful links:

http://www.beyondlogic.org/usbnutshell/usb1.shtml

http://eleccelerator.com/tutorial-about-usb-hid-report-descriptors/

http://ww1.microchip.com/downloads/en/AppNotes/01144a.pdf

PyUSB send HID report (https://pypi.python.org/pypi/pywinusb/)

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34
0

I've not done this with a special device as a QR-Reader attached to the computer, but what I did was to use the webcam to provide image data which is subsequently processed by a tool named zbar to scan barcode (in my case the isbn number of books) and then call up a browser to find the information concerning the book.

So in my opinion, you can do the same.

My code looks like this:

import os

p=os.popen('/usr/bin/zbarcam /dev/video1', 'r')
while True:
    code = p.readline()
    print 'Got barcode:', code
    isbn = code.split(':')[1]
    os.system('google-chrome http://www.goodreads.com/search/search?q=%s'%isbn)

So the generalized approach is to find a tool for your system, that provides you with the necessary support to make use of your device (possibly http://zbar.sourceforge.net/ is a good starting point). If you're able to call it on the command line, you can use os.popen() or better subprocess.popen() to call the command line tool and further process the data as you like.

HTH

ferdy
  • 7,366
  • 3
  • 35
  • 46