Skip to content

Instantly share code, notes, and snippets.

@stenius
Created January 20, 2013 00:05
Show Gist options
  • Save stenius/4575919 to your computer and use it in GitHub Desktop.
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.
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()
@stenius
Copy link
Author

stenius commented Jan 20, 2013

pySerial, get it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment