๐Ÿ“ฃ๐Ÿ“ช ๐Ÿšš Adafruit will not be shipping USPS orders Monday January 20, 2020 ๐Ÿ—“. Expedited USPS orders placed after 11am ET ๐Ÿ•š Friday January 17 will go out Tuesday January 21 ๐Ÿ“ฃ๐Ÿ“ช๐Ÿšš
0

multi rfid reader sketch issue
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

multi rfid reader sketch issue

by chrisnet on Sun Jul 28, 2019 4:37 pm

I have in issue with the sketch not working the way I think it should!
Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK  (2)
#define PN532_MOSI (3)
#define PN532_SS   (4)
#define PN532_MISO (5)

#define PN532_SS2   (6)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ   (2)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield

// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a SPI connection:
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

Adafruit_PN532 nfc2(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS2);

// Use this line for a breakout with a hardware SPI connection.  Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
// also change #define in Adafruit_PN532.cpp library file
//   #define Serial SerialUSB
#endif

void setup(void) {
  Serial.begin(115200);
  Serial.println("Hello!");


  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.println("Didn't find PN53x board 1");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
    // configure board to read RFID tags
  nfc.SAMConfig();
 
  delay(1000);

  nfc2.begin();
  uint32_t versiondata2 = nfc2.getFirmwareVersion();
  if (! versiondata2) {
    Serial.println("Didn't find PN53x board 2");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata2>>24) & 0xFF, HEX);
  Serial.print("Firmware ver. "); Serial.print((versiondata2>>16) & 0xFF, DEC);
  Serial.print('.'); Serial.println((versiondata2>>8) & 0xFF, DEC);
 
  nfc2.SAMConfig();
 
  Serial.println("Waiting for an ISO14443A Card ...");
}


void loop(void) {
  ReadRFID1();
  ReadRFID2(); 
}

void ReadRFID1(){ 
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
   
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
 
  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);   
    if (uidLength == 4)
    {
      // We probably have a Mifare Classic card ...
      uint32_t cardid = uid[0];
      cardid <<= 8;
      cardid |= uid[1];
      cardid <<= 8;
      cardid |= uid[2]; 
      cardid <<= 8;
      cardid |= uid[3];
      Serial.print("Seems to be a Mifare Classic card #");
      Serial.println(cardid);
    }
    Serial.println("");
  }
  else{
     Serial.println("No card at reader 1");
  } 
}

void ReadRFID2(){
  //// Look at second reader
  uint8_t uid2[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength2;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  uint8_t success2;
  success2 = nfc2.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid2, &uidLength2);
 
  if (success2) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength2, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc2.PrintHex(uid2, uidLength2);   
    if (uidLength2 == 4)
    {
      // We probably have a Mifare Classic card ...
      uint32_t cardid = uid2[0];
      cardid <<= 8;
      cardid |= uid2[1];
      cardid <<= 8;
      cardid |= uid2[2]; 
      cardid <<= 8;
      cardid |= uid2[3];
      Serial.print("Seems to be a Mifare Classic card #");
      Serial.println(cardid);
    }
    Serial.println("");
  }
  else{
    Serial.println("No card at 2nd reader");
  }
}

IF
Code: Select all | TOGGLE FULL SIZE
  ReadRFID1();
  ReadRFID2();

Then it will read the tag if at ReadRFID1 but not ReadRFID2

IF
Code: Select all | TOGGLE FULL SIZE
  ReadRFID2();
  ReadRFID1();

it will read the tag if at ReadRFID2 but not ReadRFID1
for some resone, it is holding at the first Read and waiting there for a tag to be read how would I make this non-blocking?

chrisnet
 
Posts: 70
Joined: Mon Jun 04, 2012 11:05 am

Re: multi rfid reader sketch issue

by relaxing on Tue Jul 30, 2019 10:29 am

Looking through the library header... https://github.com/adafruit/Adafruit-PN ... it_PN532.h

...you can see the readPassiveTargetID() function has an optional timeout param that defaults to blocking:
Code: Select all | TOGGLE FULL SIZE
bool readPassiveTargetID(uint8_t cardbaudrate, uint8_t * uid, uint8_t * uidLength, uint16_t timeout = 0); //timeout 0 means no timeout - will block forever.


I think you want to add that timeout parameter to your calls with a reasonable value.

relaxing
 
Posts: 59
Joined: Thu Mar 24, 2005 11:22 am

Re: multi rfid reader sketch issue

by chrisnet on Tue Jul 30, 2019 10:58 am

Thanks

chrisnet
 
Posts: 70
Joined: Mon Jun 04, 2012 11:05 am

Please be positive and constructive with your questions and comments.