-
Notifications
You must be signed in to change notification settings - Fork 1
/
RFID.py
executable file
·45 lines (37 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
41
42
43
44
#!/usr/bin/python2
import serial
import re, sys, signal, os, time, datetime
import serial.tools.list_ports
BITRATE = 19200
class RFID():
def get_ports(self):
com_ports = set()
try:
for port, desc, port_type in serial.tools.list_ports.comports():
#print port
com_ports.add(port)
except:
pass
return com_ports
def connect(self, port):
self.ser = serial.Serial(port, BITRATE, timeout=0)
def signal_handler(self, signal, frame):
print "Closing RFID reader"
if (self.ser != None):
self.ser.fser.close()
sys.exit(0)
def read_from_serial(self):
buffer = ''
rfidPattern = re.compile(b'[\W_]+')
signal.signal(signal.SIGINT, self.signal_handler)
while True:
# Read data from RFID reader
buffer = buffer + self.ser.read(self.ser.inWaiting())
if len(buffer) > 10:
#lines = buffer.split('\n')
last_received = buffer[:10]
# Clear buffer
buffer = ''
lines = ''
return last_received
time.sleep(0.1)