rigol-dho800
Version:
Node.js bindings for Rigol DHO800
132 lines • 5.26 kB
JavaScript
import assert from 'node:assert';
export class RigolDho800 {
instr;
constructor(instr) {
this.instr = instr;
}
reset() {
this.instr.write('*RST');
this.instr.write('*CLS');
this.wait();
}
configureTimebase(config = {}) {
if (config.scale !== undefined) {
this.instr.write(`:TIM:SCAL ${config.scale}`);
}
if (config.offset !== undefined) {
this.instr.write(`:TIM:OFFS ${config.offset}`);
}
}
configureChannel(ch, config = {}) {
if (config.display !== undefined) {
this.instr.write(`:CHAN${ch}:DISP ${config.display ? 'ON' : 'OFF'}`);
}
if (config.probeRatio !== undefined) {
this.instr.write(`:CHAN${ch}:PROB ${config.probeRatio}`);
}
if (config.verticalScale !== undefined) {
this.instr.write(`:CHAN${ch}:SCAL ${config.verticalScale}`);
}
if (config.offset !== undefined) {
this.instr.write(`:CHAN${ch}:OFFS ${config.offset}`);
}
if (config.coupling !== undefined) {
this.instr.write(`:CHAN${ch}:COUP ${config.coupling}`);
}
}
configureMemoryDepth(depth) {
this.instr.write(`:ACQ:MDEP ${depth}`);
}
configureEdgeTrigger(config) {
this.instr.write(`:TRIG:MODE EDGE`);
this.instr.write(`:TRIG:EDGE:SOUR ${config.source}`);
this.instr.write(`:TRIG:EDGE:LEV ${config.level}`);
this.instr.write(`:TRIG:EDGE:SLOP ${config.slope}`);
}
singleTrigger() {
this.instr.write(':SING');
}
stop() {
this.instr.write(':STOP');
}
run() {
this.instr.write(':RUN');
}
async waitTigger(timeout = 0) {
const start = Date.now();
while (true) {
const status = this.instr.query(':TRIG:STAT?');
if (status === 'STOP') {
break;
}
await new Promise((resolve) => setTimeout(resolve, 100));
if (timeout > 0 && Date.now() - start > timeout) {
return false;
}
}
return true;
}
readWaveform(ch, opts) {
const convertToVoltage = opts?.convertToVoltage ?? true;
const bufferSize = opts?.bufferSize ?? 2 * 1024 * 1024;
this.instr.write(`:WAV:SOUR CHAN${ch}`);
this.instr.write(':WAV:MODE RAW');
this.instr.write(':WAV:FORM WORD');
this.wait();
const result = this.instr.queryBinary(':WAV:DATA?', bufferSize);
const params = this.queryWaveformParameters();
assert(result.length > 2, new Error('Invalid waveform data'));
assert(result.at(0) === '#'.charCodeAt(0), new Error('Invalid waveform data'));
const lengthSize = parseInt(String.fromCharCode(result.at(1) ?? 0), 10);
assert(lengthSize >= 1 && lengthSize <= 9, new Error('Invalid waveform data'));
const length = parseInt(result.toString('ascii', 2, 2 + lengthSize));
const data = result.subarray(2 + lengthSize, 2 + lengthSize + length);
assert(data.length === length, new Error('Invalid waveform data'));
const samples = [];
for (let i = 0; i < data.length; i += 2) {
const sample = data.readUInt16LE(i);
if (!convertToVoltage) {
samples.push(sample);
continue;
}
const voltage = (sample - params.yreference - params.yorigin) * params.yincrement;
samples.push(voltage);
}
return { samples, params };
}
queryWaveformParameters() {
const response = this.instr.query(':WAV:PRE?');
const params = response.split(',').map((p) => parseFloat(p));
const [format, type, points, count, xincrement, xorigin, xreference, yincrement, yorigin, yreference,] = params;
assert(format !== undefined, new Error('Invalid waveform parameters'));
assert(type !== undefined, new Error('Invalid waveform parameters'));
assert(points !== undefined, new Error('Invalid waveform parameters'));
assert(count !== undefined, new Error('Invalid waveform parameters'));
assert(xincrement !== undefined, new Error('Invalid waveform parameters'));
assert(xorigin !== undefined, new Error('Invalid waveform parameters'));
assert(xreference !== undefined, new Error('Invalid waveform parameters'));
assert(yincrement !== undefined, new Error('Invalid waveform parameters'));
assert(yorigin !== undefined, new Error('Invalid waveform parameters'));
assert(yreference !== undefined, new Error('Invalid waveform parameters'));
return {
format,
type,
points,
count,
xincrement,
xorigin,
xreference,
yincrement,
yorigin,
yreference,
};
}
createWaveformXLabels(params, inMs = false) {
const multiplier = inMs ? 1e3 : 1;
return Array.from({ length: params.points }, (_, i) => (params.xorigin + (i - params.xreference) * params.xincrement) * multiplier);
}
wait() {
this.instr.query('*OPC?');
}
}
//# sourceMappingURL=RigolDho800.js.map