--- 2014/10/03更新------------ 感谢Michael Roland的建议。
我试图看到"真实"来自PN532的回复。 我改变了我的代码,所以我可以阅读回复:
uint16_t PN532::mifareultralight_IntoAuth (uint8_t *Password, uint8_t *PACK)
{
/* Prepare the command */
pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
pn532_packetbuffer[1] = 1; /* Card number */
pn532_packetbuffer[2] = MIFARE_U_CMD_PWD_AUTH; /* Xsys MH: Mifare Ultralight Read command = 0x1B */
memcpy (pn532_packetbuffer + 3, Password, 4); /* Password Payload
/* Send the command */
if (HAL(writeCommand)(pn532_packetbuffer, 7)) {
return 0xAA;
}
/* Read the response packet */
int16_t status = HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));
return status;
}
我使用默认密钥: uint8_t Password [4] = {0xFF,0xFF,0xFF,0xFF};
我得到了响应0xFFFC 现在我感到困惑,deafault PACK是0x0000,所以它不是PACK,如果它是NACK,为什么我得到一个2字节的NACK?
我做得对吗?
任何建议都很感谢!!
----发表于2014/09/22 -------------
我正在尝试使用pn532访问Ntag213的身份验证状态。 这是代码,我是如何做到的:
访问的子功能:
uint8_t PN532::mifareultralight_IntoAuth (uint8_t *Password, uint8_t *Response)
{
/* Prepare the command */
pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
pn532_packetbuffer[1] = 1; /* Card number */
pn532_packetbuffer[2] = MIFARE_U_CMD_PWD_AUTH; /* Xsys MH: Mifare Ultralight Read command = 0x1B */
memcpy (pn532_packetbuffer + 3, Password, 4); /* Password Payload
/* Send the command */
if (HAL(writeCommand)(pn532_packetbuffer, 7)) {
return 0xAA;
}
/* Read the response packet */
HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));
memcpy (Response, pn532_packetbuffer, 4);
// Return OK signal
return 0xFF;
}
以下是我如何使用子功能:
首先我检查了卡片是否存在,然后我读出配置页面以检查配置。
在第3步中,我发送带有INDATAEXCHANGE代码的PWD_AUTH命令。如果我获得了成功的0xFF,我就完成了整个过程。这意味着发送中没有错误。我收到了Ntag和Pn532的回复。
但是我检查了响应,它与我用INDATAEXCHANGE代码发送的datapayload是一样的。
Hello!
Found chip PN532
Firmware ver. 1.6
Waiting for an ISO14443A Card ...
This will try to read all the data in the Tag
If the Tag is ready, enter any key to start!
Found an ISO14443A card
UID Length: 7 bytes
UID Value: 0x4 0xEA 0xFC 0x2 0xD9 0x38 0x80
Seems to be a Mifare Ultralight tag (7 byte UID)
The data for the configuration pages before writing look like this:
0x4 0x0 0x0 0xFF
0x0 0x5 0x0 0x0
Send the PWD_AUTH command...
Success code: 0xFF
Response code: 0x40 0x1 0x1B 0xFF
End of the program, enter any key to restart it.
我以为我可以获得Pack或Nack,但似乎是,我没有从pn532获得任何东西。 那是多么幸福?如何改进代码以进入Authenticated状态?
感谢您的帮助。
主要代码如下:
// choose to SPI or I2C or HSU
#if 0
#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"
PN532SPI pn532spi(SPI, 10);
PN532 nfc(pn532spi);
#elif 0
#include <PN532_HSU.h>
#include <PN532.h>
PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);
#else
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
#endif
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello!");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
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();
Serial.println("Waiting for an ISO14443A Card ...");
Serial.println("This will try to read all the data in the Tag");
Serial.println("If the Tag is ready, enter any key to start!");
// Wait for user input before proceeding
while (!Serial.available());
while (Serial.available()) Serial.read();
}
void loop()
{
int i; // For for-loop
uint8_t success; // Flag to check if there was an error with the PN532
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)
bool Fail = false;
uint8_t data[4]; // Array to store block data during reads
// Keyb on NDEF and Mifare Classic should be the same
uint8_t Password[4] = {0xFF, 0xFF, 0xFF, 0xFF }; // using default password
uint8_t PACK[2] = {0x00, 0x00};
uint8_t Response[4] = {0,0,0,0};
// 1. Step: Get the Tag ID
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
// put your main code here, to run repeatedly:
if (success)
{
// Display some basic information about the card
Serial.println("");
Serial.println("Found an ISO14443A card");
Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print(" UID Value: ");
for (i=0;i<uidLength;i++)
{
Serial.print(" 0x");Serial.print(uid[i],HEX);
}
//nfc.PrintHex(uid, uidLength);
Serial.println("");
}
else
{
Serial.println("Can not read the Tag");
Fail = true;
}
if (uidLength == 7 && !Fail)
{
// We probably have a Mifare Ultralight card ...
Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
Serial.println("");
}
else
{
Serial.println("Ooops... Its not a 7-byte Tag");
Fail = true;
}
// 2. Step: Try to Read and show the Configuration in Page 0x29, 0x2A
if(!Fail)
{
Serial.println("The data for the configuration pages before writing look like this:");
//for page 0x29
success = nfc.mifareultralight_ReadPage (0x29, data);
if (success)
{
// Data seems to have been read ... spit it out
int j;
for(j=0;j<4;j++)
{
Serial.print(" 0x");Serial.print(data[j],HEX);
}
//nfc.PrintHexChar(data, 4);
Serial.println("");
}
else
{
Serial.println("Ooops ... unable to read the configuration page 1 !?");
Fail = true;
}
// for page 0x2A
success = nfc.mifareultralight_ReadPage (0x2A, data);
if (success)
{
// Data seems to have been read ... spit it out
int j;
for(j=0;j<4;j++)
{
Serial.print(" 0x");Serial.print(data[j],HEX);
}
//nfc.PrintHexChar(data, 4);
Serial.println("");
}
else
{
Serial.println("Ooops ... unable to read the configuration page 2 !?");
Fail = true;
}
}
// 3. Step: Use PWD_AUTH command to try to get a PAck
if (!Fail)
{
Serial.println("Send the PWD_AUTH command...");
success = nfc.mifareultralight_IntoAuth(Password, Response);
// If the success code is 0xFF, we can check the response
// print success code
Serial.print("Success code: 0x");Serial.println(success,HEX);
// print response code
Serial.print("Response code:");
int j;
for(j=0;j<4;j++)
{
Serial.print(" 0x");Serial.print(Response[j],HEX);
}
//nfc.PrintHexChar(data, 4);
Serial.println("");
}
Serial.println("End of the program, enter any key to restart it.");
// Wait for user input before proceeding
while (!Serial.available());
while (Serial.available()) Serial.read();
}
答案 0 :(得分:0)
我建议您尝试以下操作(请注意,您可能需要在检测到标记后从标记中读取页面,并且在发出该身份验证命令之前):
uint16_t PN532::mifareultralight_IntoAuth (uint8_t *Password, uint8_t *PACK)
{
/* Prepare the command */
pn532_packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;
pn532_packetbuffer[1] = 0x1B;
memcpy (pn532_packetbuffer + 2, Password, 4); /* Password Payload */
/* Send the command */
if (HAL(writeCommand)(pn532_packetbuffer, 6)) {
return 0xAA;
}
/* Read the response packet */
int16_t status = HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));
return status;
}
如果可行,PN532 MIFARE PCD模式可能不支持NTAG PWD_AUTH命令。</ p>