From d1c24c2ecca0f1795fb720c15a9144914a885760 Mon Sep 17 00:00:00 2001 From: samofab Date: Wed, 5 Aug 2015 19:05:42 +0200 Subject: [PATCH] initalcommit --- .gitignore | 1 + README.md | 29 ++++++------------ index.html | 10 ++++++- index.js | 13 +++++--- nisa32.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 35 ++++++++++++++++++++++ 6 files changed, 146 insertions(+), 25 deletions(-) create mode 100644 .gitignore create mode 100644 nisa32.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md index de2f6a0..11a0230 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,16 @@ -# visa32test.js +# node-nisa32 - VISA32TEST.JS - -visa32test.js is a prototype for using VISA ( Virtual Instrument Software Architechture) library in node.js. -This is a wrapper for visa32.dll ( in your system32 folder). +nisa32 is another prototype for using VISA library in node.js. +This is a wrapper for visa32.dll (in your system32 folder). -I tested this code on Windows 7 (32bit), Keysight IO Library (17.1), node.js(0.10.26) . +I tested this code on Windows 8.1 (64bit), Keysight IO Library (17.1), node.js -Please test on your insturment (e.g. GPIB, RS232, LAN etc...) and fork me, and develop complete VISA module. (like pyVISA) +Talked to HP 6623A... +``` +> npm install nisa32 -to use... +``` -> npm install ffi +Original Author : 7M4MON -> npm install ref - -> npm install ref-array - -> node visa32test.js - - - -Author : 7M4MON - -Licence : MIT diff --git a/index.html b/index.html index 5b323f0..c6d9ac2 100644 --- a/index.html +++ b/index.html @@ -7,7 +7,7 @@

NODE.JS INTERACTIVE IO

- +

Receive Message

@@ -29,5 +29,13 @@

Receive Message

}); +

Common commands for HP 662xA

+ \ No newline at end of file diff --git a/index.js b/index.js index 0f95141..61f8e6f 100644 --- a/index.js +++ b/index.js @@ -15,9 +15,9 @@ var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); -var visa32test = require('./visa32test.js'); +var visa32test = require('./nisa32.js'); -//visa32test.Visa32TestQuery('GPIB0::22::INSTR','*IDN?'); +// visa32test.Visa32TestQuery('GPIB0::12::INSTR','*IDN?'); app.get('/',function(req,res){ res.sendfile('index.html'); @@ -25,8 +25,13 @@ app.get('/',function(req,res){ var rcvMsg; io.on('connection',function(socket){ socket.on('sendmsg',function(msg){ - rcvMsg = visa32test.Visa32TestQuery(msg.addr,msg.cmd); - io.emit('recvmsg', rcvMsg); + rcvMsg = visa32test.query(msg.addr,msg.cmd, function(err, result) { + if (err) + io.emit('recvmsg', err); + else + io.emit('recvmsg', result); + } + ); }); }); diff --git a/nisa32.js b/nisa32.js new file mode 100644 index 0000000..cb2b645 --- /dev/null +++ b/nisa32.js @@ -0,0 +1,83 @@ +var ref = require('ref'); +var ffi = require('ffi'); +var ArrayType = require('ref-array'); + + +// typedef +var ViError = ref.types.int; +var ViSession = ref.types.int; + +var byte = ref.types.byte; +var ByteArray = ArrayType(byte); + +var visa32 = ffi.Library('visa32', { + 'viOpenDefaultRM': [ViError, ['string'] ] , //viOpenDefaultRM(sesn) + 'viOpen' : [ViError, ['int', 'string', 'int', 'int', 'string'] ], //viOpen(sesn, rsrcName, accessMode, timeout, vi) + 'viPrintf' : ['int',['int', 'string']], + 'viScanf' : ['int',['int', 'string', ByteArray]], + 'viClose' : ['int', [ViSession] ] +}); + +/* idea: +module.exports = function(visaAddress) { + return new nisa32(visaAddress); +} + +function nisa32() { + var self = this; // Reference to "this" that won't get clobbered by some other "this" + // Private state variables + var tempPath; + // Public method: initialize the object + self.init = function(options, callback) { ... } + function private() { ... } +} +*/ + +exports.query = function (visaAddress, queryString, callback){ + return nisaQuery(visaAddress, queryString, callback); +}; + +function nisaQuery(visaAddress, queryString, callback){ + + var resourceManager = '0'; + var viError = 0; + var session = 0; + var replyString = ''; + + // intialize Buffer + var replyBuff = new ByteArray(256); + var counter; + for (counter = 0 ; counter < 256 ; counter++){ + replyBuff [counter] = 0 ; + } + + viError = visa32.viOpenDefaultRM('0'); + if (viError) { + callback(viError); + } + + console.log("ADDR : " + visaAddress + " SEND : " + queryString); + viError = visa32.viOpen('256', visaAddress, '0', '2000', '256'); + if (viError) { + return callback(viError); + } + viError = visa32.viPrintf('1', queryString + "\n"); + if (viError) { + return callback(viError); + } + + viError = visa32.viScanf('1', "%s", replyBuff); + if (viError) { + return callback(viError); + } + visa32.viClose(resourceManager); + + // make reply string + counter = 0; + while(replyBuff[counter] != 0){ + replyString += String.fromCharCode( replyBuff [counter] ); + counter ++; + } + console.log("RECV : " + replyString); + callback(null, replyString); +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..a8683bf --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "node-nisa32", + "version": "1.0.0", + "description": "A simple npm module for interfacing with GPIB instruments ", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node index.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/samofab/node-nisa32.git" + }, + "keywords": [ + "gpib", + "hpib" + ], + "author": "Samo Fabcic ", + "license": "MIT", + "bugs": { + "url": "https://github.com/samofab/node-nisa32/issues" + }, + "homepage": "https://github.com/samofab/node-nisa32#readme", + "dependencies": { + "express": "^4.13.3", + "ffi": "^1.3.2", + "http": "0.0.0", + "ref": "^1.0.2", + "ref-array": "^1.1.1", + "socket.io": "^1.3.6" + }, + "files": { + "nisa32.js" + } +}