-
Notifications
You must be signed in to change notification settings - Fork 401
/
Reader.py
executable file
·63 lines (55 loc) · 2.3 KB
/
Reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# There are a variety of RFID readers out there, USB and non-USB variants.
# This might create problems in recognizing the reader you are using.
# We haven't found the silver bullet yet. If you can contribute to this
# quest, please comment in the issue thread or create pull requests.
# ALTERNATIVE SCRIPTS:
# If you encounter problems with this script Reader.py
# consider and test one of the alternatives in the same scripts folder.
# Replace the Reader.py file with one of the following files:
# * Reader.py.experimental
# This alternative Reader.py script was meant to cover not only USB readers but more.
# It can be used to replace Reader.py if you have readers such as
# MFRC522, RDM6300 or PN532.
# * Reader.py.kkmoonRFIDreader
# KKMOON RFID Reader which appears twice in the devices list as HID 413d:2107
# and this required to check "if" the device is a keyboard.
# import string
# import csv
import os.path
import sys
from evdev import InputDevice, ecodes, list_devices
from select import select
def get_devices():
return [InputDevice(fn) for fn in list_devices()]
class Reader:
reader = None
def __init__(self):
self.reader = self
path = os.path.dirname(os.path.realpath(__file__))
self.keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
if not os.path.isfile(path + '/deviceName.txt'):
sys.exit('Please run RegisterDevice.py first')
else:
with open(path + '/deviceName.txt', 'r') as f:
deviceName = f.read()
devices = get_devices()
for device in devices:
if device.name == deviceName:
self.dev = device
break
try:
self.dev
except:
sys.exit('Could not find the device %s\n. Make sure is connected' % deviceName)
def readCard(self):
stri = ''
key = ''
while key != 'KEY_ENTER':
r, w, x = select([self.dev], [], [])
for event in self.dev.read():
if event.type == 1 and event.value == 1:
stri += self.keys[event.code]
# print( keys[ event.code ] )
key = ecodes.KEY[event.code]
return stri[:-1]