-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfid.py
executable file
·40 lines (33 loc) · 1.03 KB
/
rfid.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
#!/usr/bin/env python
import os
import serial
import sys
import string
def writefile(data, filename, tmp_ext=".tmp"):
tmp = "%s%s" % (filename, tmp_ext)
with file(tmp, "wb") as f:
f.write(data)
os.rename(tmp, filename)
if __name__ == "__main__":
if len(sys.argv) < 3:
serial_device = "/dev/ttyUSB0"
outputfilename = "/tmp/monitor/rfid"
else:
serial_device = sys.argv[1]
outputfilename = sys.argv[1]
print "opening serial '%s'" % serial_device
serial = serial.Serial(serial_device, baudrate=9600)
code = ''
# make the directory if it doesn't exist
outputpath = os.path.dirname(outputfilename)
if not os.path.exists(outputpath):
os.makedirs(outputpath)
while True:
data = serial.read()
if data == '\r':
c = ''.join(s for s in code.strip() if s in string.printable)
print >> sys.stderr, len(c), c
writefile(c, outputfilename)
code = ''
else:
code += data