def in_data_exchange(self, data): """Sends a (Mifare) command to the currently active target. The "data" parameter contains the command data as a bytearray. Returns the data returned by the command (as a bytearray). Raises an IOError if the command failed. """ logging.debug("InDataExchange sending: " + " ".join("{0:02X}".format(k) for k in data)) logging.debug(data) frame = Pn532Frame( frame_type=PN532_FRAME_TYPE_DATA, data=bytearray([PN532_COMMAND_INDATAEXCHANGE, 0x01]) + data) self.send_command_check_ack(frame) response_frame = self.read_response() if response_frame.get_frame_type() == PN532_FRAME_TYPE_ERROR: raise IOError("InDataExchange failed (error frame returned)") response = response_frame.get_data() logging.debug("InDataExchange response: " + " ".join("{0:02X}".format(k) for k in response)) if response[1] != 0x00: # Only the status byte was returned. There was an error. if response[1] == 0x14: raise IOError("Mifare authentication failed") else: raise IOError( "InDataExchange returned error status: {0:#x}".format( response[1])) return response[2:]
def in_deselect(self): """Deselects the current target.""" logging.debug("InDeselect sending...") frame = Pn532Frame(frame_type=PN532_FRAME_TYPE_DATA, data=bytearray([PN532_COMMAND_INDESELECT, 0x01])) self.send_command_check_ack(frame) response_frame = self.read_response() if response_frame.get_frame_type() == PN532_FRAME_TYPE_ERROR: raise IOError("InDeselect failed (error frame returned)") response = response_frame.get_data() logging.debug("InDeselect response: " + " ".join("{0:02X}".format(k) for k in response)) if response[1] != 0x00: # Only the status byte was returned. There was an error. raise IOError("InDataExchange returned error status: {0:#x}".format(response[1]))
def set_max_retries(self, mx_rty_passive_activation): """Configure the PN532 for the number of retries attempted during the InListPassiveTarget operation (set to MIFARE_SAFE_RETRIES for a safe one-time check, set to MIFARE_WAIT_FOR_ENTRY so it waits until entry of a card). """ # We set MxRtyPassiveActivation to 5 because it turns out that one # try sometimes does not detect the card properly. frame = Pn532Frame(frame_type=PN532_FRAME_TYPE_DATA, data=bytearray([PN532_COMMAND_RFCONFIGURATION, PN532_RFCONFIGURATION_CFGITEM_MAXRETRIES, 0xFF, 0x01, mx_rty_passive_activation])) self.send_command_check_ack(frame) self.read_response()
def scan_field(self): """Scans the PN532's field for a Mifare card using the InListPassiveTarget operation. Returns the card's UID (a bytearray) if a card was in the field or False if no card was in the field. Only one card is detected at a time (the PN532 can handle two but this is not implemented here). False is never returned if the number of retries (see set_max_retries()) is set to MIFARE_WAIT_FOR_ENTRY. """ frame = Pn532Frame(frame_type=PN532_FRAME_TYPE_DATA, data=bytearray([PN532_COMMAND_INLISTPASSIVETARGET, 0x01, 0x00])) self.send_command_check_ack(frame) response = self.read_response().get_data() target_count = response[1] if not target_count: self._uid = False return False uid_length = response[6] self._uid = response[7:7 + uid_length] return self._uid