Skip to content

Instantly share code, notes, and snippets.

@EvanSimpson
Created July 1, 2014 00:15
Show Gist options
  • Save EvanSimpson/62f5687baf19f183fb4b to your computer and use it in GitHub Desktop.
Save EvanSimpson/62f5687baf19f183fb4b to your computer and use it in GitHub Desktop.
Read the entire contents of a MIFARE Classic 1k card using a Tessel + RFID module.
var tessel = require('tessel');
var rfidLib = require('rfid-pn532');
var rfid = rfidLib.use(tessel.port['A'], { listen: false });
var addr = 0x00;
var auth_key = [0xff,0xff,0xff,0xff,0xff,0xff];
var auth_type = 0;
var cardData = [];
rfid.on('ready', function(version){
rfid.on('read', function(card){
console.log('Found card', card.uid.toString('hex'));
console.log('Reading addresses 1-64...');
authenticate(card);
});
function authenticate(card) {
rfid.mifareClassicAuthenticateBlock(card.uid,addr,auth_type,auth_key,function(err){
if (err){
console.log('Authentication error on block', addr);
console.log('Starting over at block', addr);
setTimeout(function(){rfid.startListening();}, 500);
} else{
readBlock(card);
}
});
}
function readBlock(card) {
// console.log('Reading block', addr);
rfid.mifareClassicReadBlock(addr, function(err, data){
if (err){
console.log('Read error!', err);
setTimeout(function(){rfid.startListening();}, 2000);
} else {
cardData[addr] = new Buffer(16);
data.copy(cardData[addr]);
addr++;
if (addr < 64){
authenticate(card);
} else {
prettyPrint(cardData);
}
}
});
}
});
function prettyPrint(buffArray){
for (var i=0; i<buffArray.length; i++){
console.log('Block',i,'\t', buffArray[i].toString('hex'));
}
}
rfid.on('error', function(error){
console.log('error', error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment