From 3b2818f150f8e3d1d0880bf6a520a870f703f149 Mon Sep 17 00:00:00 2001 From: Elia Marcinkiewicz Date: Fri, 28 Jul 2023 22:07:51 +0200 Subject: [PATCH] add separate PN532 exception handling, print exception messages --- esp32/main.py | 6 ++++-- esp32/pn532.py | 25 ++++++++++++++----------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/esp32/main.py b/esp32/main.py index 87b2838..62a9721 100644 --- a/esp32/main.py +++ b/esp32/main.py @@ -1,4 +1,4 @@ -from pn532 import PN532Uart +from pn532 import PN532Uart, PN532Error import utime import machine import hashlib @@ -52,5 +52,7 @@ while rf is not None: uart.write("FS") utime.sleep(2) buf='' + except PN532Error as e: + print('PN532:', e) except Exception as e: - print('timeout!') + print(e) diff --git a/esp32/pn532.py b/esp32/pn532.py index c92d2c8..0b5c264 100644 --- a/esp32/pn532.py +++ b/esp32/pn532.py @@ -27,6 +27,9 @@ _FRAME_START = b'\x00\x00\xFF' # Codes _MIFARE_ISO14443A = const(0x00) +class PN532Error(RuntimeError): + pass + class PN532Uart(object): """ Class for interacting with the PN532 via the uart interface. @@ -49,7 +52,7 @@ class PN532Uart(object): continue if(utime.ticks_ms() >= end_time): - raise RuntimeError('No response from PN532!') + raise PN532Error('No response from PN532!') return self.uart.read(len) @@ -103,7 +106,7 @@ class PN532Uart(object): if self.debug: print('_write_frame: ACK: ', [hex(i) for i in ack]) if ack != _ACK: - raise RuntimeError('Did not receive expected ACK from PN532!') + raise PN532Error('Did not receive expected ACK from PN532!') def _read_frame(self): """ @@ -116,13 +119,13 @@ class PN532Uart(object): print('_read_frame: frame_start + header:', [hex(i) for i in response]) if len(response) < (len(_FRAME_START) + 2) or response[:-2] != _FRAME_START: - raise RuntimeError('Response does not begin with _FRAME_START!') + raise PN532Error('Response does not begin with _FRAME_START!') # Read the header (length & length checksum) and make sure they match. frame_len = response[-2] frame_checksum = response[-1] if (frame_len + frame_checksum) & 0xFF != 0: - raise RuntimeError('Response length checksum did not match length!') + raise PN532Error('Response length checksum did not match length!') # read the frame (data + data checksum + end frame) & validate data = self.wait_read_len(frame_len+2) @@ -132,10 +135,10 @@ class PN532Uart(object): checksum = sum(data) & 0xFF if checksum != 0: - raise RuntimeError('Response checksum did not match expected value: ', checksum) + raise PN532Error('Response checksum did not match expected value: ', checksum) if data[-1] != 0x00: - raise RuntimeError('Response does not include Frame End') + raise PN532Error('Response does not include Frame End') # Return frame data. return data[0:frame_len] @@ -156,10 +159,10 @@ class PN532Uart(object): response = self._read_frame() if len(response) < 2: - raise RuntimeError('Received smaller than expected frame') + raise PN532Error('Received smaller than expected frame') if not(response[0] == _PN532TOHOST and response[1] == (command+1)): - raise RuntimeError('Received unexpected command response!') + raise PN532Error('Received unexpected command response!') # Return response data. return response[2:] @@ -182,7 +185,7 @@ class PN532Uart(object): response = self.call_function(_COMMAND_GETFIRMWAREVERSION) if response is None: - raise RuntimeError('Failed to detect the PN532') + raise PN532Error('Failed to detect the PN532') return tuple(response) def read_passive_target(self, card_baud=_MIFARE_ISO14443A): @@ -198,9 +201,9 @@ class PN532Uart(object): # Check only 1 card with up to a 7 byte UID is present. if response[0] != 0x01: - raise RuntimeError('More than one card detected!') + raise PN532Error('More than one card detected!') if response[5] > 7: - raise RuntimeError('Found card with unexpectedly long UID!') + raise PN532Error('Found card with unexpectedly long UID!') # Return UID of card. return response[6:6+response[5]]