Skip to content

Instantly share code, notes, and snippets.

@VANITAX
Forked from tony1223/test.js
Created July 31, 2017 09:13
Show Gist options
  • Save VANITAX/f0ee72b657eb522d42ef8e1d2cc64af6 to your computer and use it in GitHub Desktop.
Save VANITAX/f0ee72b657eb522d42ef8e1d2cc64af6 to your computer and use it in GitHub Desktop.

Revisions

  1. @tony1223 tony1223 revised this gist Jul 30, 2017. 1 changed file with 76 additions and 16 deletions.
    92 changes: 76 additions & 16 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    var pcsc = require('pcsclite');
    var iconv = require('iconv-lite');


    var pcsc = pcsc();
    pcsc.on('reader', function(reader) {
    @@ -10,27 +12,27 @@ pcsc.on('reader', function(reader) {
    });

    reader.on('status', function(status) {
    console.log('Status(', this.name, '):', status);
    console.log(status.atr.toString());
    // 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 */
    console.log("\n=== 卡片已拔除");/* card removed */
    reader.disconnect(reader.SCARD_LEAVE_CARD, function(err) {
    if (err) {
    console.log(err);
    } else {
    console.log('Disconnected');
    // console.log('== 已設定');
    }
    });
    } else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
    console.log("\n=== card inserted");/* card inserted */
    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);
    // 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);
    @@ -45,25 +47,83 @@ pcsc.on('reader', function(reader) {
    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));
    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;
    }
    hexs = hexs.reverse();
    console.log('Data received',data.length, hexs.join(""),"tag id:",parseInt(hexs.join(""),16));

    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();
    }
    });
    });

    }
    });
    }
  2. @tony1223 tony1223 created this gist Jul 30, 2017.
    80 changes: 80 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    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);
    });