-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrfidtag.py
114 lines (99 loc) · 2.98 KB
/
rfidtag.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
#!/usr/bin/env python
# below is the string name of the USB device we want to read
# the rfid tags from
DEVICE_NAME="NewRoad Sem. NewRoad System PS2 Interface"
# find how many hidraw devices are connected and then
# find which hidraw device the rfid reader is connected to
def get_rfid_device_file():
numdevs = len(os.popen('ls /dev |grep hidraw').read().split("\n"))
for i in range(numdevs):
path = '/sys/class/hidraw/hidraw' + str(i) + '/device/uevent'
device_info = os.popen('cat ' + path).read()
if DEVICE_NAME in device_info:
return '/dev/hidraw'+str(i)
return '/dev/null'
# get the rfid code
def get_tag(dev):
"""
Get rfidtag.
This function reads an hidraw data stream from a rfidtag scanner
returns the numeric value of the rfidtag scnned.
Parameters
----------
dev : str, optional
Full path hidraw device from which to read hidraw data stream.
Default path is `/dev/hidraw0`.
Returns
-------
rfidtag : str
String representation of numerical value of scanned rfidtag.
"""
hiddev = open(dev, "rb")
rfidtag = ''
continue_looping = True
k = 0
while continue_looping:
report = hiddev.read(10)
# print "k value: ", k
k += 1
for i in report:
j = ord(i)
# # print j
if j == 0:
# print "j = ", j
continue
if j == 0x1E:
rfidtag += '1'
# print "j = ", j
continue
elif j == 0x1F:
rfidtag += '2'
# print "j = ", j
continue
elif j == 0x20:
rfidtag += '3'
# print "j = ", j
continue
elif j == 0x21:
rfidtag += '4'
# print "j = ", j
continue
elif j == 0x22:
rfidtag += '5'
# print "j = ", j
continue
elif j == 0x23:
rfidtag += '6'
# print "j = ", j
continue
elif j == 0x24:
rfidtag += '7'
# print "j = ", j
continue
elif j == 0x25:
rfidtag += '8'
# print "j = ", j
continue
elif j == 0x26:
rfidtag += '9'
# print "j = ", j
continue
elif j == 0x27:
rfidtag += '0'
# print "j = ", j
continue
elif j == 0x28:
# print "j = ", j
# print rfidtag
hiddev.close()
continue_looping = False
break
else:
pass
# print "+++ Melon melon melon +++"
# print "j = ", j
# hiddev.close()
# continue_looping = False
# break
return rfidtag