def init_rfid(): try: if debug: print "Initializing RFID object..." rfid = RFID() rfid.setOnErrorhandler(rfidError) rfid.setOnTagHandler(rfidTagGained) rfid.setOnTagLostHandler(rfidTagLost) rfid.openPhidget() if debug: print "RFID object initialized!" print "Waiting to attach RFID reader..." rfid.waitForAttach(10000) if debug: print "RFID reader attached!" rfid.setAntennaOn(True) return rfid except RuntimeError as e: print "Runtime error: %s stopping!" % e.details exit(1) except PhidgetException as e: print "Phidget error: %i, %s" % (e.code, e.details) exit(1)
class RFIDObject: def __init__(self): self.InitRFID() def InitRFID(self): try: self.RFIDEnabled=False self.rfid = RFID() self.rfid.setOnAttachHandler(self.RFIDAttached) self.rfid.setOnDetachHandler(self.RFIDDetached) self.rfid.setOnTagHandler(self.RFIDNewTag) self.rfid.openPhidget() print ('here') except RuntimeError as e: self.Log("Runtime Exception creating RFID: %s" % e.details) self.Log("Exiting....") exit(1) def RFIDAttached(self,e): self.RFIDEnabled=True print "Found RFID" self.rfid.setAntennaOn(True) def RFIDDetached(self,e): self.RFIDEnabled=False print "RFID Detached" def RFIDNewTag(self,e): self.RFIDButtonHit=True print "RFID tag %s"%e.tag
rfid.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: rfid.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) else: displayDeviceInfo() print("Turning on the RFID antenna....") rfid.setAntennaOn(True) print("Press Enter to quit....") chr = sys.stdin.read(1) # Write tag example: #try: # rfid.write("Some Tag", RFIDTagProtocol.PHIDGET_RFID_PROTOCOL_PHIDGETS) #except PhidgetException as e: # print("Phidget Exception %i: %s" % (e.code, e.details)) try: lastTag = rfid.getLastTag() print("Last Tag: %s" % (lastTag)) except PhidgetException as e:
rfid.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: rfid.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) else: displayDeviceInfo() print("Turning on the RFID antenna....") rfid.setAntennaOn(True) print("Press Enter to quit....") chr = sys.stdin.read(1) try: lastTag = rfid.getLastTag() print("Last Tag: %s" % (lastTag)) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Closing...") try: rfid.closePhidget()
class Reader: ''' Phidgets 125khz RFID Reader/Writer ''' def __init__(self): # Create an RFID object try: self.rfid = RFID() self.user_manager = UserManager() except RuntimeError as e: Logger.info("RFID: Phidget Runtime Exception: %s" % e.details) Logger.info("RFID: Phidget Exiting....") exit(1) #Main Phiget Program Code try: #logging example, uncomment to generate a log file #rfid.enableLogging(PhidgetLogLevel.PHIDGET_LOG_VERBOSE, "phidgetlog.log") self.rfid.setOnAttachHandler(self.rfidAttached) self.rfid.setOnDetachHandler(self.rfidDetached) self.rfid.setOnErrorhandler(self.rfidError) self.rfid.setOnOutputChangeHandler(self.rfidOutputChanged) self.rfid.setOnTagHandler(self.rfidTagGained) self.rfid.setOnTagLostHandler(self.rfidTagLost) except PhidgetException as e: Logger.exception("RFID: Phidget Exception %i: %s" % (e.code, e.details)) Logger.exception("RFID: Exiting....") exit(1) Logger.info("RFID: Opening phidget object....") try: self.rfid.openPhidget() except PhidgetException as e: Logger.info("RFID: Phidget Exception %i: %s" % (e.code, e.details)) Logger.info("RFID: Exiting....") exit(1) Logger.info("RFID: Waiting for attach....") try: self.rfid.waitForAttach(10000) except PhidgetException as e: Logger.exception("RFID: Phidget Exception %i: %s" % (e.code, e.details)) try: self.rfid.closePhidget() except PhidgetException as e: Logger.exception("RFID: Phidget Exception %i: %s" % (e.code, e.details)) Logger.exception("RFID: Exiting....") exit(1) Logger.exception("RFID: Exiting....") exit(1) else: self.displayDeviceInfo() Logger.info("RFID: Turning on the RFID antenna....") self.rfid.setAntennaOn(True) # Information Display Function def displayDeviceInfo(self): Logger.info("RFID: |------------|----------------------------------|--------------|------------|") Logger.info("RFID: |- Attached -|- Type -|- Serial No. -|- Version -|") Logger.info("RFID: |------------|----------------------------------|--------------|------------|") Logger.info("RFID: |- %8s -|- %30s -|- %10d -|- %8d -|" % (self.rfid.isAttached(), self.rfid.getDeviceName(), self.rfid.getSerialNum(), self.rfid.getDeviceVersion())) Logger.info("RFID: |------------|----------------------------------|--------------|------------|") Logger.info("RFID: Number of outputs: %i -- Antenna Status: %s -- Onboard LED Status: %s" % (self.rfid.getOutputCount(), self.rfid.getAntennaOn(), self.rfid.getLEDOn())) #Event Handler Callback Functions def rfidAttached(self, e): self.attached = e.device Logger.info("RFID: %i Attached!" % (self.attached.getSerialNum())) def rfidDetached(self, e): self.detached = e.device Logger.info("RFID: %i Detached!" % (self.detached.getSerialNum())) def rfidError(self, e): try: source = e.device Logger.exception("RFID: %i Phidget Error %i: %s" % (self.source.getSerialNum(), e.eCode, e.description)) except PhidgetException as e: Logger.exception(("RFID: Phidget Exception %i: %s" % (e.code, e.details))) def rfidOutputChanged(self, e): self.source = e.device Logger.info("RFID: %i Output %i State: %s" % (self.source.getSerialNum(), e.index, e.state)) def rfidTagGained(self, e): self.rfid.setLEDOn(1) Logger.info("RFID: Tag gained: {}".format(e.tag)) self.user_manager.tag_gained = e.tag # this sets up the UserManager.on_tag_gained() to be called def rfidTagLost(self, e): self.rfid.setLEDOn(0) Logger.info("RFID: Tag lost: {}".format(e.tag))