/
magtek-reader.py
168 lines (131 loc) · 4.35 KB
/
magtek-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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/python
import hid
import time
import sys
import getopt
import os
import platform
import re
# Global Variables #
VENDOR_ID = 0x0801
PRODUCT_ID = 0x0003
DATA_SIZE = 338
enc_formats = ('ISO/ABA', 'AAMVA', 'CADL', 'Blank', 'Other', 'Undetermined', 'None')
vervose = False
output_file = False
last_status = ''
def process_data(data,last_status,vervose):
status = '0'
if(data[len(data)-1]):
if vervose:
print "Card In"
if bool(not data[0]) and bool(not data[6]):
card_data = ''.join(map(chr, data[7:116]))
if(last_status != card_data):
status = card_data
else:
if(last_status != '1'):
status = '1'
if vervose:
print "Card Encoding Type: %s" % enc_formats[data[6]]
print "Track 1 Decode Status: %r" % bool(not data[0])
print "Track 1 Data: %s" % ''.join(map(chr, data[7:116]))
elif(last_status != '0'):
if vervose:
print "Card Out"
status = '0'
return status
def read_hid_device():
global last_status
global vervose
while 1:
if output_file:
f = open(output_file,'w')
f.write('0')
f.close()
print 0
try:
h = hid.device(VENDOR_ID, PRODUCT_ID)
h.open(VENDOR_ID, PRODUCT_ID)
print "Device opened..."
### non-blocking mode
#h.set_nonblocking(1)
while 1:
d = h.read(338)
if d:
last_status = process_data(d,last_status,vervose)
if not vervose:
print last_status
if output_file:
f = open(output_file,'w')
f.write(last_status)
f.close()
time.sleep(0.05)
h.close()
except IOError, ex:
print "Searching for device..."
time.sleep(5)
def getargs(argv):
try:
opts, args = getopt.getopt(argv,"h:vo:",["ofile="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
if opt in ("-v", "--vervose"):
global vervose
vervose = True
elif opt in ("-o", "--ofile"):
global output_file
output_file = arg
def kill_old_processes(process_name,os_name):
### Terminates all processes with the same name ###
if os_name == 'Windows':
list_process = 'tasklist'
kill_process = "TASKKILL /PID %s /F"
pattern = '' + process_name + '\s*(\d+)'
else:
list_process = 'ps ax'
kill_process = "kill %s"
p_name = os.path.basename(os.path.normpath(process_name))
pattern = '\s*(\d+)\s[?a-zA-X0-9/\s+:]*python\s[/a-zA-Z-\\\\\s]*' + p_name
tasks = os.popen(list_process)
pids = []
for task in tasks:
m = re.match(pattern, task)
if m:
pids.insert(0,m.group(1))
for pid in pids[1:]:
os.system(kill_process % (pid,))
def usage():
print """
NAME
magtek-reader.py -- HID Magtek Card Reader
SYNOPSIS
magte-reader.py [-o output.txt] [-v] [-h]
DESCRIPTION
This program is a simple example to use the Magtek 99875205 Rev 13 card
reader (doc: https://www.magtek.com/docs/99875205.pdf), but may be used
by other HID compatible hardware.
Without -v (vervose) option, the outout shows a 0 if the device has not
detected any input, 1 if there was any error reading the card and the
card ID (on track 1) if the card has been read succesfully.
The options are as follows:
-o,--ofile output
Sends the output to a file. Sets a 0 if
the device has not detected any input, 1 if there was a reading
error and the card ID if the card has been read succesfully.
-v,--vervose
Produce a verbose output of all tracks and indicates whether the
card was inserted or removed.
-h,--help
Displays this help message.
GPL Jun 2015 GPL
"""
if __name__ == "__main__":
kill_old_processes(sys.argv[0],platform.system())
getargs(sys.argv[1:])
read_hid_device()