Created
January 20, 2013 00:05
-
-
Save stenius/4575919 to your computer and use it in GitHub Desktop.
Basic loop for reading RFID tags. Filters out unwanted characters and ignores reads that happen back to back.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
import datetime | |
ser = serial.Serial('/dev/ttyUSB0', 2400, timeout=1) | |
count = 0 | |
read_time = datetime.datetime.now() | |
last_read = 'asdfasdf' #trash string so initial comparison fails | |
while True: | |
line = ser.readline() | |
if len(line) > 1: | |
line = line.replace('\n','').replace('\r','') | |
if line == last_read: | |
if datetime.datetime.now() - datetime.timedelta(seconds=2) < read_time: | |
continue | |
count += 1 | |
last_read = line | |
print { | |
'length': len(line), | |
'count': count, | |
'tag_id': line | |
} | |
read_time = datetime.datetime.now() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pySerial, get it!