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 flush command handling and Windows PNP ID regex #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 29 additions & 5 deletions lib/g2core-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ class G2coreAPI extends EventEmitter {
this.emit('setupDone');

// Allow data to be sent. We'll start with 5 lines to fill the buffer.
this.linesRequested = 5;
this._resetLinesRequested();
this._sendLines();
});
}; // _completeConnection
Expand Down Expand Up @@ -467,7 +467,15 @@ class G2coreAPI extends EventEmitter {
this.emit('sentLine', lastLineSent);
}; // _sendLines


/**
* _resetLinesRequested - internal only
*/
_resetLinesRequested() {
this.linesRequested = 5;
}; // _resetLinesRequested


/**
* flush - empty the send buffer, without sending what's left
*/
Expand All @@ -479,7 +487,7 @@ class G2coreAPI extends EventEmitter {
this.lineBuffer.length = 0;

// Reset line requested
this.linesRequested = 5;
this._resetLinesRequested();

// Send a queue flush followed by an alarm clear
this._write('\x04'); // send the ^D
Expand Down Expand Up @@ -603,6 +611,15 @@ class G2coreAPI extends EventEmitter {
this.ignoredResponses++;
}
this._write(value);

// handle flush command
if(typeof value === 'string' && value.match(/%+/)) {
this._resetLinesRequested();
this.linesSent = 0;
this.ignoredResponses = 0;
this.lineBuffer.length = 0;
}

return;
}

Expand Down Expand Up @@ -1189,20 +1206,27 @@ class G2coreAPI extends EventEmitter {

for (let i = 0; i < results.length; i++) {
let item = results[i];
let x;

if (process.platform === 'win32') {
// Windows:
// pnpId: USB\VID_1D50&PID_606D&MI_00\6&3B3CEA53&0&0000
// pnpId: USB\VID_1D50&PID_606D&MI_02\6&3B3CEA53&0&0002
// pnpId: USB\VID_1D50&PID_606D\0084-D639-0084-08C6

// WARNING -- explicit test against VIP/PID combo.
if ((x = item.pnpId.match(/^USB\\VID_([0-9A-Fa-f]+)&PID_([0-9A-Fa-f]+)&MI_([0-9]+)\\(.*)$/)) && // eslint-disable-line
if ((x = item.pnpId.match(/^USB\\VID_([0-9A-Fa-f]+)&PID_([0-9A-Fa-f]+)(?:(?:&MI_([0-9]+)\\(.*))|(?:\\(.*)))$/)) && // eslint-disable-line
(x[1] == '1D50') && (x[2] == '606D')
) {
let serialNumber;
// let vendor = x[1]; // never used
// let pid = x[2]; // never used
let theRest = x[4].split('&');
let serialNumber = theRest[1];
if(x[4]) {
let theRest = x[4].split('&');
serialNumber = theRest[1];
} else {
serialNumber = x[5];
}

if (
(g2s.length > 0) &&
Expand Down