Skip to content

Instantly share code, notes, and snippets.

@tony1223
Last active August 8, 2017 14:15
Show Gist options
  • Save tony1223/132e06f2c4b69b0b525f0ce9462fa109 to your computer and use it in GitHub Desktop.
Save tony1223/132e06f2c4b69b0b525f0ce9462fa109 to your computer and use it in GitHub Desktop.
var pcsc = require('pcsclite');
var iconv = require('iconv-lite');
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 */
reader.disconnect(reader.SCARD_LEAVE_CARD, function(err) {
if (err) {
console.log(err);
} else {
// console.log('== 已設定');
}
});
} else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
console.log("\n=== 讀取到卡片");/* 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);
}
if(data.length == 2){
//not mifare card;
return true;
}
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(' 悠遊卡感應結果 hex:'+ hexs.join(""),"tag id:",parseInt(hexs.join(""),16));
//感應到:D01ACD76 ,tag = 76CD1AD0, num= 1993153232
});
reader.transmit(new Buffer([0x00, 0xA4, 0x04, 0x00, 0x10, 0xD1, 0x58, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00 ]),40,protocol,(err,data)=>{
reader.transmit(new Buffer([0x00, 0xca, 0x11, 0x00, 0x02, 0x00, 0x00]), 59, protocol, function(err, data) {
if (err) {
console.log(err);
return true;
}
if(data.length == 2){
//not 健保卡;
return true;
}
var buffToString = function(buff){
for(var i = 0 ; i < buff.length;++i){
if(buff[i] == 0x00){
return iconv.decode(buff.slice(0,i), 'Big5');
}
}
return iconv.decode(buff, 'Big5');
};
var card_id = data.slice(0,12).toString(),
name = buffToString(data.slice(12,32)),
identify = data.slice(32,42).toString(),
birthday =
data.slice(42,45).toString()+"/"+
data.slice(45,47).toString()+"/"+
data.slice(47,49).toString(),
gender = data.slice(49,50).toString(),
publish_date =
data.slice(50,53).toString()+"/"+
data.slice(53,55).toString()+"/"+
data.slice(55,57).toString();
// console.log(' 健保卡 原始資料',data.length,data);
console.log(' 健保卡感應結果: \n'+JSON.stringify({
card_id:card_id.substring(0,4)+"******"+card_id.substring(10),
name,
identify: identify.substring(0,2)+"*****"+identify.substring(7),
birthday,gender, publish_date}));
// console.log("sam hex:"+ 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