weblab-instrument
Version:
communication with instrument through usb
241 lines (230 loc) • 6.97 kB
JavaScript
const propMethod = require('../dev/propMethod.js');
// const util = require('util');
const debugInfo = require('debug');
const log = debugInfo('channel:log');
function Channel() {
this.coupling = 'DC';
this.impedance = '1E+6';
this.invert = 'OFF';
this.bandwidth = 'FULL';
this.expand = 'CENTER';
this.state = 'OFF';
this.scale = '1E+1';
this.position = '0E+0';
this.deskew = '0E+0';
this.rawdata = new Buffer(10000);
this.raw_state = '0';
this.probe = {
unit: 'voltage',
atten: '1E+1',
};
this.dataCount = 0;
this.recCount = 0;
}
Channel.prototype.cmdHandler = {
COUPling: {
setHelper(chObj, arg) {
chObj.coupling = arg;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.coupling = res.toString();
return true;
},
},
BWLimit: {
setHelper(chObj, arg) {
chObj.bandwidth = arg;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.bandwidth = res.toString();
return true;
},
},
VerPOSition: {
setHelper(chObj, arg) {
const fval = parseFloat(arg);
chObj.position = fval.toExponential(3);
return true;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.position = res.toString();
return true;
},
},
VerSCALe: {
setHelper(chObj, arg) {
chObj.scale = arg;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.scale = res.toString();
return true;
},
},
ChState: {
setHelper(chObj, arg) {
chObj.state = arg;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.state = res.toString();
return true;
},
},
VerEXPand: {
setHelper(chObj, arg) {
chObj.expand = arg;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.expand = res.toString();
return true;
},
},
IMPedance: {
setHelper(chObj, arg) {
chObj.impedance = arg;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.impedance = res.toString();
return true;
},
},
INVert: {
setHelper(chObj, arg) {
chObj.invert = arg;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.invert = res.toString();
return true;
},
},
DESKew: {
setHelper(chObj, arg) {
const fval = parseFloat(arg);
chObj.deskew = fval.toExponential(3);
return true;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.deskew = res.toString();
return true;
},
},
PROBe_RATio: {
setHelper(chObj, arg) {
chObj.probe.atten = arg;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.probe.atten = res.toString();
return true;
},
},
PROBe_Type: {
setHelper(chObj, arg) {
chObj.probe.unit = arg;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.probe.unit = res.toString();
return true;
},
},
AcqMemory: {
setHelper() {
return false;
},
getHandler(chObj, data) {
if (chObj.dataCount === 0) {
let header;
if (data.length > 10) {
header = data.slice(0, 10).toString();
} else {
header = data.toString();
}
log(`data length=${data.length}`);
// log('data[0]='+data[0]);
if (header[0] === '#') {
chObj.dataCount = Number(header.slice(2, Number(header[1]) + 2));
// delete sysCmd.dispData;
chObj.rawdata = new Buffer(chObj.dataCount);
log(`raw dataCount = ${chObj.dataCount}`);
// log(sysObj);
chObj.rawdata = chObj.rawdata.slice(0, chObj.dataCount);
if (data.length > (Number(header[1]) + 2)) {
log(`before slice data length = ${data.length}`);
data = data.slice((Number(header[1]) + 2), data.length);
log(`slice data length = ${data.length}`);
data.copy(chObj.rawdata, chObj.recCount);
chObj.recCount += data.length;
log('=======');
if (chObj.recCount > chObj.dataCount) {
chObj.recCount = 0;
chObj.dataCount = 0;
log(`last 1 byte = ${data[data.length - 1]}`);
return true;
}
}
}
} else {
data.copy(chObj.rawdata, chObj.recCount);
chObj.recCount += data.length;
// log('chObj.recCount='+chObj.recCount+',data.length='+data.length);
if (chObj.recCount > chObj.dataCount) {
// if(chObj.recCount>chObj.dataCount){
// log('slice data');
// chObj.rawdata=chObj.rawdata.slice(0,-1);
// }
// log('chObj.recCount='+chObj.recCount);
log(`chObj.rawdata length= ${chObj.rawdata.length}`);
chObj.recCount = 0;
chObj.dataCount = 0;
return true;
}
}
return false;
},
},
AcqState: {
setHelper() {
return false;
},
getHandler(chObj, res) {
res = res.slice(0, -1);
chObj.raw_state = res.toString();
return true;
},
},
};
Channel.prototype.setToDefault = function setToDefault(chObj) {
chObj.coupling = 'DC';
chObj.impedance = '1E+6';
chObj.invert = 'OFF';
chObj.bandwidth = 'FULL';
chObj.expand = 'CENTER';
chObj.state = 'OFF';
chObj.scale = '1E+1';
chObj.position = '0E+0';
chObj.deskew = '0E+0';
chObj.raw_state = '0';
chObj.probe = {
unit: 'voltage',
atten: '1E+1',
};
chObj.dataCount = 0;
chObj.recCount = 0;
};
exports.initChanObj = function initChanObj(id) {
const chanCmd = new Channel();
chanCmd.id = id;
chanCmd.prop = propMethod.CreatMethod.call(this, id);
// log('meas id='+id);
// log('meas this='+this);
return chanCmd;
};