Hi everyone..
I'm New to Arduino ..I want to read first the RFID Card and then write to it using Serial but..when I do that with my code I must withdraw the card and pute it again..
Any help would appreciate it.. thanks
Hi everyone..
I'm New to Arduino ..I want to read first the RFID Card and then write to it using Serial but..when I do that with my code I must withdraw the card and pute it again..
Any help would appreciate it.. thanks
Welcome to the forum
Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE
Please post your sketch, using < CODE/ > tags when you do
Post your code and we might be able to help...
Hi , this is my working code ..
//https://www.youtube.com/arafamicrosystems
#include <SPI.h> //include the SPI bus library
#include <MFRC522.h> //include the RFID reader library
#define SS_PIN 10 //slave select pin
#define RST_PIN 9 //reset pin
MFRC522 mfrc522(SS_PIN, RST_PIN); // instatiate a MFRC522 reader object.
MFRC522::MIFARE_Key key; //create a MIFARE_Key struct named 'key', which will hold the card information
int soldblock = 38;
byte blockcontent[16] = { " " }; //an array with 16 bytes to be written into one of the 64 card blocks
String montant = "";
byte soldbackblock[18];
//The MIFARE_Read method requires a buffer that is at least 18 bytes to hold the 16 bytes of a block.
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card (in case you wonder what PCD means: proximity coupling device)
//Serial.println("Scan a MIFARE Classic card");
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF; //keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
}
}
void loop() {
// Look for new cards (in case you wonder what PICC means: proximity integrated circuit card)
if (!mfrc522.PICC_IsNewCardPresent()) {
// flag = 0;
return; //if it did not find a new card is returns a '0' and we return to the start of the loop
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) { //if PICC_ReadCardSerial returns 1, the "uid" struct (see MFRC522.h lines 238-45)) contains the ID of the read card.
return;
}
if (Serial.available() > 0) {
montant = Serial.readString();
montant.trim();
if (isValidData(montant)) {
for (byte i = 0; i < montant.length(); i++) {
blockcontent[i] = montant[i];
}
writeBlock(soldblock, blockcontent); //the blockcontent array is written into the card block
} else {
Serial.println("Invalid data format. Please input data up to 16 characters.");
}
}
readBlock(soldblock, soldbackblock);
name.trim();
String solde = "";
for (uint8_t i = 0; i < 16; i++) {
solde += (char)soldbackblock[i];
}
solde.trim();
Serial.print("{\"sold\":\"" + solde + "\"}");
delay(1000);
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
bool isValidData(String data) {
if (data.length() > 16) {
return false;
}
return true;
}
//read block function
int readBlock(int blockNumber, byte arrayAddress[]) {
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3; //determine trailer block for the sector
//authentication of the desired block for access
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed (read): ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3; //return "3" as error message
}
//it appears the authentication needs to be made before every block read/write within a specific sector.
//If a different sector is being authenticated access to the previous one is lost.
//reading a block
byte buffersize = 18; //we need to define a variable with the read buffer size, since the MIFARE_Read method below needs a pointer to the variable that contains the size...
status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize); //&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_read() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 4; //return "4" as error message
}
}
//write block function
int writeBlock(int blockNumber, byte arrayAddress[])
{
//this makes sure that we only write into data blocks. Every 4th block is a trailer block for the access/security info.
int largestModulo4Number=blockNumber/4*4;//(2/4)*4=0*4=0// (57/4)*4
int trailerBlock=largestModulo4Number+3;//determine trailer block for the sector
if (blockNumber > 2 && (blockNumber+1)%4 == 0)
{
Serial.print(blockNumber);
Serial.println(" is a trailer block:");
return 2;
}
//authentication of the desired block for access
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3;//return "3" as error message
}
//it appears the authentication needs to be made before every block read/write within a specific sector.
//If a different sector is being authenticated access to the previous one is lost.
//writing the block
status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);//valueBlockA is the block number, MIFARE_Write(block number (0-15), byte array containing 16 values, number of bytes in block (=16))
if (status != MFRC522::STATUS_OK)
{
Serial.print("MIFARE_Write() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 4;//return "4" as error message
}
// Serial.println("block was written");
}
the context is to read the sold block and then write to it but when proceeding with this code I must take the card and put it again...
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.