Skip to content

Instantly share code, notes, and snippets.

@swinton
Created November 18, 2010 16:56
Show Gist options
  • Select an option

  • Save swinton/705271 to your computer and use it in GitHub Desktop.

Select an option

Save swinton/705271 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import datetime, serial, subprocess, sys, time
DEVICE='/dev/cu.usbserial-A7006gOH'
IDS={
'28001D71E1': 'Ross',
'0800D9D1AB': 'Steve',
'2200562718': 'Edd',
'2500718C43': 'Danny',
}
def puts(msg):
sys.stdout.write(msg + "\n")
sys.stdout.flush()
def beep():
subprocess.call(["/bin/echo",chr(7)])
def rfid_reader(device=DEVICE, ids=None):
def setDTRHigh():
ser.setDTR(0)
beep()
def setDTRLow():
time.sleep(0.5)
ser.setDTR(1)
puts("Opening " + device)
ser=serial.Serial(device, 2400, timeout=1)
last_id=None
last_id_time=None
delta=datetime.timedelta(seconds=3)
try:
while ser.isOpen():
data=ser.readline().strip()
if not data:
continue
# Check for duplicates
now=datetime.datetime.now()
if last_id == data and (now - last_id_time < delta):
continue
# All good, record last id and timestamp
last_id=data
last_id_time=now
if not ids:
setDTRHigh()
yield data
setDTRLow()
else:
# Do mapping of ids onto values...
id=ids.get(data, False)
if id:
setDTRHigh()
yield id
setDTRLow()
except KeyboardInterrupt, e:
ser.close()
raise e
if __name__ == "__main__":
try:
for id in rfid_reader():
puts(id)
except KeyboardInterrupt, e:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment