system
September 28, 2013, 1:04pm
#1
Hi, I have this code to read RFID tags, but the output is just a load of numbers and not like the proper ID that I get from another code; I’m using this code simply because I want to search an array for the ID and see if it’s registered.
Code:
#include <SoftwareSerial.h>
SoftwareSerial RFID = SoftwareSerial(10, 11);
char* RegisteredTagIDs[4] = {"", “”, “”, “”};
void setup(){
Serial.begin(9600);
RFID.begin(9600);
}
char CurrentChar;
String WholeCardID;
void loop(){
while(RFID.available() > 0){
if(WholeCardID.length() == 12){
Serial.println("Tag ID: " + WholeCardID);
for(int Index = 0; Index < 3; Index++){
if((String(RegisteredTagIDs[Index])) == WholeCardID){
Serial.println(“Tag Registered”);
}else{
Serial.println(“Tag NOT Registered”);
}
}
WholeCardID = “”;
}else{
WholeCardID = String(RFID.read());
Serial.println(WholeCardID);
}
}
}
system
September 28, 2013, 1:08pm
#2
char* RegisteredTagIDs[4] = {"", "", "", ""};
4 pointers to empty strings. How useful is that?
Ditch the String class. You KNOW how long a tag is. Create a char array of that length, and get rid of the crutches.
but the output is just a load of numbers and not like the proper ID that I get from another code
Where is that other code? What is a "load of numbers"? What is "the proper ID"?
Perhaps once you get rid of the String class, and learn to read, and compare, tags properly, your problem, whatever it is, will have resolved itself.
Converting the int read from the serial port to a String is completely wrong.
system
September 28, 2013, 1:15pm
#3
I know i didn't register any tags- that's not going to change how the id is read. could you run the code and see my problem please?
the other code is simply this:
char read;
void loop(){
read = rfid.read();
Serial.write(read);
}
But I want to collect the whole tag in a string first, so I can check if it's in RegisteredTagIDs.
system
September 28, 2013, 1:40pm
#4
could you run the code and see my problem please?
Sure. PM me, and I’ll send you an address where you can send your hardware.
But I want to collect the whole tag in a string first
And that’s what I suggested. So, why are you using String, instead. String and string are not the same thing.
A string is a NULL terminated array of chars. You don’t have any of them.
system
September 28, 2013, 1:46pm
#5
Stop getting stroppy with me.So why does this not work? It just sends a load of "y"s with dots above them.
#include <SoftwareSerial.h>
SoftwareSerial RFID = SoftwareSerial(10, 11);
void setup(){
Serial.begin(9600);
RFID.begin(9600);
}
char read;
String ID = “”; // String version of a single character
String WholeID = “”; // Whole ID of tag
void loop(){
if(ID.length() == 12){
Serial.print(ID);
WholeID = “”;
}else if(ID.length() < 12){
read = RFID.read();
WholeID = WholeID + ID.concat(read);
}
}
system
September 28, 2013, 1:55pm
#6
Stop getting stroppy with me.So why does this not work?
I'll consider answering that when you stop using the String class. It is totally unnecessary for storing fixed length data.
Just for giggles, suppose the reader sent '1', '2', '3' as the tag ID. Work out what this:
WholeID = WholeID + ID.concat(read);
is going to result in WholeID containing. If you get "123", you are wrong.
If you get "112123", please explain how that is useful.
And, please explain why you think that you can read data without knowing if there is any to read.
system
September 28, 2013, 2:48pm
#7
For the last time, you don't need to store any tag id's yet.
It would help if you could stop being cryptic and tell me how to store however many chars there are in a tag id, then check if an array contains it.
system
September 28, 2013, 2:57pm
#8
It would help if you could stop being cryptic and tell me how to store however many chars there are in a tag id, then check if an array contains it.
It’s so basic that I can’t see why you are having a problem. Clearly, you’ve jumped in over your head.
char tag[13];
// See if all 12 bytes have arrived
if(Serial.available() >= 12)
{
for(byte i=0; i<12; i++)
{
tag[i] = Serial.read();
}
tag[12] = '\0';
}
// At this point, tag is a NULL terminated array of chars that
// can be printed or compared using strcmp()
I’d suggest putting the attitude in a drawer, too.
system
September 28, 2013, 3:26pm
#9
#include <SoftwareSerial.h>
SoftwareSerial RFID = SoftwareSerial(10, 11);
char* RegisteredIDs = {"", “”, “”, “”};
void setup(){
Serial.begin(9600);
RFID.begin(9600);
}
String WholeID = “”;
void loop(){
char tag[13];
if(rfid.available() >= 12){
for(byte i=0; i<12; i++){
tag = rfid.read();
}*
tag[12] = ‘\0’;*
}*
for(int Index = 0; Index < (sizeof(RegisteredIDs) / sizeof(int)); Index++){*
if(strcmp(String(tag), RegisteredIDs[Index] == 0){*
Serial.println(“Tag ID: " + String(tag) + " – Access Granted”)*
}else{*
Serial.println(“Tag ID: " + String(tag) + " – Access Granted”)*
}*
}*
}
I tried this and got nothing except Tag ID: X X Y – Access Granted
system
September 28, 2013, 3:30pm
#10
I tried this and got nothing except Tag ID: X X Y -- Access Granted
I'm calling bullshit on that. That code won't even compile.
system
September 28, 2013, 4:19pm
#11
I'm calling bullshit on that. That code won't even compile.
Maybe without the italics.
OP, please use code tags when posting code.
system
September 28, 2013, 4:24pm
#12
Maybe without the italics.
No. The RegisteredIDs is no longer an array, and strcmp() does not take a String. There may be other issues, too.
system
September 28, 2013, 6:14pm
#13
Serial.println("Tag ID: " + String(tag) + " -- Access Granted")
}else{
Serial.println("Tag ID: " + String(tag) + " -- Access Granted")
}
}
}
I tried this and got nothing except Tag ID: X X Y -- Access Granted
On the other hand, had it compiled, it isn't surprising it only printed "Tag ID: X X Y -- Access Granted"
Rixterz:
Hi, I have this code to read RFID tags, but the output is just a load of numbers and not like the proper ID that I get from another code; I'm using this code simply because I want to search an array for the ID and see if it's registered.
How to post:
Read this before posting a programming question
How to use this forum
How to solve your problem:
zoomkat
September 29, 2013, 4:34am
#16
It would help if you could stop being cryptic and tell me how to store however many chars there are in a tag id, then check if an array contains it.
Probably ain't gonna happen. Code retentive issues. What does the received character string normally look like?