Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Read/Write miFare classic 1K
var tessel = require('tessel')
, rfidLib = require('rfid-pn532')
, log = console.log
, rfid = rfidLib.use(tessel.port['A'], { listen: false })
var card = {}
, cursor = 0x00
, auth_key = [0xff,0xff,0xff,0xff,0xff,0xff]
, auth_type = 0
var new_data = [0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8]
rfid.on('ready', function(version){
rfid.on('read', function(card){
log(['Found card', card.uid.toString('hex')].join(' '))
card = card
read(card, function(err){
log('done reading !')
cursor = 0x00 // reset cursor
listen()
})
// write(card, 0x01, new_data)
})
})
function authenticate(card, cursor, callback){
rfid.mifareClassicAuthenticateBlock(card.uid, cursor, auth_type, auth_key, callback)
}
function read(card, callback){
callback = callback || function(){
cursor = 0x00 // reset cursor to 0
listen()
}
if(cursor == 0x00)
log('start reading ...')
authenticate(card, cursor, function(err){
if(err){
log('block '+ cursor +' : authentication error')
return callback(err)
}
rfid.mifareClassicReadBlock(cursor, function(err, data){
if(err){
log('block '+ cursor +' : read error')
return callback(err)
}
log('block ' + cursor + ' : ' + data.toString('hex'))
cursor++
cursor <= 0x3f //63
? read(card, callback)
: callback(err)
})
})
}
function write(card, cursor, data, callback){
callback = callback || function(){
listen()
}
authenticate(card, cursor, function(err){
if(err){
log('block '+ cursor+' : authentication error')
// log(err)
return callback(err)
}
rfid.mifareClassicwrite(cursor, data, function(err){
if(err){
log('block '+ cursor+' : write error')
// log(err)
return callback(err)
}
log('block ' + cursor + ' : write success')
callback(err)
})
})
}
function listen(){
log(' ')
log('--------------------')
log(' ')
log('waiting for card ...')
setTimeout(function(){rfid.startListening()}, 2000)
}
rfid.on('error', function(error){
log('error', error)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.