weblab-instrument
Version:
communication with instrument through usb
1,279 lines (1,222 loc) • 78 kB
JavaScript
/**
* Module use to communicate with GWINSTEK's DSO through Ethernet or USB
*
* @module instrument-com
*/
/* eslint no-restricted-syntax: 0 */
/* eslint no-await-in-loop: 0 */
import cmdObj from './sys/dso-command.json';
const EventEmitter = require('events').EventEmitter;
const debug = require('debug');
const log = debug('dso:log');
const sysConstant = require('./sys/sysConstant.js');
const syscmd = require('./dso/system.js');
const trigcmd = require('./dso/trigger.js');
const acqcmd = require('./dso/acquire.js');
const horcmd = require('./dso/horizontal.js');
const mathcmd = require('./dso/math.js');
const meascmd = require('./dso/measure.js');
const channel = require('./dso/channel.js');
const usbDev = require('./dev/devUsb.js');
const base = require('./dev/base.js');
function BindNetObj(dsoObj, port, hostAddr) {
dsoObj.dev.interf = 'net';
dsoObj.dev.net = {
dataHandler(data) {
// log('socket on data event!');
// socket idle when send query command
if (dsoObj.dev.state.setTimeout) {
if (dsoObj.dev.state.conn !== 'timeout') {
log('clearTimeout');
clearTimeout(dsoObj.dev.state.timeoutObj);
}
dsoObj.dev.state.setTimeout = false;
}
if ((dsoObj.dev.state.conn === 'query') || (dsoObj.dev.state.conn === 'timeout')) {
if (dsoObj.cmdHandler(dsoObj.handlerSelf, data, dsoObj.syncCallback) === true) {
if (dsoObj.syncCallback) {
log('call callback');
dsoObj.syncCallback();
}
}
}
},
port,
hostAddr,
socket: {},
};
}
/**
* Create all needed private properties and method
*
* @private
* @constructor DsoObject
*
* @return {Object} Private method used to control DSO
*/
function DsoObject() {
this.dev = new base.Dev();
// uitl.inherits(this.dev, base.Dev);
log(this);
// assign dso system command process method to dsoObj.sys
this.sys = syscmd.initSysObj.call(this, 'sys');
this.trig = trigcmd.initTrigObj.call(this, 'trig');
this.acq = acqcmd.initAcqObj.call(this, 'acq');
this.hor = horcmd.initHorObj.call(this, 'hor');
this.math = mathcmd.initMathObj.call(this, 'math');
this.meas1 = meascmd.initMeasObj.call(this, 'meas1');
this.meas2 = meascmd.initMeasObj.call(this, 'meas2');
this.meas3 = meascmd.initMeasObj.call(this, 'meas3');
this.meas4 = meascmd.initMeasObj.call(this, 'meas4');
this.meas5 = meascmd.initMeasObj.call(this, 'meas5');
this.meas6 = meascmd.initMeasObj.call(this, 'meas6');
this.meas7 = meascmd.initMeasObj.call(this, 'meas7');
this.meas8 = meascmd.initMeasObj.call(this, 'meas8');
this.ch1 = channel.initChanObj.call(this, 'ch1');
this.ch2 = channel.initChanObj.call(this, 'ch2');
this.ch3 = channel.initChanObj.call(this, 'ch3');
this.ch4 = channel.initChanObj.call(this, 'ch4');
this.cmdEvent = new EventEmitter();
this.commandObj = cmdObj;
this.dev.commandObj = this.commandObj;
this.dev.errHandler = function setToDefautl(self) {
log('Call errHandler');
if (self[self.dev.state.currentId].setToDefault) {
self[self.dev.state.currentId].setToDefault(self[self.dev.state.currentId]);
}
};
return this;
}
const availableNetDevice = [];
/**
* The class define all needed public properties and methods
*
* @class dsoctrl
*
*
*/
const initDsoCtrl = function initDsoCtrl(dsoObj) {
const dsoctrl = {};
/**
* The method belong to dsoctrl class used to release device's resource.
*
* @method closeDev
* @return {null} null
*
*/
dsoctrl.closeDev = function closeDev() {
log('closeDev');
return new Promise(((resolve, reject) => {
dsoctrl.disconnect()
.then(resolve)
.catch((e) => {
reject(e);
});
}));
};
/**
* The method belong to dsoctrl class used to connect to device,
* connect method must be called and wait to complete before any dsoctrl method.
*
* @method connect
*
*
*/
dsoctrl.connect = function connect(dontCheckdev) {
const self = this;
return new Promise(((resolve, reject) => {
function conn(e) {
if (e) {
reject(e);
} else {
resolve();
}
}
self.dev.usbConnect(conn, dontCheckdev);
// if (self.dev.interf === 'usb') {
// self.dev.usbConnect(conn);
// }else if (self.dev.interf === 'net') {
// self.dev.tcpConnect(conn);
// }
// else{
// reject(Error('Not supported interface'));
// }
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to disconnect from device.
*
* @method disconnect
*
*
*/
dsoctrl.disconnect = function disconnect() {
log('disconnect');
const self = this;
return new Promise(((resolve, reject) => {
function disconnectDone(e) {
if (e) {
log('disconnect return');
log(e);
// delete self.dev.usb;
reject(e);
} else {
// delete self.dev.usb;
resolve();
}
}
self.dev.asyncWrite = 'done';
self.dev.queryBlock = false;
self.dev.state.setTimeout = false;
if (self.dev.state.conn !== 'disconnected') {
self.dev.cmdSequence = [];
if (self.dev.writeTimeoutObj !== null) {
clearTimeout(self.dev.writeTimeoutObj);
}
self.dev.usbDisconnect(disconnectDone);
} else {
resolve();
}
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to sync all properties from device,
* like trigger type, channel state .. etc.
*
* @method syncConfig
*
*
*/
dsoctrl.syncConfig = function syncConfig() {
const self = this;
return new Promise(((resolve, reject) => {
function reload(e) {
if (e) {
reject(e);
} else {
resolve(self.sys.lrn);
}
}
const cmd = [
{ id: 'sys', prop: 'LRN', arg: '', cb: reload, method: 'get' },
];
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to load horizontal properties from device,
* like time division, position .. etc.
*
* @method getHorizontal
* @return {Object} horProperty
*
*/
/**
* Horizontal property of device.
*
* @property horProperty
* @type Object
* @param {String} position Specify the distance with triggered pointer of the main window
* @param {String} zposition Specify the distance with triggered pointer of the zoom window
* @param {String} scale Specify the time divison of the main window
* @param {String} zscale Specify the time divison of the zoom window
* @param {String} mode Specify which mode device on
* @param {String} expand Specify timebase expand by center or trigger position
*/
dsoctrl.getHorizontal = function getHorizontal(hor) {
const self = this;
return new Promise(((resolve, reject) => {
function rawData(e) {
if (e) {
reject(e);
} else {
resolve(self.hor);
}
}
let cmd = [];
if (hor === undefined) {
cmd = [
{ id: 'hor', prop: 'HorPosition', arg: '', cb: null, method: 'get' },
{ id: 'hor', prop: 'HorScale', arg: '', cb: null, method: 'get' },
{ id: 'hor', prop: 'HorMode', arg: '', cb: null, method: 'get' },
{ id: 'hor', prop: 'HorExpand', arg: '', cb: null, method: 'get' },
{ id: 'hor', prop: 'HorZoomPosition', arg: '', cb: null, method: 'get' },
{ id: 'hor', prop: 'HorZoomScale', arg: '', cb: rawData, method: 'get' },
];
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
if (hor.scale !== undefined) {
cmd.push({ id: 'hor', prop: 'HorScale', arg: hor.scale, cb: null, method: 'get' });
}
if (hor.zscale !== undefined) {
cmd.push({ id: 'hor', prop: 'HorZoomScale', arg: hor.zscale, cb: null, method: 'get' });
}
if (hor.position !== undefined) {
cmd.push({ id: 'hor', prop: 'HorPosition', arg: hor.position, cb: null, method: 'get' });
}
if (hor.zposition !== undefined) {
cmd.push({ id: 'hor', prop: 'HorZoomPosition', arg: hor.zposition, cb: null, method: 'get' });
}
if (hor.mode !== undefined) {
cmd.push({ id: 'hor', prop: 'HorMode', arg: hor.mode, cb: null, method: 'get' });
}
if (hor.expand !== undefined) {
cmd.push({ id: 'hor', prop: 'HorExpand', arg: hor.expand, cb: null, method: 'get' });
}
if (cmd.length > 0) {
cmd[cmd.length - 1].cb = rawData;
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
// log(self.dev.cmdSequence);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
log('setHorizontal do nothing');
resolve();
}
}
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to set horizontal properties to device,
* like time division, position .. etc.
*
* @method setHorizontal
* @param {Object} horProperty
*
*/
/**
* Horizontal property of device.
*
* @property horProperty
* @type Object
* @param {String} position Specify the distance with triggered pointer of the main window
* @param {String} zposition Specify the distance with triggered pointer of the zoom window
* @param {String} scale Specify the time divison of the main window
* @param {String} zscale Specify the time divison of the zoom window
* @param {String} mode Specify which mode device on
* @param {String} expand Specify timebase expand by center or trigger position
*/
dsoctrl.setHorizontal = function setHorizontal(hor) {
const self = this;
return new Promise(((resolve, reject) => {
const cmd = [];
function rawData(e) {
if (e) {
reject(e);
} else {
resolve();
}
}
if (hor === undefined) {
log('setHorizontal do nothing');
reject(['400', 'Parameter Error']);
return;
}
if (hor.scale !== undefined) {
cmd.push({ id: 'hor', prop: 'HorScale', arg: hor.scale, cb: null, method: 'set' });
}
if (hor.zscale !== undefined) {
cmd.push({ id: 'hor', prop: 'HorZoomScale', arg: hor.zscale, cb: null, method: 'set' });
}
if (hor.position !== undefined) {
cmd.push({ id: 'hor', prop: 'HorPosition', arg: hor.position, cb: null, method: 'set' });
}
if (hor.zposition !== undefined) {
cmd.push({ id: 'hor', prop: 'HorZoomPosition', arg: hor.zposition, cb: null, method: 'set' });
}
if (hor.mode !== undefined) {
cmd.push({ id: 'hor', prop: 'HorMode', arg: hor.mode, cb: null, method: 'set' });
}
if (hor.expand !== undefined) {
cmd.push({ id: 'hor', prop: 'HorExpand', arg: hor.expand, cb: null, method: 'set' });
}
if (cmd.length > 0) {
cmd[cmd.length - 1].cb = rawData;
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
// log(self.dev.cmdSequence);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
log('setHorizontal do nothing');
resolve();
}
}));
}.bind(dsoObj);
/**
*
*/
dsoctrl.fastSyncConfig = function fastSyncConfig() {
const self = this;
return new Promise(((resolve, reject) => {
function getDone(e) {
if (e) {
reject(e);
} else {
resolve({
chProps: {
ch1: {
scale: self.ch1.scale,
pos: self.ch1.position,
state: self.ch1.state,
},
ch2: {
scale: self.ch2.scale,
pos: self.ch2.position,
state: self.ch2.state,
},
ch3: {
scale: self.ch3.scale,
pos: self.ch3.position,
state: self.ch3.state,
},
ch4: {
scale: self.ch4.scale,
pos: self.ch4.position,
state: self.ch4.state,
},
},
trigProps: {
level: self.trig.level,
},
horProps: {
pos: self.hor.position,
scale: self.hor.scale,
},
});
}
}
const cmd = [];
let i;
const chID = ['ch1', 'ch2', 'ch3', 'ch4'];
log('fastSyncConfig');
for (i = 0; i < self.dev.maxChNum; i += 1) {
cmd.push({ id: chID[i], prop: 'VerSCALe', arg: '', cb: null, method: 'get' });
cmd.push({ id: chID[i], prop: 'VerPOSition', arg: '', cb: null, method: 'get' });
cmd.push({ id: chID[i], prop: 'ChState', arg: '', cb: null, method: 'get' });
}
cmd.push({ id: 'hor', prop: 'HorScale', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'hor', prop: 'HorPosition', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'acq', prop: 'AcqRecLength', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'trig', prop: 'TrigLevel', arg: '', cb: getDone, method: 'get' });
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
// log(self.dev.cmdSequence);
self.cmdEvent.emit('cmdWrite', cmd);
}));
}.bind(dsoObj);
/**
*
*/
dsoctrl.getSetup = function getSetup() {
const self = this;
return new Promise(((resolve, reject) => {
function getDone(e) {
if (e) {
reject(e);
} else {
resolve({
chProps: {
ch1: {
scale: self.ch1.scale,
pos: self.ch1.position,
state: self.ch1.state,
invert: self.ch1.invert,
coupling: self.ch1.coupling,
bandwidth: self.ch1.bandwidth,
probUnit: self.ch1.probe.unit,
probRatio: self.ch1.probe.atten,
},
ch2: {
scale: self.ch2.scale,
pos: self.ch2.position,
state: self.ch2.state,
invert: self.ch2.invert,
coupling: self.ch2.coupling,
bandwidth: self.ch2.bandwidth,
probUnit: self.ch2.probe.unit,
probRatio: self.ch2.probe.atten,
},
ch3: {
scale: self.ch3.scale,
pos: self.ch3.position,
state: self.ch3.state,
invert: self.ch3.invert,
coupling: self.ch3.coupling,
bandwidth: self.ch3.bandwidth,
probUnit: self.ch3.probe.unit,
probRatio: self.ch3.probe.atten,
},
ch4: {
scale: self.ch4.scale,
pos: self.ch4.position,
state: self.ch4.state,
invert: self.ch4.invert,
coupling: self.ch4.coupling,
bandwidth: self.ch4.bandwidth,
probUnit: self.ch4.probe.unit,
probRatio: self.ch4.probe.atten,
},
},
trigProps: {
type: self.trig.type,
holdoff: self.trig.holdoff,
level: self.trig.level,
source: self.trig.source,
mode: self.trig.mode,
edge: {
coupling: self.trig.edge.coupling,
slope: self.trig.edge.slope,
alt: self.trig.edge.alt,
},
},
horProps: {
pos: self.hor.position,
mode: self.hor.mode,
scale: self.hor.scale,
zscale: self.hor.zscale,
},
acqProps: {
mode: self.acq.mode,
length: self.acq.mem_length,
average: self.acq.average,
},
});
}
}
const cmd = [];
let i;
const chID = ['ch1', 'ch2', 'ch3', 'ch4'];
log('getSetup');
for (i = 0; i < self.dev.maxChNum; i += 1) {
cmd.push({ id: chID[i], prop: 'COUPling', arg: '', cb: null, method: 'get' });
cmd.push({ id: chID[i], prop: 'INVert', arg: '', cb: null, method: 'get' });
cmd.push({ id: chID[i], prop: 'BWLimit', arg: '', cb: null, method: 'get' });
cmd.push({ id: chID[i], prop: 'VerSCALe', arg: '', cb: null, method: 'get' });
cmd.push({ id: chID[i], prop: 'VerPOSition', arg: '', cb: null, method: 'get' });
cmd.push({ id: chID[i], prop: 'ChState', arg: '', cb: null, method: 'get' });
cmd.push({ id: chID[i], prop: 'PROBe_Type', arg: '', cb: null, method: 'get' });
cmd.push({ id: chID[i], prop: 'PROBe_RATio', arg: '', cb: null, method: 'get' });
}
cmd.push({ id: 'hor', prop: 'HorScale', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'hor', prop: 'HorPosition', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'hor', prop: 'HorMode', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'hor', prop: 'HorZoomScale', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'acq', prop: 'AcqMode', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'acq', prop: 'AcqAverage', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'acq', prop: 'AcqRecLength', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'trig', prop: 'TrigSource', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'trig', prop: 'TrigMode', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'trig', prop: 'TrigHoldoff', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'trig', prop: 'TrigLevel', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'trig', prop: 'TrigType', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'trig', prop: 'TrigEdgeSlope', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'trig', prop: 'TrigCouple', arg: '', cb: null, method: 'get' });
cmd.push({ id: 'trig', prop: 'TrigALT', arg: '', cb: getDone, method: 'get' });
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
// log(self.dev.cmdSequence);
self.cmdEvent.emit('cmdWrite', cmd);
}));
}.bind(dsoObj);
/**
*
*/
dsoctrl.setSetup = function setSetup(setup) {
const self = this;
return new Promise(((resolve, reject) => {
function setDone(e) {
if (e) {
log('dso.setSetup error');
log(e);
reject(e);
} else {
resolve();
}
}
const cmd = [];
let i;
const chID = ['ch1', 'ch2', 'ch3', 'ch4'];
log('dso setSetup');
log(setup);
cmd.push({ id: 'sys', prop: 'RST', arg: '', cb: null, method: 'set' });
for (i = 0; i < self.dev.maxChNum; i += 1) {
cmd.push({ id: chID[i], prop: 'COUPling', arg: setup.chProps[chID[i]].coupling, cb: null, method: 'set' });
cmd.push({ id: chID[i], prop: 'INVert', arg: setup.chProps[chID[i]].invert, cb: null, method: 'set' });
cmd.push({ id: chID[i], prop: 'BWLimit', arg: setup.chProps[chID[i]].bandwidth, cb: null, method: 'set' });
cmd.push({ id: chID[i], prop: 'VerSCALe', arg: setup.chProps[chID[i]].scale, cb: null, method: 'set' });
cmd.push({ id: chID[i], prop: 'VerPOSition', arg: setup.chProps[chID[i]].pos, cb: null, method: 'set' });
cmd.push({ id: chID[i], prop: 'ChState', arg: setup.chProps[chID[i]].state, cb: null, method: 'set' });
cmd.push({ id: chID[i], prop: 'PROBe_Type', arg: setup.chProps[chID[i]].probUnit, cb: null, method: 'set' });
cmd.push({ id: chID[i], prop: 'PROBe_RATio', arg: setup.chProps[chID[i]].probRatio, cb: null, method: 'set' });
}
cmd.push({ id: 'hor', prop: 'HorScale', arg: setup.horProps.scale, cb: null, method: 'set' });
cmd.push({ id: 'hor', prop: 'HorPosition', arg: setup.horProps.pos, cb: null, method: 'set' });
cmd.push({ id: 'hor', prop: 'HorMode', arg: setup.horProps.mode, cb: null, method: 'set' });
cmd.push({ id: 'hor', prop: 'HorZoomScale', arg: setup.horProps.zscale, cb: null, method: 'set' });
cmd.push({ id: 'acq', prop: 'AcqMode', arg: setup.acqProps.mode, cb: null, method: 'set' });
cmd.push({ id: 'acq', prop: 'AcqAverage', arg: setup.acqProps.average, cb: null, method: 'set' });
cmd.push({ id: 'acq', prop: 'AcqRecLength', arg: setup.acqProps.length, cb: null, method: 'set' });
cmd.push({ id: 'trig', prop: 'TrigSource', arg: setup.trigProps.source, cb: null, method: 'set' });
cmd.push({ id: 'trig', prop: 'TrigMode', arg: setup.trigProps.mode, cb: null, method: 'set' });
cmd.push({ id: 'trig', prop: 'TrigHoldoff', arg: setup.trigProps.holdoff, cb: null, method: 'set' });
cmd.push({ id: 'trig', prop: 'TrigLevel', arg: setup.trigProps.level, cb: null, method: 'set' });
cmd.push({ id: 'trig', prop: 'TrigType', arg: setup.trigProps.type, cb: null, method: 'set' });
cmd.push({ id: 'trig', prop: 'TrigEdgeSlope', arg: setup.trigProps.edge.slope, cb: null, method: 'set' });
cmd.push({ id: 'trig', prop: 'TrigCouple', arg: setup.trigProps.edge.coupling, cb: null, method: 'set' });
cmd.push({ id: 'trig', prop: 'TrigALT', arg: setup.trigProps.edge.alt, cb: setDone, method: 'set' });
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to load vertical properties from device,
* like scale, position .. etc.
*
* @method getVertical
* @param {String} ch Specify which channel wants to be loaded
* @return {Object} chProperty
*
*/
/**
* Channel property
*
* @property chProperty
* @type Object
* @param {String} coupling Specify coupling on AC,DC or GND
* @param {String} impedance Specify the impedance of the analog channel
* @param {String} invert
* @param {String} bandwidth
* @param {String} expand
* @param {String} state
* @param {String} scale
* @param {String} position
* @param {String} deskew
* @param {String} rawdata
* @param {String} probe.unit
* @param {String} probe.atten
*/
dsoctrl.getChannel = function getChannel(chProp) {
const self = this;
const chNum = sysConstant.chID[chProp.ch];
return new Promise(((resolve, reject) => {
function vetical(e) {
if (e) {
reject(e);
} else {
resolve(self[chProp.ch]);
}
}
let cmd = [];
log(`chNum =${chNum}`);
log(`maxChNum =${self.dev.maxChNum}`);
if (chNum === undefined) {
reject(['400', 'Parameter Error']);
return;
}
if (chNum >= self.dev.maxChNum) {
reject(['400', 'Parameter Error']);
return;
}
if (chProp.coupling !== undefined) {
cmd.push({ id: chProp.ch, prop: 'COUPling', arg: chProp.coupling, cb: null, method: 'get' });
}
if (chProp.impedance !== undefined) {
cmd.push({ id: chProp.ch, prop: 'IMPedance', arg: chProp.impedance, cb: null, method: 'get' });
}
if (chProp.invert !== undefined) {
cmd.push({ id: chProp.ch, prop: 'INVert', arg: chProp.invert, cb: null, method: 'get' });
}
if (chProp.bandwidth !== undefined) {
cmd.push({ id: chProp.ch, prop: 'BWLimit', arg: chProp.bandwidth, cb: null, method: 'get' });
}
if (chProp.expand !== undefined) {
cmd.push({ id: chProp.ch, prop: 'VerEXPand', arg: chProp.expand, cb: null, method: 'get' });
}
if (chProp.scale !== undefined) {
cmd.push({ id: chProp.ch, prop: 'VerSCALe', arg: chProp.scale, cb: null, method: 'get' });
}
if (chProp.position !== undefined) {
cmd.push({ id: chProp.ch, prop: 'VerPOSition', arg: chProp.position, cb: null, method: 'get' });
}
if (chProp.state !== undefined) {
cmd.push({ id: chProp.ch, prop: 'ChState', arg: chProp.state, cb: null, method: 'get' });
}
if (chProp.probe !== undefined) {
cmd.push({ id: chProp.ch, prop: 'PROBe_Type', arg: chProp.probe.unit, cb: null, method: 'get' });
cmd.push({ id: chProp.ch, prop: 'PROBe_RATio', arg: chProp.probe.atten, cb: null, method: 'get' });
}
if (chProp.deskew !== undefined) {
cmd.push({ id: chProp.ch, prop: 'DESKew', arg: chProp.probe.deskew, cb: null, method: 'get' });
}
if (cmd.length > 0) {
cmd[cmd.length - 1].cb = vetical;
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
cmd = [
{ id: chProp.ch, prop: 'COUPling', arg: '', cb: null, method: 'get' },
{ id: chProp.ch, prop: 'IMPedance', arg: '', cb: null, method: 'get' },
{ id: chProp.ch, prop: 'INVert', arg: '', cb: null, method: 'get' },
{ id: chProp.ch, prop: 'BWLimit', arg: '', cb: null, method: 'get' },
{ id: chProp.ch, prop: 'VerEXPand', arg: '', cb: null, method: 'get' },
{ id: chProp.ch, prop: 'VerSCALe', arg: '', cb: null, method: 'get' },
{ id: chProp.ch, prop: 'VerPOSition', arg: '', cb: null, method: 'get' },
{ id: chProp.ch, prop: 'ChState', arg: '', cb: null, method: 'get' },
{ id: chProp.ch, prop: 'PROBe_Type', arg: '', cb: null, method: 'get' },
{ id: chProp.ch, prop: 'PROBe_RATio', arg: '', cb: null, method: 'get' },
{ id: chProp.ch, prop: 'DESKew', arg: '', cb: vetical, method: 'get' },
];
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
}
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to setup vertical properties to device,
* like scale, position .. etc.
*
* @method setVertical
* @param {Object} chProperty Specify all channel parameter
*
*/
/**
* Channel property
*
* @property chProperty
* @type Object
* @param {String} coupling Specify coupling on AC,DC or GND
* @param {String} impedance Specify the impedance of the analog channel
* @param {String} invert
* @param {String} bandwidth
* @param {String} expand
* @param {String} scale
* @param {String} state
* @param {String} position
* @param {String} deskew
* @param {String} rawdata
* @param {String} probe.unit
* @param {String} probe.atten
*/
dsoctrl.setChannel = function setChannel(chProp) {
const self = this;
const chNum = sysConstant.chID[chProp.ch];
const cmd = [];
return new Promise(((resolve, reject) => {
function vertical(e) {
if (e) {
reject(e);
} else {
resolve(self[chProp.ch]);
}
}
log(`chNum =${chNum}`);
log(`maxChNum =${self.dev.maxChNum}`);
log(chProp);
if (chNum === undefined) {
reject(['400', 'Parameter Error']);
return;
}
if (chNum >= self.dev.maxChNum) {
reject(['400', 'Parameter Error']);
return;
}
if (chProp.coupling !== undefined) {
cmd.push({ id: chProp.ch, prop: 'COUPling', arg: chProp.coupling, cb: null, method: 'set' });
}
if (chProp.impedance !== undefined) {
cmd.push({ id: chProp.ch, prop: 'IMPedance', arg: chProp.impedance, cb: null, method: 'set' });
}
if (chProp.invert !== undefined) {
cmd.push({ id: chProp.ch, prop: 'INVert', arg: chProp.invert, cb: null, method: 'set' });
}
if (chProp.bandwidth !== undefined) {
cmd.push({ id: chProp.ch, prop: 'BWLimit', arg: chProp.bandwidth, cb: null, method: 'set' });
}
if (chProp.expand !== undefined) {
cmd.push({ id: chProp.ch, prop: 'VerEXPand', arg: chProp.expand, cb: null, method: 'set' });
}
if (chProp.scale !== undefined) {
cmd.push({ id: chProp.ch, prop: 'VerSCALe', arg: chProp.scale, cb: null, method: 'set' });
}
if (chProp.position !== undefined) {
cmd.push({ id: chProp.ch, prop: 'VerPOSition', arg: chProp.position, cb: null, method: 'set' });
}
if (chProp.state !== undefined) {
cmd.push({ id: chProp.ch, prop: 'ChState', arg: chProp.state, cb: null, method: 'set' });
}
if (chProp.probe !== undefined) {
if (chProp.probe.unit !== undefined) {
cmd.push({ id: chProp.ch, prop: 'PROBe_Type', arg: chProp.probe.unit, cb: null, method: 'set' });
}
if (chProp.probe.atten !== undefined) {
cmd.push({ id: chProp.ch, prop: 'PROBe_RATio', arg: chProp.probe.atten, cb: null, method: 'set' });
}
}
if (chProp.deskew !== undefined) {
cmd.push({ id: chProp.ch, prop: 'DESKew', arg: chProp.probe.deskew, cb: null, method: 'set' });
}
if (cmd.length > 0) {
cmd[cmd.length - 1].cb = vertical;
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
log('setVertical do nothing');
resolve();
}
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to turn selected channel on
*
* @method getVertical
* @param {String} ch Specify which channel wants to turn on
*
*
*/
dsoctrl.enableCh = function enableCh(ch) {
const self = this;
return new Promise(((resolve, reject) => {
function chstate(e) {
if (e) {
reject(e);
} else {
resolve();
}
}
const chid = ch.toLowerCase();
if ((chid === 'ch1') || (chid === 'ch2') || (chid === 'ch3') || (chid === 'ch4')) {
const cmd = [
{ id: chid, prop: 'ChState', arg: 'ON', cb: chstate, method: 'set' },
];
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
reject(Error('parameter error'));
}
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to turn selected channel off
*
* @method getVertical
* @param {String} ch Specify which channel wants to turn off
*
*
*/
dsoctrl.disableCh = function disableCh(ch) {
const self = this;
return new Promise(((resolve, reject) => {
function chstate(e) {
if (e) {
reject(e);
} else {
resolve();
}
}
log('disableCh cmd');
log(ch);
const chid = ch.toLowerCase();
log('disableCh cmd toLowerCase');
log(chid);
if ((chid === 'ch1') || (chid === 'ch2') || (chid === 'ch3') || (chid === 'ch4')) {
const cmd = [
{ id: chid, prop: 'ChState', arg: 'OFF', cb: chstate, method: 'set' },
];
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
reject(Error('parameter error'));
}
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to get the current screen from device,
*
* @method getSnapshot
* @return {Buffer} dsiplay data buffer
*
*/
dsoctrl.screen = function screen() {
const self = this;
return new Promise(((resolve, reject) => {
function snapshot(e) {
if (e) {
reject(e);
} else {
resolve(self.sys.dispData.slice(self.sys.headerlen, self.sys.dataValidLen));
}
}
const cmd = [
{ id: 'sys', prop: 'DispOut', arg: 'OFF', cb: snapshot, method: 'get' },
];
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to get the current screen from device,
*
* @method getSnapshot
* @return {Buffer} dsiplay data buffer
*
*/
dsoctrl.image = function image() {
const self = this;
return new Promise(((resolve, reject) => {
function getDone(e) {
if (e) {
reject(e);
} else {
resolve(self.sys.image);
}
}
const cmd = [
{ id: 'sys', prop: 'PngOutput', arg: '', cb: getDone, method: 'get' },
];
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to get the data in acquisition memory for
* the selected channel form device
*
* @method getRawdata
* @param {String} ch Specify which channel wants to be loaded
* @return {Buffer} rawdata buffer
*
*/
dsoctrl.getRawdata = function getRawdata(ch) {
const self = this;
return new Promise(((resolve, reject) => {
function rawData(e) {
if (e) {
log('getRawdata fail');
reject(e);
} else {
log('getRawdata done');
resolve(self[ch].rawdata);
}
}
if (sysConstant.chID[ch] !== undefined) {
const cmd = [
{ id: 'acq', prop: 'AcqHeader', arg: 'OFF', cb: null, method: 'set' },
{ id: ch, prop: 'AcqMemory', arg: '', cb: rawData, method: 'get' },
];
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
reject(Error('error null parameter'));
}
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to get acquisition setting
*
* @method getAcquire
* @return {Buffer} rawdata buffer
*
*/
dsoctrl.getAcquire = function getAcquire(acqProp) {
const self = this;
return new Promise(((resolve, reject) => {
function acq(e) {
if (e) {
reject(e);
} else {
resolve(self.acq);
}
}
let cmd = [];
if (acqProp) {
if (acqProp.mode !== undefined) {
cmd.push({ id: 'acq', prop: 'AcqMode', arg: acqProp.mode, cb: null, method: 'get' });
}
if (acqProp.average !== undefined) {
cmd.push({ id: 'acq', prop: 'AcqAverage', arg: acqProp.average, cb: null, method: 'get' });
}
if (acqProp.mem_length !== undefined) {
cmd.push({ id: 'acq', prop: 'AcqRecLength', arg: acqProp.mem_length, cb: null, method: 'get' });
}
if (cmd.length > 0) {
cmd[cmd.length - 1].cb = acq;
} else {
log('getAcquire do nothing');
reject('getAcquire do nothing');
return;
}
} else {
cmd = [
{ id: 'acq', prop: 'AcqRecLength', arg: '', cb: null, method: 'get' },
{ id: 'acq', prop: 'AcqMode', arg: '', cb: null, method: 'get' },
{ id: 'acq', prop: 'AcqAverage', arg: '', cb: acq, method: 'get' },
];
}
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to set acquisition property
*
* @method setAcquire
* @return {Buffer} rawdata buffer
*
*/
dsoctrl.setAcquire = function setAcquire(acqProp) {
const self = this;
return new Promise(((resolve, reject) => {
function acq(e) {
if (e) {
reject(e);
} else {
resolve(self.acq);
}
}
const cmd = [];
log(acqProp);
if (acqProp.mode !== undefined) {
cmd.push({ id: 'acq', prop: 'AcqMode', arg: acqProp.mode, cb: null, method: 'set' });
}
if (acqProp.average !== undefined) {
cmd.push({ id: 'acq', prop: 'AcqAverage', arg: acqProp.average, cb: null, method: 'set' });
}
if (acqProp.mem_length !== undefined) {
cmd.push({ id: 'acq', prop: 'AcqRecLength', arg: acqProp.mem_length, cb: null, method: 'set' });
}
if (cmd.length > 0) {
cmd[cmd.length - 1].cb = acq;
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
log('setAcquire do nothing');
resolve();
}
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to get acquisition setting
*
* @method getAcquire
* @return {Buffer} rawdata buffer
*
*/
dsoctrl.getTeacherFunc = function getTeacherFunc(prop) {
const self = this;
return new Promise(((resolve, reject) => {
function getDone(e) {
if (e) {
reject(e);
} else {
resolve(self.sys);
}
}
const cmd = [];
if (prop) {
if (prop.autosetenable !== undefined) {
cmd.push({ id: 'sys', prop: 'AutosetEnable', arg: '', cb: null, method: 'get' });
}
if (prop.cursorenable !== undefined) {
cmd.push({ id: 'sys', prop: 'CursorEnable', arg: '', cb: null, method: 'get' });
}
if (prop.measureenable !== undefined) {
cmd.push({ id: 'sys', prop: 'MeasureEnable', arg: '', cb: null, method: 'get' });
}
if (cmd.length > 0) {
cmd[cmd.length - 1].cb = getDone;
} else {
log('getTeacherFunc do nothing');
reject('getTeacherFunc do nothing');
return;
}
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
log('getTeacherFunc do nothing');
reject('getTeacherFunc do nothing');
}
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to set acquisition property
*
* @method setAcquire
* @return {Buffer} rawdata buffer
*
*/
dsoctrl.setTeacherFunc = function setTeacherFunc(prop) {
const self = this;
return new Promise(((resolve, reject) => {
function setDone(e) {
if (e) {
reject(e);
} else {
resolve();
}
}
const cmd = [];
log(prop);
if (prop) {
if (prop.autosetenable !== undefined) {
cmd.push({ id: 'sys', prop: 'AutosetEnable', arg: prop.autosetenable, cb: null, method: 'set' });
}
if (prop.cursorenable !== undefined) {
cmd.push({ id: 'sys', prop: 'CursorEnable', arg: prop.cursorenable, cb: null, method: 'set' });
}
if (prop.measureenable !== undefined) {
cmd.push({ id: 'sys', prop: 'MeasureEnable', arg: prop.measureenable, cb: null, method: 'set' });
}
if (cmd.length > 0) {
cmd[cmd.length - 1].cb = setDone;
} else {
log('setTeacherFunc do nothing');
reject('setTeacherFunc do nothing');
return;
}
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
log('setTeacherFunc do nothing');
reject('setTeacherFunc do nothing');
}
}));
}.bind(dsoObj);
// //////////////////////////
// dsoctrl.onError = (function(callback) {
// this.errHandler = callback;
// }.bind(dsoObj);
// /////////////////////////////
/**
* The method belong to dsoctrl class used to get the edge trigger properties from device
*
* @method getEdgeTrig
* @return {object} trigProperty
*
*/
/**
* Trigger property of device.
*
* @property trigProperty
* @type Object
* @param {String} type
* @param {String} source
* @param {String} mode
* @param {String} holdoff
* @param {String} noise-rej
* @param {String} level
* @param {String} alt
* @param {String} state
* @param {String} edge.coupling
* @param {String} edge.slop
*/
dsoctrl.getTrigProp = function getTrigProp(trigProp) {
const self = this;
const cmd = [];
return new Promise(((resolve, reject) => {
function trigCb(e) {
if (e) {
reject(e);
} else {
resolve(self.trig);
}
}
log(trigProp);
if (trigProp === undefined) {
reject(['-100', 'Parameter Error']);
return;
}
log(trigProp);
if (trigProp.type === 'edge') {
if (trigProp.edge.slope !== undefined) {
cmd.push({ id: 'trig', prop: 'TrigEdgeSlope', arg: trigProp.noise_rej, cb: null, method: 'get' });
}
if (trigProp.edge.coupling !== undefined) {
cmd.push({ id: 'trig', prop: 'TrigCouple', arg: trigProp.alt, cb: null, method: 'get' });
}
if (trigProp.edge.alt !== undefined) {
cmd.push({ id: 'trig', prop: 'TrigALT', arg: trigProp.alt, cb: null, method: 'get' });
}
}
if (cmd.length > 0) {
cmd[cmd.length - 1].cb = trigCb;
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
// log(self.dev.cmdSequence);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
log('getEdgeTrig do nothing');
resolve();
}
}));
}.bind(dsoObj);
dsoctrl.setTrigProp = function setTrigProp(trigProp) {
const self = this;
const cmd = [];
return new Promise(((resolve, reject) => {
function trigCb(e) {
if (e) {
reject(e);
} else {
resolve();
}
}
log(trigProp);
if (trigProp === undefined) {
reject(['-100', 'Parameter Error']);
return;
}
log(trigProp);
if (trigProp.type === 'edge') {
if (trigProp.edge.slope !== undefined) {
cmd.push({ id: 'trig', prop: 'TrigEdgeSlope', arg: trigProp.edge.slope, cb: null, method: 'set' });
}
if (trigProp.edge.coupling !== undefined) {
cmd.push({ id: 'trig', prop: 'TrigCouple', arg: trigProp.edge.coupling, cb: null, method: 'set' });
}
if (trigProp.edge.alt !== undefined) {
cmd.push({ id: 'trig', prop: 'TrigALT', arg: trigProp.edge.alt, cb: null, method: 'set' });
}
}
if (cmd.length > 0) {
cmd[cmd.length - 1].cb = trigCb;
self.dev.cmdSequence = self.dev.cmdSequence.concat(cmd);
// log(self.dev.cmdSequence);
self.cmdEvent.emit('cmdWrite', cmd);
} else {
log('getEdgeTrig do nothing');
resolve();
}
}));
}.bind(dsoObj);
/**
* The method belong to dsoctrl class used to set edge trigger properties to device
*
* @method setEdgeTrig
* @param {object} trigProperty
*
*/
/**
* Trigger property of device.
*
* @property trigProperty
* @type Object
* @param {String} type
* @param {String} source
* @param {String} mode
* @param {String} holdoff
* @param {String} noise_rej
* @param {String} level
* @param {String} alt
* @param {String} edge.coupling
* @param {String} edge.slop
*/
dsoctrl.setEdgeTrig = function setEdgeTrig(trigProp) {
const self = this;
const cmd = [];
return new Promise(((resolve, reject) => {
function edgeTrig(e) {
if (e) {
reject(e);
} else {
resolve(self.trig);
}
}
if (trigProp === undefined) {
reject(['-100', 'Parameter Error']);