Hi Guys,
I am trying to write a small amount of data to a MIFARE Standard 1K RFiD Tag, using a Skytek M1 mini RFiD module.
In Amal Graafstra's book, RFiD toys, there is an example of achieving this, however the example implements a BASIC Stamp 2 Microprocessor. I am aiming to use an arduino instead, so have had a crack at using this as a basis and write the arduino code for this.
I have included a number of trace LED's into the breadboard in the hope I can use them as indicators of how far through the code I am getting when I execute the code.
When I put a card near the reader All the lights go on in order, but the value returned is response code "84" which according to the Skyetek M1 data sheet means unknown command.
Can anyone offer me any advice? Kind of stuck at the moment?
The code I have so far is as follows:
// File......RFiD Read/Write Test
// Version 1.0
// Author Ian Culverhouse
// Date 10th July 2009
// Purpose - To implement the SkyeTek M1 RFiD Module with the Arduino to read and write to a 13.56mhz RFiD IC
// Revision History -
// Imports --------------------------------------------------------------
#include <SoftwareSerial.h> // Initialise Software Serial
// I/O Definitions ------------------------------------------------------
#define txPin 5 // Software serial tx pin
#define rxPin 4 // Software serial rx pin
#define ledPin 13 // LED pin
#define writecmdLED 12 // write command LED pin
#define drainLED 11 // buffer clear LED pin
#define dataWrittenLED 10 // data written to tag LED
#define successWrite 9 // data from reader after write data
// Initialise the Software serial port ----------------------------------
SoftwareSerial mySerial(rxPin, txPin);
#define d_size 64 // create some space for receiving data from tag reader
#define d2_size 64
byte readerData[ d_size];
byte responseData[d2_size];
// Setup the Pins & Ports -----------------------------------------------
void setup() {
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT); // pin mode for the software serial rx pin
pinMode(txPin, OUTPUT); // pin mode for software serial tx pin
pinMode(ledPin, OUTPUT); // pin mode for the ledpin
pinMode(writecmdLED, OUTPUT);
pinMode(drainLED, OUTPUT);
pinMode(dataWrittenLED, OUTPUT);
pinMode(successWrite, OUTPUT);
mySerial.begin(9600); // set the data rate for the serial ports
Serial.begin(9600); // sets data rate for the hardware serial port
Serial.println("begin "); //prints the word begin to the hardware serial port as a trace that the system is working
digitalWrite(ledPin, HIGH); // Sets the ledpin to high, indicating the system is active
}
// Program code ----------------------------------------------------------
void loop(){
// first step is to query the reader for a tag. Send a 001400 command to it. If no tag is found then it will lopp back
// and try again. When a tag is found the code will store the entire response from the reader.
int in_count; // defines variable in_count
mySerial.print( 0x0d, BYTE); //send CR
mySerial.print("001400"); // Send read command to M1 mini
mySerial.print( 0x0d, BYTE); //send CR
delay(50);
byte ch = mySerial.read(); // get first byte of answer from reader
// if it was not a LF (0x0a), send read command again if it was,
// store all bytes until ending LF was received
if (ch == 0x0a) // if first byte received is a line feed enter if statement
{
ch = mySerial.read(); // read the first byte into the variable ch
in_count = 0; // set in_count to 0 - used as a counter for the array
while( (ch != 0x0a) && ( in_count < d_size))// while the byte being received is not a line feed and the eeprom is not full enter while loop
{
readerData[in_count] = ch; // the array dta is filled with the value of ch
in_count++; // in_count is increased by 1
ch = mySerial.read(); // the next character from the serial port is read in and ch is assigned the value
}
}
if (readerData[0] != '9') { // check to see if first byte of data is a 9. If it is then
// error code 94 has been sent from reader, indicating that there is no
// in range of reader, go back and try again.
// assuming that a tag has been found. issue write command to write data to the tag.
// start by sending write command
mySerial.print( 0x0d, BYTE); // Sends a CR
mySerial.print("4044"); // Write command
digitalWrite(writecmdLED, HIGH); // Set Write command LED to high
// Output tag type and ID data from buffer to reader
for ( int i = 0; i < in_count; i++) mySerial.print( readerData*, BYTE);*
// Turn Buffer drain LED on whilst draining, Drain buffer and clear variables - just in case, turn LED off
- digitalWrite(drainLED, HIGH);*
for ( int i = 0; i < in_count; i++) readerData = 0; // clears the readerData Array
* in_count = 0; // clears in_count and resets it to 0*
// Send block number to write to (00) and number of blocks to write (01)
* mySerial.print("0002");*
// Data to be written to the block is Amal and followed by a CR to end the request
* mySerial.print("416D616C"); // Sends data "AMAL" to the card*
* mySerial.print( 0x0d, BYTE); // CR*
* digitalWrite(dataWrittenLED, HIGH); // sets data written LED to high*
* //Serial.println("data written to card"); *
// Read the data from the reader and print to Serial (Hardware Port)
byte ch = mySerial.read(); // read the first byte into the variable ch
* in_count = 0; // set in_count to 0 - used as a counter for the array*
* //Serial.println("in_count zeroed");
while( in_count < 3 )// while the byte being received is not a line feed and the eeprom is not full enter while loop*
* {*
* //Serial.print("inside_while");
responseData[in_count] = ch; // the array dta is filled with the value of ch*
* in_count++; // in_count is increased by 1*
* ch = mySerial.read(); // the next character from the serial port is read in and ch is assigned the value*
* }*
for ( int i = 0; i < in_count; i++) Serial.print(responseData*, BYTE);
_ Serial.print(responseData[0]); // print a line feed on the hardware serial line*
* Serial.print(responseData[1]);*_
* digitalWrite(successWrite, HIGH);*
* }*
}