Created
August 2, 2019 12:04
-
-
Save naveenrobo/17812b486a6b9cbf5da1d49952a5ee79 to your computer and use it in GitHub Desktop.
Python code to read the USB HID devices. i.e., to read from that nameless blackbox rfid scanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import evdev | |
from evdev import categorize, ecodes | |
class Device(): | |
name = 'Sycreader USB Reader' | |
@classmethod | |
def list(cls, show_all=False): | |
# list the available devices | |
devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()] | |
if show_all: | |
for device in devices: | |
print("event: " + device.fn, "\tname: " + device.name, "\thardware: " + device.phys+"\n") | |
return devices | |
@classmethod | |
def connect(cls): | |
# connect to device if available | |
try: | |
device = [dev for dev in cls.list(show_all=True) if cls.name in dev.name][0] | |
device = evdev.InputDevice(device.fn) | |
return device | |
except IndexError: | |
print("Device not found.\n - Check if it is properly connected. \n - Check permission of /dev/input/ (see README.md)") | |
exit() | |
@classmethod | |
def run(cls): | |
device = cls.connect() | |
container = [] | |
try: | |
device.grab() | |
# bind the device to the script | |
print("RFID scanner is ready....") | |
print("Press Control + c to quit.") | |
for event in device.read_loop(): | |
# enter into an endeless read-loop | |
if event.type == ecodes.EV_KEY and event.value == 1: | |
digit = evdev.ecodes.KEY[event.code] | |
if digit == 'KEY_ENTER': | |
# create and dump the tag | |
tag = "".join(i.strip('KEY_') for i in container) | |
print(tag) | |
container = [] | |
else: | |
container.append(digit) | |
except: | |
# catch all exceptions to be able release the device | |
device.ungrab() | |
print('Quitting.') | |
Device.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment