Skip to content

Instantly share code, notes, and snippets.

@jacky810124
Forked from tony1223/test.js
Created July 30, 2017 18:25
Show Gist options
  • Save jacky810124/247d510ea4369d323078176922b0fc3e to your computer and use it in GitHub Desktop.
Save jacky810124/247d510ea4369d323078176922b0fc3e to your computer and use it in GitHub Desktop.
var pcsc = require('pcsclite');
var pcsc = pcsc();
pcsc.on('reader', function(reader) {
console.log('New reader detected', reader.name);
reader.on('error', function(err) {
console.log('Error(', this.name, '):', err.message);
});
reader.on('status', function(status) {
console.log('Status(', this.name, '):', status);
console.log(status.atr.toString());
/* check what has changed */
var changes = this.state ^ status.state;
if (changes) {
if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
console.log("\n=== card removed");/* card removed */
reader.disconnect(reader.SCARD_LEAVE_CARD, function(err) {
if (err) {
console.log(err);
} else {
console.log('Disconnected');
}
});
} else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
console.log("\n=== card inserted");/* card inserted */
reader.connect({ share_mode : this.SCARD_SHARE_SHARED }, function(err, protocol) {
if (err) {
console.log(err);
} else {
console.log('Protocol(', reader.name, '):', protocol);
// reader.transmit(new Buffer([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, protocol, function(err, data) {
// if (err) {
// console.log(err);
// } else {
// console.log('Data received', data);
// // reader.close();
// // pcsc.close();
// }
// });
reader.transmit(new Buffer([0xFF, 0xCA, 0x00, 0x00, 0x00]), 40, protocol, function(err, data) {
if (err) {
console.log(err);
} else {
var hexs = [];
for(var i =0 ; i < data.length;++i){
if(data[i] == 0x90 && i < data.length-1 && data[i+1] == 0x00){
break;
}
hexs.push(data[i].toString(16));
}
hexs = hexs.reverse();
console.log('Data received',data.length, hexs.join(""),"tag id:",parseInt(hexs.join(""),16));
//感應到:D01ACD76 ,tag = 76CD1AD0, num= 1993153232
// reader.close();
// pcsc.close();
}
});
}
});
}
}
});
reader.on('end', function() {
console.log('Reader', this.name, 'removed');
});
});
pcsc.on('error', function(err) {
console.log('PCSC error', err.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment