向MFRC522库添加功能SetKeys

时间:2019-04-02 14:34:08

标签: c++ arduino

我尝试将功能SetKeys添加到我的MFRC522库中。 我无法理解函数MIFARE_Read不起作用...
我的标签是Mifare Classic 1k

bool MFRC522::MIFARE_SetKeys(byte *oldKeyA, byte *oldKeyB, byte *newKeyA, byte *newKeyB, byte sector){

    byte block_bufferRead[18];
    byte block_bufferWrite[16];
    byte sizeRead = sizeof(block_bufferRead);
    byte sizeWrite = sizeof(block_bufferWrite);
    byte block = (sector * 4) + 3;

    MFRC522::StatusCode status;
    MFRC522::MIFARE_Key key;

    for(int i=0; i<6; i++) key.keyByte[i] = oldKeyA[i];

    if(!MFRC522::PCD_Authenticate(PICC_CMD_MF_AUTH_KEY_A, block, &key, &uid)){
        Serial.println("PCD_Authenticate() failed - Block " + String(block));
        return false;
    }
    else{
        status = MIFARE_Read(block, block_bufferRead, &sizeRead);
        if(status != MFRC522::STATUS_OK){
            Serial.println("MIFARE_Read() failed. Block " + String(block));
            Serial.println(GetStatusCodeName(status));
            return false;
        }
        else{
            for(int i=0; i<6; i++) key.keyByte[i] = oldKeyB[i];
            if(!MFRC522::PCD_Authenticate(PICC_CMD_MF_AUTH_KEY_B, block, &key, &uid)){
                Serial.println("PCD_Authenticate() failed - Block " + String(block));
                return false;
            }
            else{
                for(byte i=0; i<16; i++) block_bufferWrite[i] = block_bufferRead[i];
                for(byte i=0; i<6; i++){
                    block_bufferWrite[i] = newKeyA[i];
                    block_bufferWrite[i+10] = newKeyB[i];
                }
                for(int i=0; i<6; i++) key.keyByte[i] = oldKeyB[i];
                status = MIFARE_Write(block, block_bufferWrite, &sizeWrite);
                if(status != MFRC522::STATUS_OK){
                    Serial.println("MIFARE_Write() failed.");
                    Serial.println(GetStatusCodeName(status));
                    return false;
                }
                else return true;
            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您似乎缺少该库的include标签。你需要类似的东西 #include "MFRC522.h"