Change method for scanning for card from poll, which is OK for USB-connected PN532, but causes 100% CPU usage with SPI-connected PN532

This commit is contained in:
Ondrej Mikle 2018-05-06 01:43:43 +02:00
parent 9b89a230c9
commit 91c0d72de6
3 changed files with 16 additions and 15 deletions

View file

@ -127,8 +127,10 @@ class NFCScanner(object):
e = threading.Event() e = threading.Event()
e.wait(timeout=0.3) e.wait(timeout=0.3)
except NFCError, e: except NFCError, e:
#this exception happens also when scanUID times out #this exception happens also when scanUID finds no cards
logging.debug("Failed to wait for RFID card: %s", e) logging.debug("Failed to find RFID cards in reader's field: %s", e)
e = threading.Event()
e.wait(timeout=0.2)
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info("Exiting on keyboard interrupt") logging.info("Exiting on keyboard interrupt")
self.nfc.close() self.nfc.close()

View file

@ -104,19 +104,16 @@ std::string NFCDevice::scanUID() throw(NFCError)
throw NFCError("NFC device not opened"); throw NFCError("NFC device not opened");
} }
// We release GIL because otherwise it would block other threads. Since this polling is de-facto sleep and /* nfc_initiator_poll_target works fine for USB-connected PN532 (sleeps until card is available or timeout),
// doesn't touch any variables, it works as a language without GIL would. * but causes 100% CPU usage on SPI-connected PN532
Py_BEGIN_ALLOW_THREADS */
res = nfc_initiator_poll_target(_nfcDevice, _modulations, _modulationsLen, pollNr, pollPeriod, &nt); //res = nfc_initiator_poll_target(_nfcDevice, _modulations, _modulationsLen, pollNr, pollPeriod, &nt);
Py_END_ALLOW_THREADS res = nfc_initiator_list_passive_targets(_nfcDevice, _modulations[0], &nt, 1);
if (res < 0) { if (res < 0) {
throw NFCError("NFC polling error"); throw NFCError("NFC list passive targets error");
} } else if (res == 0) {
throw NFCError("No card in reader's field");
// we are not interested in non-ISO-14443A cards
if (nt.nm.nmt != NMT_ISO14443A) {
return string();
} }
const nfc_iso14443a_info& nai = nt.nti.nai; const nfc_iso14443a_info& nai = nt.nti.nai;

View file

@ -99,11 +99,13 @@ public:
virtual ~NFCDevice(); virtual ~NFCDevice();
/** /**
* Poll until a ISO14443A card is in the field and returns its UID. * Read UID of a card in field. If multiple cards are found, return UID of first one.
*
* If you are polling for cards with this, include some sleep in-between the calls (e.g. 0.1 sec).
* *
* @returns binary string containing UID or empty if non-supported card * @returns binary string containing UID or empty if non-supported card
* present in reader field * present in reader field
* @throws NFCError if polling failed * @throws NFCError if no cards in reader's field
*/ */
std::string scanUID() throw(NFCError); std::string scanUID() throw(NFCError);