How can one read barcode from a barcode scanner using python - python

I am in need of library that can read barcodes from usb connected barcode scanners. I have done my research but there happens to be one barcode api which was recommended which is https://github.com/libusb/hidapi hidapi but it happens to be written in C++ and I do not know how to use it.
Any suggestion of alternatives to read barcodes from usb barcode scanners will be very well appreciated.

Many barcode scanners will hook into a PC as if it were a keyboard, and send through the text string as keyboard input when a barcode is scanned.
That would require no specialist library, as just monitoring keyboard input would be sufficient.
Looking through the specifications of your specific "datalogic gryphon" barcode scanner, it seems like there are some variants and drivers that allow this "Keyboard Interface".
If this is not working for your device, you can use the "COM-serial" or "RS-232" interfaces as input to your PC, and then use a library like pyserial to read in the input of that stream.
TL;DR: This is a driver issue, you can probably get an easy keyboard-like input if you contact the manufacturer of your device. Otherwise PySerial is the library you are probably looking for.

Connecting barcode scanners as keyboards can be problematic (security issues) or unrefined (scanning barcodes into incorrect fields). Here's a Python library that purports to bring barcodes in directly.
https://github.com/vpatron/barcode_scanner_python

Related

reading input from a specific device python

If i have two keyboards (default keyboard and an RFID reader) in my Linux machine
Using python how can I know from which device the input is coming from ?
I can read my input using the
input()
but i need to distinguish between the two devices
I'm assuming the RFID reader works on USB, if that's the case, should be treated like an input device as well as the HID Keybord (I'm assuming HID by your default).
I'm using evdev in python to do something similar for myself.
You can find the documentation here: http://python-evdev.readthedocs.io/en/latest/tutorial.html#reading-events
It has lots of simple and useful examples, like identify and read from multiple devices asynchronously.
I found it very easy to use.

How to read a tag from PN532 in Python?

I want to experiment with the PN532 that comes connected with an arduino UNO. I have never touched anything similar, and in fact it took me quite a few tries to be able to play with LEDs on another arduino board I have.
Can someone provide me with a concrete example of reading data from the pn532 when I use a tag or card on it in Python?
Take a look at https://github.com/HubCityLabs/py532lib. The standard library is in C, somebody wrote a Python wrapper for the C library that you can use.
I did found a way that might be partially correct. Since Eugenes' answer does not provide a definitive way that we know will work, it cannot be considered a full answer so I will most probably accept this one if nothing else changes.
First of all since the PN532 does not continuously monitor for signals and emits data, we will have to program it the usual way in order for it to behave according to what we want to achieve.
This can be done using the same software that you program any other Arduino device.
Make sure you have chosen the correct port from the tools menu. If you don't know which port is that, in windows go to Start>All Programs>Accessories>System Tools>System Information>Components>Ports>Serial. For Linux going to /dev/serial/by-id should do.
Then I would recommend using the examples provided by the manufacturer here. Make sure you choose the right connection type or else you will see no data coming from the device (Most probably you will want I2C).
Once that is done, and your device emits data each time a tag is used on it (check with a serial terminal configured at 115200 Baud rate) then you are ready to start working with python.
Again I recommend this module to read your data from the serial port. It even comes with a ready to use example of a wxWidgets terminal to read your data from the PN532. If of curse you use another python library and you think its better do say so in the comments.

put interface down in windows with python?

A friend asked me for a program to switch his integrated wifi card off and on. I've googled it with no luck. Can someone tell me if there's any way to do that natively in python?
Does the solution have to be native? I would use a utility called 'devcon' which is created by Microsoft. You could use Python to call devcon and parse the output and disable the required device(s).
Here's an example:
http://en.kioskea.net/faq/1886-enable-disable-a-device-from-the-command-line
You can get the list of network devices with
devcon hwids =net
You can try to parse the one with WiFi in it and the lines that start with PCI.
Or you can do something like:
devcon disable hwids =net, unfortunately this disables the ethernet devices as well.
If you need to just disconnect from WiFi you can use netsh wlan disconnect
EDIT: Ok, finally found the perfect solution (IMHO).
You will need to install two packages pywin32 and wmi.
Using WMI (Windows Management Instrumentation) bindings for Python this becomes pretty easy.
import wmi
cur=wmi.WMI()
wireless_devices=cur.query("select * from Win32_NetworkAdapter WHERE NetConnectionID = 'Wireless Network Connection'")
for device in wireless_devices:
device.Disable()
If it returns a tuple with the first value of (0) it succeeded. Obviously, this must be run as an administrator (otherwise you will get 5 as the return value).
If there is a way to do it from the win32 API in C/C++ (I honestly don't know), then you can use the pywin32 bindings to do it from Python.

Insert keypresses into the Linux console from Python

I have recently been faced with a rather odd task, one result being the necessity for the ability to use DTMF (aka "Touch Tone") tones to control a non-X Linux computer's terminal. The computer has a modem which can be accessed through ALSA, and therefore the sox "rec" program, which is what I am reading the input through. The computer in question is otherwise completely isolated, having no Ethernet or other network interfaces whatsoever. The Goertzel algorithm implementation I am using works very well, as does the eSpeak speech synthesis engine which is the only source of output; this is supposed to work with any Touch Tone phone. It reads back both input (input being octal digits, one ASCII byte at a time)and whatever the dash shell feeds back -- the prompt, the output from commands, etc., using ASCII mnemonics for control characters.
The current method that I am using for interacting with dash and the programs launched through it is the pexpect module. However, I need it to be able to, on demand, read back the entire contents of the line on which the cursor is positioned, and I do not recall pexpect being able to do this (If it is, I cannot tell.). The only other solution that I can think of is to somehow use Python to either control, or act as, the keyboard and console drivers.
Is this, indeed, the only way to go about it (and if so, is it even possible with Python?), or is there another way of having direct access to the contents of the console?
Edit: Through dumb luck, I just recently found that the SVN version of PExpect has pexpect.screen. However, it does not have any way of actually running a program under it. I'll have to keep an eye on its development.
The simple solution is to use the Linux kernel uinput interface. It allows you to insert keypresses and mouse events into the kernel, exactly as if they came from a physical human interface device. This would basically turn your application into a keyboard/mouse.
Since you are working with Python, I recommend you take a look at the python-uinput module.
If you are comfortable with binary I/O in Python, then you can certainly do the same without any libraries; just check out the /usr/include/linux/uinput.h header file for the structures involved (the interface is completely stable), and perhaps some uinput tutorials in C, too.
Note that accessing the /dev/uinput or /dev/input/uinput device (depending on your distribution) normally requires root privileges. I would personally run the Python service as a user and group dedicated to the service, and modify/add a udev rule (check all files under rules.d) to allow read-write access to the uinput device to that group, something like
SUBSYSTEM=="input", ENV{ID_INPUT}=="", IMPORT{builtin}="input_id"
KERNEL=="uinput", MODE="0660", GROUP="the-dedicated-group"
However, if your Python application simply executes programs, you should make it a terminal emulator -- for example, using this. You can do it too without any extra libraries, using the Python pty; the main work is to however simulate a terminal with ANSI escape sequences, so that applications don't get confused, and the existing terminal emulators have such code.
If you want to manipulate the contents of the console, you probably want to use curses. It's well documented here. Look at window.getch() and window.getyx().

USB Barcode scanner research

I'm doing some feasibility research with regards to a (large) book cataloging project. Any help would w/r/t good sources of information would be appreciated but the things I would specifically like to know are:
1.)does python have any modules for use with barcode readers (preferably USB)? What other programs are available for reading barcode information?
2.)This may be better answered with a few sources of information, but what kinds of things should I look for in a barcode scanner, especially if I'm looking to scan, specifically, a lot of barcodes on books.
3.)This may also be better answered with a few sources of information, what kind of information is stored standard in a book's barcode.
I know this is relatively off-topic, but any help would be greatly appreciated.
You don't need any - most USB barcode scanners emulate a keyboard - if you scan a barcode it simply sends a series of key presses representing the numbers (or text if it's a more advanced barcode encoding) encoded in the barcode. Most scanners are pretty configurable so you could configure it to send a special character before the barcode data, then listen for key presses and if it matches that special character, then focus on the input field where you want the barcode data.
It depends a lot on what you're trying to do, the furniture where it's being done, etc. I was involved in a reasonably large rollout of general purpose scanners (happened to be these) - it worked fine for what we were doing and there weren't any drivers to install or anything...the only thing that needed to be done was the stand that came with it was bolted to the desk next to the PC - and that wasn't our concern!
A typical book barcode encodes the book's ISBN.
Are you making a python web-app or a client application? Unless you're looking for specialized bar code scanning functionality it doesn't really matter since the input from the scanner appears like keyboard entry (sometimes prefixed with a special input character), so you just have to have a way of listening to the keyboard input and catching that special character.

Categories

Resources