Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(windows): Parse more types of pnpIds #1288

Merged
merged 1 commit into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/bindings/win32-sn-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const PARSERS = [
/USB\\(?:.+)\\(.+)/,
/FTDIBUS\\(?:.+)\+(.+?)A?\\.+/
];

module.exports = function(pnpId) {
if (!pnpId) {
return null;
}
for (let index = 0; index < PARSERS.length; index++) {
const parser = PARSERS[index];
const sn = pnpId.match(parser);
if (sn) {
return sn[1];
}
}
return null;
};
8 changes: 5 additions & 3 deletions lib/bindings/win32.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
const binding = require('bindings')('serialport.node');
const BaseBinding = require('./base');
const promisify = require('../util').promisify;
const serialNumParser = require('./win32-sn-parser');

class WindowsBinding extends BaseBinding {
static list() {
return promisify(binding.list)().then(ports => {
// Grab the serial number from the pnp id
ports.forEach(port => {
if (port.pnpId) {
const parts = port.pnpId.match(/USB\\(.+)\\(.+)/);
if (!parts) { return }
port.serialNumber = parts.pop();
const serialNumber = serialNumParser(port.pnpId);
if (serialNumber) {
port.serialNumber = serialNumber;
}
}
});
return ports;
Expand Down
40 changes: 40 additions & 0 deletions test/bindings-win32-sn-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const serialNumParser = require('../lib/bindings/win32-sn-parser');

const devices = {
'FTDI Device': {
pnpId: 'FTDIBUS\\VID_0403+PID_6015+DO004ZB7A\\0000',
serialNumber: 'DO004ZB7'
},
'Arduino Mega': {
pnpId: 'USB\\VID_2341&PID_0042\\85531303630351C081D2',
serialNumber: '85531303630351C081D2'
},
'Atlas Scientific EZO-RGB Sensor': {
pnpId: 'FTDIBUS\\VID_0403+PID_6015+DJ1XJE67A\\0000',
serialNumber: 'DJ1XJE67'
},
'Gearmo FTDI2-LED USB RS-232 Serial Adapter': {
pnpId: 'FTDIBUS\\VID_0403+PID_6001+AL1WHZWFA\\0000',
serialNumber: 'AL1WHZWF'
},
'Arducam Nano V3.0 (Arduino Nano with FTDI)': {
pnpId: 'FTDIBUS\\VID_0403+PID_6001+A51MAMMEA\\0000',
serialNumber: 'A51MAMME'
},
'Pretend Device with an unknown pnp id': {
pnpId: 'WATEVER\\Whoever\\However!',
serialNumber: null
}
};

describe('serialNumParser', () => {
Object.keys(devices).forEach(device => {
it(`parses pnp id for ${device}`, () => {
const pnpId = devices[device].pnpId;
const serialNumber = devices[device].serialNumber;
assert.equal(serialNumParser(pnpId), serialNumber);
});
});
});