def poll_loop(self): """ Poll for presence of tag, read data, until stop() is called. """ # initialize music mixer pygame.mixer.init() # set default volume util.set_volume(settings.DEFAULT_VOLUME) while not self.do_stop: with self.mutex: # initialize tag state self.uid[0] = None self.data[0] = None # always create a new RFID interface instance, to clear any errors from previous operations rdr = RFID() # check for presence of tag err, _ = rdr.request() if not err: logger.debug("RFIDHandler poll_loop: Tag is present") # tag is present, get UID err, uid = rdr.anticoll() if not err: logger.debug(f"RFIDHandler poll_loop: Read UID: {uid}") # read data err, data = rdr.read(self.page) if not err: logger.debug( f"RFIDHandler poll_loop: Read tag data: {data}" ) # all good, store data to shared mem for i in range(5): self.uid[i] = uid[i] for i in range(16): self.data[i] = data[i] else: logger.debug( "RFIDHandler poll_loop: Error returned from read()" ) else: logger.debug( "RFIDHandler poll_loop: Error returned from anticoll()" ) # clean up rdr.cleanup() # act on data self.action() # wait a bit (this is in while loop, NOT in mutex env) time.sleep(self.sleep)
break else: block = block + 1 result = ascii_to_str(result) return result while True: print('Please type mode:') mode = input() rdr.wait_for_tag() (error, tag_type) = rdr.request() if not error: print("Tag detected") (error, uid) = rdr.anticoll() if not error: print("UID: " + str(uid)) if not rdr.select_tag(uid): if not rdr.card_auth(rdr.auth_a, 10, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], uid): if mode == 'r': print("Reading: " + read_from_rfid(NAME_BLOCK)) # Always stop crypto1 when done working rdr.stop_crypto() else: write_to_rfif('1221', NAME_BLOCK) rdr.stop_crypto() # Calls GPIO cleanup
def write(self, data): """ Write a 16-byte string of data to the tag """ if len(data) != 16: logger.debug(f"Illegal data length, expected 16, got {len(data)}") return False with self.mutex: rdr = RFID() success = False # check for presence of tag err, _ = rdr.request() if not err: logger.debug("RFIDHandler write: Tag is present") # tag is present, get UID err, uid = rdr.anticoll() if not err: logger.debug("RFIDHandler write: Read UID: " + str(uid)) # write data: RFID lib writes 16 bytes at a time, but for NTAG213 # only the first four are actually written err = False for i in range(4): page = self.page + i page_data = [ord(c) for c in data[4 * i:4 * i + 4]] + [0] * 12 # read data once (necessary for successful writing?) err_read, _ = rdr.read(page) if err: logger.debug( "Error signaled on reading page {:d} before writing" .format(page)) # write data err |= rdr.write(page, page_data) if err: logger.debug( f'Error signaled on writing page {page:d} with data {page_data:s}' ) if not err: logger.debug( "RFIDHandler write: successfully wrote tag data") success = True else: logger.debug( "RFIDHandler write: Error returned from write()") else: logger.debug( "RFIDHandler write: Error returned from anticoll()") # clean up rdr.cleanup() return success