-
Notifications
You must be signed in to change notification settings - Fork 5
/
ni-visa.js
287 lines (266 loc) · 7.88 KB
/
ni-visa.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* Copyright (C) Peter Torelli
*
* Licensed under Apache 2.0
*
* Interface wrapper to the VISA dynamic library.
*/
/**
* TODO List
*
* TODO: Error handling. Throw? Return status? How should we do this?
*/
const
debug = require('debug')('ni-visa'),
vcon = require('./ni-visa-constants.js'),
ffi = require('ffi'),
ref = require('ref'),
os = require('os');
/**
* Create types like the ones in "visatype.h" from National Instruments
*/
const ViInt32 = ref.types.int32;
const ViPInt32 = ref.refType(ViInt32);
const ViUInt32 = ref.types.uint32;
const ViPUInt32 = ref.refType(ViUInt32);
const ViInt16 = ref.types.int16;
const ViPInt16 = ref.refType(ViInt16);
const ViUInt16 = ref.types.uint16;
const ViPUInt16 = ref.refType(ViUInt16);
const ViChar = ref.types.char;
const ViPChar = ref.refType(ViChar);
const ViByte = ref.types.uchar;
const ViPByte = ref.refType(ViByte);
// Note, this needs to be ViUInt32, not ViInt32 other we get negative hex
const ViStatus = ViUInt32;
const ViObject = ViUInt32;
const ViSession = ViUInt32;
const ViPSession = ref.refType(ViSession);
const ViString = ViPChar;
const ViConstString = ViString;
const ViRsrc = ViString;
const ViConstRsrc = ViConstString;
const ViAccessMode = ViUInt32;
const ViBuf = ViPByte;
const ViPBuf = ViPByte;
const ViConstBuf = ViPByte;
const ViFindList = ViObject;
const ViPFindList = ref.refType(ViFindList);
// Choose the proper DLL name
let dllName;
// I didn't see Linux support on the NI website...
switch (os.platform()) {
case 'darwin':
dllName = 'visa.framework/visa';
break;
case 'linux':
dllName = 'librsvisa';
break;
case 'win32':
dllName = os.arch() == 'x64' ? 'visa64.dll' : 'visa32.dll';
break;
default:
throw new Error('Unknown platform: ' + os.platform());
}
// 'string' is used to reduce code, the FFI module will create Buffers as needed
const libVisa = ffi.Library(dllName, {
// Resource Manager Functions and Operations
'viOpenDefaultRM': [ViStatus, [ViPSession]],
'viFindRsrc': [ViStatus, [ViSession, 'string', ViPFindList, ViPUInt32, 'string']],
'viFindNext': [ViStatus, [ViFindList, 'string']],
'viParseRsrc': [ViStatus, [ViSession, 'string', ViPUInt16, ViPUInt16]],
'viParseRsrcEx': [ViStatus, [ViSession, 'string', ViPUInt16, ViPUInt16, 'string', 'string', 'string']],
'viOpen': [ViStatus, [ViSession, 'string', ViAccessMode, ViUInt32, ViPSession]],
// Resource Template Operations
'viClose': [ViStatus, [ViObject]],
// Basic I/O Operations
'viRead': [ViStatus, [ViSession, ViPBuf, ViUInt32, ViPUInt32]],
'viReadToFile': [ViStatus, [ViSession, 'string', ViUInt32, ViPUInt32]],
'viWrite': [ViStatus, [ViSession, 'string', ViUInt32, ViPUInt32]],
});
// TODO: since error handling is undecided, every function calls this
function statusCheck (status) {
if (status & vcon.VI_ERROR) {
console.log('Warning: VISA Error: 0x' + (status >>> 0).toString(16).toUpperCase());
throw new Error();
} else {
if (status) {
let str = vcon.decodeStatus(status);
if (str != null) {
debug(`non-error status check: ${status.toString(16)} ${str}`);
} else {
debug(`non-error status check: ${status.toString(16)}`);
}
}
}
}
function viOpenDefaultRM () {
let status;
let pSesn = ref.alloc(ViSession);
status = libVisa.viOpenDefaultRM(pSesn);
statusCheck(status);
return [status, pSesn.deref()];
}
function viFindRsrc (sesn, expr) {
let status;
let pFindList = ref.alloc(ViFindList);
let pRetcnt = ref.alloc(ViUInt32);
let instrDesc = Buffer.alloc(512);
status = libVisa.viFindRsrc(sesn, expr, pFindList, pRetcnt, instrDesc);
statusCheck(status);
return [
status,
pFindList.deref(),
pRetcnt.deref(),
// Fake null-term string
instrDesc.toString('ascii', 0, instrDesc.indexOf(0))
];
}
function viFindNext (findList) {
let status;
let instrDesc = Buffer.alloc(512);
status = libVisa.viFindNext(findList, instrDesc);
statusCheck(status);
return [
status,
// Fake null-term string
instrDesc.toString('ascii', 0, instrDesc.indexOf(0))
];
}
function viParseRsrc (sesn, rsrcName) {
let status;
let pIntfType = ref.alloc(ViUInt16);
let pIntfNum = ref.alloc(ViUInt16);
status = libVisa.viParseRsrc(sesn, rsrcName, pIntfType, pIntfNum);
statusCheck(status);
return [
status,
// This is a VI_INTF_* define
pIntfType.deref(),
// This is the board #
pIntfNum.deref()
];
}
// TODO: Untested, I don't hardware that responds to this call
function viParseRsrcEx (sesn, rsrcName) {
let status;
let pIntfType = ref.alloc(ViUInt16);
let pIntfNum = ref.alloc(ViUInt16);
let rsrcClass = Buffer.alloc(512);
let expandedUnaliasedName = Buffer.alloc(512);
let aliasIfExists = Buffer.alloc(512);
status = libVisa.viParseRsrcEx(sesn, rsrcName, pIntfType, pIntfNum,
rsrcClass, expandedUnaliasedName, aliasIfExists);
statusCheck(status);
return [
status,
// This is a VI_INTF_* define
pIntfType.deref(),
// This is the board #
pIntfNum.deref(),
rsrcClass.toString('ascii', 0, rsrcClass.indexOf(0)),
expandedUnaliasedName.toString('ascii', 0, expandedUnaliasedName.indexOf(0)),
aliasIfExists.toString('ascii', 0, aliasIfExists.indexOf(0))
];
}
function viOpen (sesn, rsrcName, accessMode=0, openTimeout=2000) {
let status;
let pVi = ref.alloc(ViSession);
status = libVisa.viOpen(sesn, rsrcName, accessMode, openTimeout, pVi);
statusCheck(status);
return [status, pVi.deref()];
}
function viClose (vi) {
let status;
status = libVisa.viClose(vi);
statusCheck(status);
return status;
}
// TODO ... assuming viRead always returns a string, probably wrong
function viRead (vi, count=512) {
let status;
let buf = Buffer.alloc(count);
let pRetCount = ref.alloc(ViUInt32);
status = libVisa.viRead(vi, buf, buf.length, pRetCount)
statusCheck(status);
debug(`read (${count}) -> ${pRetCount.deref()}`);
return [status, ref.reinterpret(buf, pRetCount.deref(), 0).toString()];
}
// Returns the raw Buffer object rather than a decoded string
function viReadRaw (vi, count=512) {
let status;
let buf = Buffer.alloc(count);
let pRetCount = ref.alloc(ViUInt32);
status = libVisa.viRead(vi, buf, buf.length, pRetCount)
statusCheck(status);
debug(`readRaw: (${count}) -> ${pRetCount.deref()}`);
return [status, buf.slice(0, pRetCount.deref())];
}
// 'viReadToFile': [ViStatus, [ViSession, 'string', ViUInt32, ViPUInt32]],
function viReadToFile (vi, fileName, count) {
let status;
let pRetCount = ref.alloc(ViUInt32);
status = libVisa.viReadToFile(vi, fileName, count, pRetCount);
statusCheck(status);
debug(`readToFile (${count}) -> ${pRetCount.deref()}`);
return [status];
}
function viWrite (vi, buf) {
debug('write:', buf);
let status;
let pRetCount = ref.alloc(ViUInt32);
status = libVisa.viWrite(vi, buf, buf.length, pRetCount)
statusCheck(status);
if (pRetCount.deref() != buf.length) {
throw new Error('viWrite length fail' + `: ${pRetCount.deref()} vs ${buf.length}`)
}
return [status, pRetCount.deref()];
}
/**
* These helper functions combine vi* functions to perform routine tasks.
* Error handling is left to the vi* functions.
*/
/**
* Returns a list of strings of found resources
*/
function vhListResources (sesn, expr='?*') {
let descList = [];
let [status, findList, retcnt, instrDesc] = viFindRsrc(sesn, expr);
if (retcnt) {
descList.push(instrDesc);
for (let i = 1; i < retcnt; ++i) {
[status, instrDesc] = viFindNext(findList);
descList.push(instrDesc);
}
}
return descList;
}
/**
* TODO: How are compound queries handled (reponsed to)
* Returns only the response, no status; status handled by error handler
*/
function vhQuery (vi, query) {
viWrite(vi, query);
// TODO: return status as well?
return viRead(vi)[1];
}
// TODO: create this from the FFI object rather than retyping
module.exports = {
// Resource Manager Functions and Operations
viOpenDefaultRM,
viFindRsrc,
viFindNext,
viParseRsrc,
viParseRsrcEx,
viOpen,
// Resource Template Operations
viClose,
/// Basic I/O Operations
viRead,
viReadRaw,
viReadToFile,
viWrite,
// Helper functions
vhListResources,
vhQuery,
}