How to accept input from USB barcode Reader with Python

Hello,

I am trying to create a program that will accept input from a usb barcode reader and then perform an HTML request to a website forming a url based on the input

eg (http://thewebsite.com/user/432432).

Looking around I found out that using pyusb library, I can lock a specific USB device and accept input from it.

Also using urllib2.urlopen i can perform the html request.

Firstly, how can I make the python script to continuously wait for user input ( the first barcode card is scanned, the url is formed and then again make the script return to wait for user input? )

Secondly, is it possible to create an executable of that script. Not having experience with python I searched and found that there is py2exe. Because this script depends on 3rd party libraries, is py2exe going to automatically package these libraries eg pyusb ( windows installation says that for pyusb to work you need cygwin ...? does that still makes sense if i develop and package under linux) or do i have to do something specific.

Thanks.

PS: if anyone else is aware of other libraries or a better way of doing what I am trying please let me know.
vlachosgAsked:
Who is Participating?
 
clockwatcherConnect With a Mentor Commented:
Are you positive your USB barcode reader doesn't show up as HID device and act as a keyboard?  Pretty sure you'll find that you don't need to use PyUSB and that the barcode reader will work as if the scanned data is simply being typed in via the keyboard.


import sys
import urllib

while 1:
    line = sys.stdin.readline().rstrip()
    f = urllib.urlopen("http://www.google.com/search?%s" % urllib.urlencode( {'q':line} ))
    print f.read()
0
 
vlachosgAuthor Commented:
thanks for that. I am aware that usb barcode scanners act as keyboard input the problem though is that in the computer that this program is going to be installed the python program will run in the background. So for example when someone is typing something on a texteditor the input is not going to be transferred to the python program as well, is this correct?
0
 
tncodeConnect With a Mentor Commented:
You could program the application to "listen" for specific key sequences and perform a method. For example, the VB 6 equivalent of KeyPreview http://www.idautomation.com/scanenable/#Program_the_application
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
clockwatcherCommented:
So the scanner is dedicated to your app alone and you want it available regardless of whatever else is going on with the computer at the time?  

In that case, maybe libusb and pyusb will be your best bet.   Not sure if Windows 7 is applicable to you or not, but I have doubts that you'll be able to get a libusb-win32 device driver installed under it since Microsoft cut out support for loading non-signed drivers (in non Test Mode).   It may not be applicable to a libusb-win32 filter-based driver (if that's the direction you end up going) but guessing that it will be--  don't see how the filter driver won't have to have some kernel mode component involved.

If this is windows only, raw input would probably be Microsoft's answer for how to do what it sounds like you want to do-- http://msdn.microsoft.com/en-us/library/ms645543%28VS.85%29.aspx

A C# example:

   http://www.codeproject.com/KB/system/rawinput.aspx?fid=375378&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2811936
0
 
vlachosgAuthor Commented:
ok so thats what I decided to do after some research. I will write a key logger with pyhook and also add in the barcode card some prefix eg( bc00c ). So when the input from the barcode (acting as keyboard) is entered the key logger will check letter by letter and if it matches the prefix then it will accept the rest as input and process it accordingly.

I guess that is the best choice because it does not tie the bar-code reader to a specific application.

0
 
vlachosgAuthor Commented:
Not completely covered the questions asked.
0
All Courses

From novice to tech pro — start learning today.