Improve example

- Call cardreader.connect on card insertion, cardreader.disconnect
  on card removal.
- It fixes bug #3.
This commit is contained in:
Santiago Gimeno
2013-07-06 22:03:22 +02:00
parent 3f2a1b5e4e
commit 1f59246876
2 changed files with 42 additions and 26 deletions

View File

@@ -37,23 +37,30 @@ Example
if (changes) {
if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
console.log("card removed");/* card removed */
} else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
console.log("card inserted");/* card inserted */
}
reader.disconnect(function(err) {
if (err) {
console.log(err);
} else {
console.log('Disconnected');
}
});
/* You can connect to a smart card */
} else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
console.log("card inserted");/* card inserted */
reader.connect(function(err, protocol) {
if (err) {
console.log(err);
} else {
console.log('Protocol(', this.name, '):', protocol);
/* And transmit data */
reader.transmit(new Buffer([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, 1, function(err, data) {
if (err) console.log(err);
else console.log('Data received', data);
})
if (err) {
console.log(err);
} else {
console.log('Data received', data);
}
});
}
});
}
}
});

View File

@@ -16,8 +16,29 @@ pcsc.on('reader', function(reader) {
if (changes) {
if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
console.log("card removed");/* card removed */
reader.disconnect(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(function(err, protocol) {
if (err) {
console.log(err);
} else {
console.log('Protocol(', this.name, '):', protocol);
reader.transmit(new Buffer([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, 1, function(err, data) {
if (err) {
console.log(err);
} else {
console.log('Data received', data);
}
});
}
});
}
}
});
@@ -25,18 +46,6 @@ pcsc.on('reader', function(reader) {
reader.on('end', function() {
console.log('Reader', this.name, 'removed');
});
reader.connect(function(err, protocol) {
if (err) {
console.log(err);
} else {
console.log('Protocol(', this.name, '):', protocol);
reader.transmit(new Buffer([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, 1, function(err, data) {
if (err) console.log(err);
else console.log('Data received', data);
})
}
});
});
pcsc.on('error', function(err) {