mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-25 18:37:19 +00:00
fix(windows): Parse more types of pnpIds (#1288)
This should give us more serial numbers on windows. fixes #1220
This commit is contained in:
parent
5662df04b7
commit
0b554d7368
3 changed files with 65 additions and 3 deletions
20
lib/bindings/win32-sn-parser.js
Normal file
20
lib/bindings/win32-sn-parser.js
Normal file
|
|
@ -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;
|
||||||
|
};
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
const binding = require('bindings')('serialport.node');
|
const binding = require('bindings')('serialport.node');
|
||||||
const BaseBinding = require('./base');
|
const BaseBinding = require('./base');
|
||||||
const promisify = require('../util').promisify;
|
const promisify = require('../util').promisify;
|
||||||
|
const serialNumParser = require('./win32-sn-parser');
|
||||||
|
|
||||||
class WindowsBinding extends BaseBinding {
|
class WindowsBinding extends BaseBinding {
|
||||||
static list() {
|
static list() {
|
||||||
|
|
@ -9,9 +10,10 @@ class WindowsBinding extends BaseBinding {
|
||||||
// Grab the serial number from the pnp id
|
// Grab the serial number from the pnp id
|
||||||
ports.forEach(port => {
|
ports.forEach(port => {
|
||||||
if (port.pnpId) {
|
if (port.pnpId) {
|
||||||
const parts = port.pnpId.match(/USB\\(.+)\\(.+)/);
|
const serialNumber = serialNumParser(port.pnpId);
|
||||||
if (!parts) { return }
|
if (serialNumber) {
|
||||||
port.serialNumber = parts.pop();
|
port.serialNumber = serialNumber;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return ports;
|
return ports;
|
||||||
|
|
|
||||||
40
test/bindings-win32-sn-parser.js
Normal file
40
test/bindings-win32-sn-parser.js
Normal file
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Reference in a new issue