Society of Robots - Robot Forum
Electronics => Electronics => Topic started by: BANE on December 30, 2012, 07:11:18 PM
-
Hello all,
Im working on a automatic door lock and need a RFID reader. I've been looking at this one
https://www.sparkfun.com/products/8628 (https://www.sparkfun.com/products/8628)
and wondering how hard it would be to use it with the axon II? It only needs to read a card and turn on a relay; any ideas or thoughts?
Thanks!
-
Assuming you know how to speak serial (UART / TTL) on the Axon II, it should be easy.
You'll definitely want the version with the breakout board, btw!
-
Connect power to power, ground to ground, and two data pins to Rx and Tx. Done.
To send commands to it, just use rprintf() at the proper baud rate to the desired UART. Done.
Reading back from it is a little harder. In a FOR loop, store the data into an array of char values:
char data[whatever];
for loop:
data=uartGetByte(UART3);
-
Okay sorry for the delay but Ive finally got all my problems down to just reading in the bytes. So what I want it to do right now is store the tag number and then display it on my 20x4 lcd screen. Here is my code im using which just prints garbage to the screen. I think that Im not reading in the bytes correctly so the garbage is the random data from the blank array.
Any ideas?
Thanks in advance
update: also, ive hooked up TX from the ID-20 to my oscilloscope and it is outputing serial data when a card is read
#include "hardware.h"
// Initialise the hardware
void appInitHardware(void) {
initHardware();
}
// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
// -------- Start Sparkfun serLCD-------
//turn on LCD
pin_high(LCD_Relay);
delay_ms(300);
rprintfInit( displayGetWriter(&LCD));
displayBacklight(&LCD,TRUE);
// -------- End Sparkfun serLCD-------
return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
//////////Main Loop/////////////////////////////////////////
char tagString[13];
int index=0;
boolean reading = false;
//read from rfid
while(uartGetByte(ID20)!=-1){//do while data recieved non -1 data
int readByte=uartGetByte(ID20);//Assign byte read to a temp int
if(readByte == 2) reading = true; //begining of tag
if(readByte == 3) reading = false; //end of tag
if(reading && readByte != 2 && readByte != 10 && readByte != 13){//store the tag
tagString[index] = readByte;
index ++;
}
}
//display data
for (int i=0;i<12;i++){//display data along collumns
displayGoto(&LCD,i,0);//display position
rprintf("%c",tagString[i]);
}
//clear screen
delay_ms(1000);
displayClear(&LCD);
return 0;
}
-
You are reading in the bytes as 'int' but storing/printing them out as 'char'.
Change:
int readByte
to:
char readByte
and instead of:
(readByte == 2)
use:
(readByte == '2')
etc. etc.
or . . . you can just do this casting and it might just work with no other changes:
tagString[index] = (char)readByte;
-
Okay I've done the casting method and same results. I think my problem lays in being able to capture all the bytes. Right now my capture loop stays open when RX != -1 but perhaps that's not reliable or something. How can i do something similar to the "serial.available" in this code?
void loop(){
char tagString[13];
int index = 0;
boolean reading = false;
while(Serial.available()){
int readByte = Serial.read(); //read next available byte
if(readByte == 2) reading = true; //begining of tag
if(readByte == 3) reading = false; //end of tag
if(reading && readByte != 2 && readByte != 10 && readByte != 13){
//store the tag
tagString[index] = readByte;
index ++;
}
}
checkTag(tagString); //Check if it is a match
clearTag(tagString); //Clear the char of all value
resetReader(); //eset the RFID reader
}
-
I think part of your problem is that your code is trying to do too much at once, making it hard to narrow down where the bug is.
Split up your code into tiny parts, and verify each part one at a time.
Another problem I see with your code is that your index is reset to 0:
int index=0;
whenever uartGetByte(ID20)==-1
(ie it escapes the while loop, and starts at the beginning again)
As your code runs way faster than the UART, this will mean you keep overwriting tagString[0], while the rest of the index remains as random character garbage.
Change this line:
while(uartGetByte(ID20)!=-1){
to something like:
while(readByte != 13){
if(uartGetByte(ID20)!=-1)
{
//your other if statements here
}
(I'm assuming 13 means the RFID number has been completed)
-
Your problem is here:
//read from rfid
while(uartGetByte(ID20)!=-1){//do while data recieved non -1 data
int readByte=uartGetByte(ID20);//Assign byte read to a temp int
When the first uartGetByte gets something other than -1 you throw it away and then the second uartGetByte problem doesn't have the next character yet and so probably returns -1.
try something like:
int readByte;
while (readByte = uartGetByte(ID20), readByte !=-1){
// You've now got the data in readByte
}