Skip to content

Commit 0b554d7

Browse files
authored
fix(windows): Parse more types of pnpIds (#1288)
This should give us more serial numbers on windows. fixes #1220
1 parent 5662df0 commit 0b554d7

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

lib/bindings/win32-sn-parser.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const PARSERS = [
4+
/USB\\(?:.+)\\(.+)/,
5+
/FTDIBUS\\(?:.+)\+(.+?)A?\\.+/
6+
];
7+
8+
module.exports = function(pnpId) {
9+
if (!pnpId) {
10+
return null;
11+
}
12+
for (let index = 0; index < PARSERS.length; index++) {
13+
const parser = PARSERS[index];
14+
const sn = pnpId.match(parser);
15+
if (sn) {
16+
return sn[1];
17+
}
18+
}
19+
return null;
20+
};

lib/bindings/win32.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
const binding = require('bindings')('serialport.node');
33
const BaseBinding = require('./base');
44
const promisify = require('../util').promisify;
5+
const serialNumParser = require('./win32-sn-parser');
56

67
class WindowsBinding extends BaseBinding {
78
static list() {
89
return promisify(binding.list)().then(ports => {
910
// Grab the serial number from the pnp id
1011
ports.forEach(port => {
1112
if (port.pnpId) {
12-
const parts = port.pnpId.match(/USB\\(.+)\\(.+)/);
13-
if (!parts) { return }
14-
port.serialNumber = parts.pop();
13+
const serialNumber = serialNumParser(port.pnpId);
14+
if (serialNumber) {
15+
port.serialNumber = serialNumber;
16+
}
1517
}
1618
});
1719
return ports;

test/bindings-win32-sn-parser.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const serialNumParser = require('../lib/bindings/win32-sn-parser');
4+
5+
const devices = {
6+
'FTDI Device': {
7+
pnpId: 'FTDIBUS\\VID_0403+PID_6015+DO004ZB7A\\0000',
8+
serialNumber: 'DO004ZB7'
9+
},
10+
'Arduino Mega': {
11+
pnpId: 'USB\\VID_2341&PID_0042\\85531303630351C081D2',
12+
serialNumber: '85531303630351C081D2'
13+
},
14+
'Atlas Scientific EZO-RGB Sensor': {
15+
pnpId: 'FTDIBUS\\VID_0403+PID_6015+DJ1XJE67A\\0000',
16+
serialNumber: 'DJ1XJE67'
17+
},
18+
'Gearmo FTDI2-LED USB RS-232 Serial Adapter': {
19+
pnpId: 'FTDIBUS\\VID_0403+PID_6001+AL1WHZWFA\\0000',
20+
serialNumber: 'AL1WHZWF'
21+
},
22+
'Arducam Nano V3.0 (Arduino Nano with FTDI)': {
23+
pnpId: 'FTDIBUS\\VID_0403+PID_6001+A51MAMMEA\\0000',
24+
serialNumber: 'A51MAMME'
25+
},
26+
'Pretend Device with an unknown pnp id': {
27+
pnpId: 'WATEVER\\Whoever\\However!',
28+
serialNumber: null
29+
}
30+
};
31+
32+
describe('serialNumParser', () => {
33+
Object.keys(devices).forEach(device => {
34+
it(`parses pnp id for ${device}`, () => {
35+
const pnpId = devices[device].pnpId;
36+
const serialNumber = devices[device].serialNumber;
37+
assert.equal(serialNumParser(pnpId), serialNumber);
38+
});
39+
});
40+
});

0 commit comments

Comments
 (0)