Improve example
- Call cardreader.connect on card insertion, cardreader.disconnect on card removal. - It fixes bug #3.
This commit is contained in:
35
README.md
35
README.md
@@ -37,26 +37,33 @@ Example
|
|||||||
if (changes) {
|
if (changes) {
|
||||||
if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
|
if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
|
||||||
console.log("card removed");/* card removed */
|
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)) {
|
} else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
|
||||||
console.log("card inserted");/* card inserted */
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/* You can connect to a smart card */
|
|
||||||
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);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
reader.on('end', function() {
|
reader.on('end', function() {
|
||||||
console.log('Reader', this.name, 'removed');
|
console.log('Reader', this.name, 'removed');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,8 +16,29 @@ pcsc.on('reader', function(reader) {
|
|||||||
if (changes) {
|
if (changes) {
|
||||||
if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
|
if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
|
||||||
console.log("card removed");/* card removed */
|
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)) {
|
} else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
|
||||||
console.log("card inserted");/* card inserted */
|
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() {
|
reader.on('end', function() {
|
||||||
console.log('Reader', this.name, 'removed');
|
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) {
|
pcsc.on('error', function(err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user