Whats going on guys!? I am trying to comprehend this RFID MFRC522 particularly with the read personal data example. In this example code, at the part of "print first and last names", could I convert them to decimal/word/int or a string? that data I would like to compare it with a definite variable. the code is : (excerpt from read personal data example MFRC522 library)
<'''
//---------------------------------------- GET LAST NAME
byte buffer2[18];
block = 1;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, buffer2, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//PRINT LAST NAME
for (uint8_t i = 0; i < 16; i++) {
Serial.write(buffer2[i] ); //Serial.print(buffer2[i], DEC);
}
'''/>
// I want to use that buffer2[i] to assign it into a variable and as well as compare it.
also how can I remove the "32"?
<'Serial.print(buffer2[i], DEC);'/>
has an output of
103117105991113232323232323232323232
buffer[2] is an array of bytes (unsigned char). If the first byte happens to be 'A' then write() will show you an 'A' and print() will show you the ascii value of 'A' which is 65.
The way to copy an array is with either strcpy() or memcpy().
If, instead of a string, this is a number, then you need to read a byte, shift the result by 8 bits and add in the byte for however many bytes your number contains. For a 32 bit number:
unsigned long number = 0;
for( int i = 0; i < 4; ++i ) {
number = number << 8;
number = number + buffer2[i];
}
Hi UKHeliBob, thank you for your reply. The reason why I wanted to convert the buffer2 to decimal or an integer so that I can transfer it thru modbus communication.
Using the MFRC522 library and the write personal data example. I was able to place the first and last name data in the RFID card. And using the read personal data example, I am able to read the first and last name of the RFID card. That buffer2[i] for clarification is already in ASCII word?
this is the full code :
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
//*****************************************************************************************//
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Read personal data on a MIFARE PICC:")); //shows in serial that it is ready to read
}
//*****************************************************************************************//
void loop() {
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
//some variables we need
byte block;
byte len;
MFRC522::StatusCode status;
//-------------------------------------------
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.println(F("**Card Detected:**"));
//-------------------------------------------
mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); //uncomment this to see all blocks in hex
//-------------------------------------------
Serial.print(F("Name: "));
byte buffer1[18];
block = 4;
len = 18;
//------------------------------------------- GET FIRST NAME
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, buffer1, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//PRINT FIRST NAME
for (uint8_t i = 0; i < 16; i++)
{
if (buffer1[i] != 32)
{
Serial.write(buffer1[i]);
}
}
Serial.print(" ");
//---------------------------------------- GET LAST NAME
byte buffer2[18];
block = 1;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, buffer2, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//PRINT LAST NAME
for (uint8_t i = 0; i < 16; i++) {
Serial.write(buffer2[i] );
}
//----------------------------------------
Serial.println(F("\n**End Reading**\n"));
delay(1000); //change value if you want to read cards faster
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
excerpt from the code :
//PRINT LAST NAME
for (uint8_t i = 0; i < 16; i++) {
Serial.write(buffer2[i] );
}
that buffer2[i] can I compare it to a particular string?
//PRINT LAST NAME
for (uint8_t i = 0; i < 16; i++)
{
Serial.write(buffer2[i] );
if (buffer2[i] == 'last name')
{
Serial.print(buffer2[i], DEC);
}
}
I know nothing about Modbus but I would be surprised of it could not be used to transfer a series of bytes such as those held by the buffer1 array
How is the name data being saved on the card ?
If it is written as a series of chars and terminated with '\0' then comparing it after reading it back will be simple
string comparison is done with strcmp() but that requires that your strings really be nul terminated, and I am guessing buffer2 is not. It is just a 16 byte block of memory.
It sounds like you need to google/learn a bit about C/C++.
That's exactly what it is, but it could be declared as an array of chars and the terminating NULL added as appropriate, hence my question as to how the data is stored to the card