I am working on a little RFID project but I am having one tiny problem.
I have a RC522 RFID Module connected to an Arduino UNO and I want to be able to read TAGS and then compare the read ID to a string.
The ID will get stored into a byte array with the size of 4.
For instance...
byte readTAG[4];
will hold...
{ C3, 7D, DF, C7 }
I now want to take this array and convert it into a String such as...
"C37DDFC7"
This way...I could do operations such as
if(readTag == "12345678") {
// Do something...
}
How would I go about doing this?
Thanks in advance!
Here is my code:
#include <MFRC522.h>
#include <SPI.h>
int RST_PIN = 9;
int SDA_PIN = 10;
byte readCard[4];
MFRC522 mfrc522(SDA_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
}
void loop() {
ReadTAG();
}
// Lese TAG aus und gebe die ID im Serial Monitor aus.
void ReadTAG() {
// Wenn ein neuer TAG vorhanden ist UND erfolgreich gelesen werden konnte, dann Lese TAG aus.
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
for (int i = 0; i < 4; i++) {
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(readCard[i], HEX);
}
Serial.println();
// Wenn der TAG ausgelesen wurde dann stoppe das Lesen da er sonnst das gleiche TAG vielemale ausließt.
mfrc522.PICC_HaltA();
}
}
KenF:
What on earth would possess you to convert into a String? Have you got something against using strcmp() ?
I just can't figure out how to do it...
I changed the code to this.
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
for (int i = 0; i < 4; i++) {
readCard[ i ] = mfrc522.uid.uidByte[ i ];
}
readCard[4] = '\0';
Serial.println(readCard);
readCard is also a 'char' now.
But when I print readCard to Serial I get...
Ã}ßÇ
and when trying to use strcmp like this...
if (strcmp(readCard, "Ã}ßÇ") == 0) {
Serial.println("Test");
}
HazardsMind:
Not to mention, its not null terminated as you think it is.
Unless you make readCard have 5 elements: char readCard[5];
This will not work.
Sorry i forgot to post the change. I did increase the array to 5 to account for the termination. But why am I getting weird output when I print readCard?
This is only showing you what is in index 0, and not the full array (use a for loop), also the result you are seeing is the character based on value in index 0.
Add in HEX to see the hex value, DEC for the decimal value or BIN to see the binary value.
Serial.println(readCard, HEX);
for (int i = 0; i < 4; i++) {
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(readCard[i], HEX);
}
You have your values in byte arrays and AWOL has pointed you toward memcmp as the way perform conditional tests. Here is a small sketch which demonstrates memcmp.
Hello, I have a problem with my rfid reader that is ID-20LA to convert this array type code.
I want to change into string form. So how?
Tq
Here is my code:
byte tagID[12];
boolean tagread=false;
//configure arduino
void setup()
{
Serial.begin(9600);
Serial.println("Staff Attendance Using RFID starting...");
}
void loop()
{
if(Serial.available()>=13) //Make sure all the frame is received
{
if(Serial.read()==0x02) //Check for the start byte = 0x02
{
tagread=true; //New tag is read
for(int index=0;index<sizeof(tagID);index++)
{
byte val=Serial.read();
//convert from ASCII value to value range from 0 to 9 and A to F
if( val >= '0' && val <= '9')
val = val - '0';
else if( val >= 'A' && val <= 'F')
val = 10 + val - 'A';
if(tagread==true) //New tag is read
{
ReadFromTag(); //Display the tag ID
clear_tag(); //Clear the tag ID and ready for next tag
tagread=false;
}
delay(10);
}
All you need to do to turn the tagID array of chars into a C style string (lowercase s) all you need to do is to put a zero in the last position in the array but you MUST increase the size of the array in the declaration to allow space for it.
byte tagID[13];
for(int index=0;index<sizeof(tagID - 1);index++)
{
byte val=Serial.read();
//convert from ASCII value to value range from 0 to 9 and A to F
if( val >= '0' && val <= '9')
val = val - '0';
else if( val >= 'A' && val <= 'F')
val = 10 + val - 'A';
tagID[index]=val;
tagID[index + 1]='\0';
}
HazardsMind:
This is only showing you what is in index 0, and not the full array (use a for loop), also the result you are seeing is the character based on value in index 0.
Add in HEX to see the hex value, DEC for the decimal value or BIN to see the binary value.
Serial.println(readCard, HEX);
Actually, Serial.println() is not overloaded to accept a pointer to a byte array.
#include <MFRC522.h>
#include <SPI.h>
int RST_PIN = 9;
int SDA_PIN = 10;
byte readCard[4];
MFRC522 mfrc522(SDA_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
}
void loop() {
ReadTAG();
}
// Lese TAG aus und gebe die ID im Serial Monitor aus.
void ReadTAG() {
// Wenn ein neuer TAG vorhanden ist UND erfolgreich gelesen werden konnte, dann Lese TAG aus.
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
for (int i = 0; i < 4; i++) {
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(readCard[i], HEX);
}
Serial.println();
// Wenn der TAG ausgelesen wurde dann stoppe das Lesen da er sonnst das gleiche TAG vielemale ausließt.
mfrc522.PICC_HaltA();
}
}
whit this section:
"Assuming readTAG is filled with data:
Code: [Select]
{ 0xC3, 0x7D, 0xDF, 0xC7 }
(as in your first post), you can simply compare it to a number, like this:
Code: [Select]
if ( *(uint32_t*)readTAG == 0xC7DF7DC3 )"