使用PN532库为Arduino Uno写入Mifare Ultralight C.

时间:2014-03-18 13:28:26

标签: nfc rfid arduino-uno mifare contactless-smartcard

我使用示例代码从MIFARE Ultralight读取并写入MIFARE Classic,并在.h文件中定义:

#define PN532_RESPONSE_INDATAEXCHANGE       (0x41)
#define MIFARE_CMD_WRITE                    (0xA0)

但是当我运行以下代码时:

/**************************************************************************/
uint8_t PN532::mifareultralight_WritePage (uint8_t page, uint8_t *buffer1)
{
    /* Prepare the first command */
    pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
    pn532_packetbuffer[1] = 1;                      /* Card number */
    pn532_packetbuffer[2] = MIFARE_CMD_WRITE;       /* Mifare Write command = 0xA0 */
    pn532_packetbuffer[3] = page;                   /* Page Number (0..63 in most cases) */
    memcpy (pn532_packetbuffer + 4, buffer1, 4);        /* Data Payload */

    /* Send the command */
    if (HAL(writeCommand)(pn532_packetbuffer, 8)) {
        Serial.println(F("Go here 1"));
        return 0;
    }
    Serial.println(F("Go here 2"));
    /* Read the response packet */
    return (0 < HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)));
}

结果如下:

Scan a NFC tag

write:  4A 1 0
read:   4B 1 1 0 44 0 7 4 C1 37 CA 2C 2C 80
ATQA: 0x 44SAK: 0x 0
Writing Mifare Ultralight
write:  40 1 30 4
read:   41 0 2 0 0 10 0 6 1 10 11 FF 0 0 0 0 0 0
write:  40 1 30 3
read:   41 0 0 0 0 0 2 0 0 10 0 6 1 10 11 FF 0 0
Tag capacity 0 bytes
write:  40 1 A0 5 1 2 3 4
Go here 2
Write failed 0

它没有进入“Go here 1”,这意味着没有写入命令给读者,有谁知道为什么?

1 个答案:

答案 0 :(得分:2)

您正在使用的写命令似乎是错误的。您正在使用COMPATIBILITY_WRITE命令代码(0xA0),但您传递了WRITE命令的参数。

我建议您坚持使用WRITE命令:

+-----------+------+------+---------------------+
| WRAPPING  | CMD  | ADDR | DATA (1 PAGE)       |
+-----------+------+------+---------------------+
| 0x40 0x01 | 0xA2 | 0x05 | 0x01 0x02 0x03 0x04 |
+-----------+------+------+---------------------+

或者你也可以使用COMPATIBILITY_WRITE命令:

  1. 首先发送命令和地址:

    +-----------+------+------+
    | WRAPPING  | CMD  | ADDR |
    +-----------+------+------+
    | 0x40 0x01 | 0xA0 | 0x05 |
    +-----------+------+------+
    

    然后,您应该从标签收到ACK / NAK状态。

  2. 然后您在第二帧发送数据:

    +-----------+---------------------+------------------------------------------------------------+
    | WRAPPING  | DATA (1 PAGE)       | FILLER (3 EMPTY PAGES)
    +-----------+---------------------+------------------------------------------------------------+
    | 0x40 0x01 | 0x01 0x02 0x03 0x04 | 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
    +-----------+---------------------+------------------------------------------------------------+