weblab-instrument
Version:
communication with instrument through usb
364 lines (335 loc) • 11.4 kB
JavaScript
/* eslint no-restricted-syntax: 0 */
const net = require('net');
const debug = require('debug');
const log = debug('base:log');
const usbDev = require('./devUsb.js');
const os = require('os');
const sysConstant = require('../sys/sysConstant.js');
function checkModel(dev, model, serialnum) {
const sysSupportType = sysConstant.supportType;
for (const supportType of sysSupportType) {
// console.log(`check supported type ${supportType}`)
if (dev.commandObj[supportType]) {
if (dev.commandObj[supportType].model.some(someModel => someModel === model)) {
dev.gdsType = supportType;
dev.gdsModel = model;
if (dev.gdsType === 'GPD3303' || dev.gdsType === 'GPDX303S' || dev.gdsType === 'AFG2200' || dev.gdsType === 'MFG22X0') {
dev.usb.serialNumber = serialnum.slice(3);
} else {
dev.usb.serialNumber = serialnum;
}
dev.maxChNum = dev.commandObj[supportType].maxChNum[model];
break;
}
}
}
}
function getIDN(dev, data) {
log(`receive data string = ${data}`);
if (!data.startsWith('GW')) {
if (!data.startsWith('LCR')) {
log('getIDN data error');
return false;
}
const [modelName, verNum, serialNum, manufacture] = data.split(',');
log(manufacture);
log(verNum);
checkModel(dev, modelName, serialNum);
} else {
const [manufacture, modelName, serialNum, verNum] = data.split(',');
log('---- check Model --------');
log(manufacture);
log(verNum);
log(modelName);
log(serialNum);
log('------------');
checkModel(dev, modelName, serialNum);
}
log(`gdsType=${dev.gdsType}`);
log(`gdsModel=${dev.gdsModel}`);
return true;
}
function checkDsoExist(dev, callback) {
let tcnt;
log('checkDsoExist');
log('write command to server');
dev.state.conn = 'query';
dev.cmdHandler = getIDN;
dev.handlerSelf = dev;
dev.state.setTimeout = true;
if (dev.usb.pid === 24577 || dev.usb.pid === '6001') {
tcnt = 4000;
} else {
tcnt = 2000;
}
dev.syncCallback = function syncCallback() {
if (dev.state.timeoutObj) {
clearTimeout(dev.state.timeoutObj);
}
if (dev.gdsType === '') {
callback('check device exist error');
return;
// dev.state.conn = 'query';
// dev.cmdHandler = getIDN;
// dev.handlerSelf = dev;
// dev.state.setTimeout = true;
// dev.write('*IDN?\r\n');
// dev.state.timeoutObj = setTimeout(() => {
// dev.state.timeoutCb();
// }, 5000);
}
if (dev.usb.pid !== 8963) {
dev.write('*CLS\r\n');
}
callback();
};
dev.state.timeoutCb = function timeoutCb() {
log('timeout');
dev.state.conn = 'timeout';
dev.state.timeoutObj = null;
dev.syncCallback();
};
dev.state.timeoutObj = setTimeout(() => {
dev.state.timeoutCb();
}, tcnt);
if (dev.usb.pid === 24577 || dev.usb.pid === '6001') {
if (dev.write('REMOTE\r\n')) {
setTimeout(() => {
dev.write('*IDN?\r\n');
}, 1000);
} else {
log('check device exist error: usb not ready');
clearTimeout(dev.state.timeoutObj);
callback('check device exist error');
}
} else if (dev.usb.pid === 8963 || dev.usb.pid === '2303' ||
dev.usb.pid === 29987 || dev.usb.pid === '7523') {
if (!dev.write('*IDN?\n')) {
log('check device exist error: usb not ready');
clearTimeout(dev.state.timeoutObj);
callback('check device exist error');
}
} else if (!dev.write('*IDN?\r\n')) {
log('check device exist error: usb not ready');
clearTimeout(dev.state.timeoutObj);
callback('check device exist error');
}
}
const Dev = function Dev() {
this.state = {
conn: 'disconnect',
currentCmd: '',
currentId: '',
setTimeout: false,
timeoutObj: {},
timeoutCb: function timeoutCbInit() {},
errCode: {
message: '',
type: '',
handler: function handlerInit() {},
},
};
this.interf = 'net';
this.gdsType = '';
this.gdsModel = '';
this.chNum = 0;
this.activeCh = '';
this.cmdHandler = getIDN;
this.handlerSelf = {};
this.syncCallback = function syncCallbackInit() {};
this.maxChNum = 0;
this.commandObj = {};
this.cmdSequence = [];
this.writeTimeoutObj = null;
this.asyncWrite = 'done';
this.queryBlock = false;
this.recData = [];
this.errHandler = function errHandlerInit() {};
this.write = function write(data) {
if (this.interf === 'usb') {
if (this.usb.write(data)) {
return true;
}
return false;
} else if (this.interf === 'net') {
this.net.socket.write(data);
return true;
}
log('error: interf not support');
return false;
};
this.dataHandler = function dataHandler(data) {
let cmdDone = false;
const self = this;
if (!this.cmdHandler) {
log(`dataHandler receive :${data},length= ${data.length}`);
log('cmdHandler not define');
return;
}
if (this.state.setTimeout) {
clearTimeout(this.state.timeoutObj);
}
if (this.queryBlock !== true) {
// Only for GPD 3303D
if (this.usb.pid === 24577 || this.usb.pid === '6001') {
if ((data[data.length - 1]) === 0x0a && (data.length === 1)) {
delete this.recData;
this.recData = [];
return;
} else if (data[data.length - 1] === 0x0d) {
data[data.length - 1] = 0x0a;
}
}
this.recData += data.slice();
if (data[data.length - 1] === 0x0a) {
if (this.usb.pid === 24577 || this.usb.pid === '6001') {
if (this.recData.includes('Invalid')) {
log('receive invalid string');
delete this.recData;
this.recData = [];
return;
}
}
if (this.cmdHandler(this.handlerSelf, this.recData, this.syncCallback)) {
cmdDone = true;
if (this.state.setTimeout) {
// if(this.state.conn!=='timeout'){
log('clearTimeout');
clearTimeout(this.state.timeoutObj);
// }
this.state.setTimeout = false;
}
if (typeof this.syncCallback === 'function') {
log('call callback');
this.syncCallback();
delete this.recData;
this.recData = [];
}
// Only for GPD 3303D
if (this.gdsType === 'GPD3303') {
this.cmdHandler = null;
}
}
}
} else {
if (os.platform() !== 'win32') {
self.usb.device.pause();
}
if (this.cmdHandler(this.handlerSelf, data, this.syncCallback)) {
cmdDone = true;
if (this.state.setTimeout) {
// if(this.state.conn!=='timeout'){
log('clearTimeout by queryBlock');
clearTimeout(this.state.timeoutObj);
// }
this.state.setTimeout = false;
}
if (typeof this.syncCallback === 'function') {
log('call callback by queryBlock');
try {
this.syncCallback();
} catch (e) {
log(e);
}
}
}
if (os.platform() !== 'win32') {
self.usb.device.resume();
}
}
if (!cmdDone) {
if (this.state.setTimeout) {
if (this.state.timeoutObj) {
clearTimeout(this.state.timeoutObj);
}
this.state.timeoutObj = setTimeout(() => {
log('dataHandler timeout');
self.state.timeoutCb();
}, 5000);
}
}
}.bind(this);
return this;
};
Dev.prototype.onSocketErr = function onSocketErr(cb) {
const self = this;
this.net.socket.on('error', (e) => {
log('onTcpConnect: connect error!');
self.state.conn = 'disconnect';
self.net.socket.end();
if (cb) {
cb(e.message);
}
});
};
Dev.prototype.tcpConnect = function tcpConnect(Callback) {
const self = this;
if (self.state.conn === 'connected') {
if (Callback) { Callback(); }
return;
}
this.net.socket = net.connect(this.net.port, this.net.host_addr, () => { // 'connect' listener
log('connected to server!');
// dsoObj.net.socket.setEncoding('utf8');
self.net.socket.setMaxListeners(0);
self.state.conn = 'connected';
self.interf = 'net';
// self.net.socket.on('data',self.net.dataHandler);
self.net.socket.on('data', self.dataHandler);
self.checkDsoExist(self, Callback);
// if(Callback)
// Callback();
});
this.net.socket.on('close', () => {
log('onTcpConnect: close!');
self.state.conn = 'disconnect';
// dsoObj.net.socket.destroy();
});
};
Dev.prototype.tcpDisconnect = function tcpDisconnect(Callback) {
const self = this;
this.net.socket.removeAllListeners('close');
this.net.socket.on('close', (e) => {
log('onTcpConnect: close!');
self.state.conn = 'disconnect';
if (Callback) {
Callback(e);
}
});
this.net.socket.end();
};
Dev.prototype.usbConnect = function usbConnect(Callback, dontCheckdev) {
const self = this;
usbDev.openUsb(this, (err) => {
if (err) {
Callback(err);
} else {
log('openUsb');
// self.usb.onData(self.dataHandler);
if (!dontCheckdev) {
checkDsoExist(self, Callback);
} else {
Callback();
}
}
});
};
Dev.prototype.usbDisconnect = function usbDisconnect(Callback) {
const self = this;
if (self.state.conn !== 'disconnected') {
usbDev.closeUsb(this, (err) => {
if (err) {
log(err);
}
self.state.conn = 'disconnected';
self.usb.device = null;
self.interf = 'empty';
if (Callback) {
Callback(err);
}
});
} else if (Callback) {
Callback();
}
};
exports.Dev = Dev;