How to communicate with a smartcard reader using node.js?

Hello there,

Probably you just acquired a Smart Card Reader and now you are looking for to handle the communication.

Smart Card Reader requires us to implement the CCID protocol. There’s an awesome lib for node.js that implements PC/SC which is actually a standard way to talk with smart cards. It’s called node-psclite.

To make it easier to get you started, just follow this instructions:

$ mkdir my-sample-app
$ npm init
$ npm install pcsclite
$ touch index.js

Now open index.js and paste the following code

// Open index.js and paste the follwing code. 

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);
        /* 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("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("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(Buffer.from([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.on('end', function() {
        console.log('Reader',  this.name, 'removed');
    });
});

pcsc.on('error', function(err) {
    console.log('PCSC error', err.message);
});

Now that you are all set, you just have to execute your app.

node index.js

Here’s a sample of my output while I was interacting with the reader

New reader detected ACS ACR1255U-J1 PICC Reader
Status( ACS ACR1255U-J1 PICC Reader ): { state: 18, atr: <Buffer > }
card removed
Disconnected
Status( ACS ACR1255U-J1 PICC Reader ): { state: 34,
  atr:
   <Buffer 3b 8f 80 01 ..... 03 00 01 00 00 00 00 6a> }
card inserted
Protocol( ACS ACR1255U-J1 PICC Reader ): 2
Status( ACS ACR1255U-J1 PICC Reader ): { state: 290,
  atr:
   <Buffer 3b 8f 80 01 ... 00 01 00 00 00 00 6a> }
Data received <Buffer 63 00>
Status( ACS ACR1255U-J1 PICC Reader ): { state: 18, atr: <Buffer > }
card removed
Disconnected
...

That’s all folks.

Hope you liked.

 

 

Useful links:

 

Leave a Reply

Close Menu