LabJack-nodejs
'LabJack-nodejs' makes LabJack's LJM library available for node.js. The library uses ffi to link to the appropriate library file (LabJackM.dll, LabJackM.dylib, or LabJackM.so) which must be installed with one of LabJack's installers. Currently, installers are available for Windows, Mac OS X, Linux 32-bit & 64-bit, and a few builds for ARMv6 and ARMV7 architectures.
This library exposes the LJM driver slightly different than our standard LJM Library wrappers because it was created primarily for use in Kipling. If this library were to be re-written it would be split into two modules, one that directly exposes the LJM Library functions, and another that performs some of the abstractions. A brief summary of how this library exposes the LJM Library:
- Created two different objects, device.js and driver.js, that can be imported individually.
- Was created to function much like the LabJack Python driver for LabJack UD devices.
- For more information about what each function does, please look at the LabJackM.h file that can be downloaded & installed from LabJacks Software & Driver page.
Currently this wrapper only supports the T4, T7, T7-Pro, Digit-TL, and Digit-TLH LabJack devices. (Which are low cost, high-quality, multifunction USB / Ethernet / 802.11b/g WiFi DAQ devices.) Devices using the UD library (Windows only) aka U3, U6, and UE9 are not supported. Additionally, the U12 is not supported.
Notes:
If you are trying to perform device IO, consider looking at the ljswitchboard-ljm_device_curator project. It makes several more device abstractions and wraps this library in a promise interface.
If you are trying to scan for devices consider using the ljswitchboard-device_scanner module.
Requirements
- LabJack's LJM Library.
- Ability to build native modules, for Windows this requires Visual Studio.
- Look at the ffi library for more requirements.
- Look at the node-gyp installation notes for various requirements.
- Also look at the ref library as it too is a native module.
- Look at the windows nodejs-guidelines page for compiling native addon modules.
- Make sure that you have node-gyp installed by running the command "node-gyp" if not:
$ npm install -g node-gyp
Installation
Make sure that node-ffi and ref can be installed, it may be wise to create a dummy project and invoke:
$ npm install ffi
before trying to install LabJack-nodejs. Once you can do that simply install via npm:
$ npm install labjack-nodejs
Installation notes for Windows:
Make sure that node-gyp is properlly installed.
Performing the standard npm install command will fail on Windows. It will hopefully complain about not having proper "TargetFrameworkVersion or PlatformToolset variables not being set. If this is true, set them:
"npm install --msvs_version=2012" or 2013.
This hint came from someone building couchbase
For higher/newer versions of node, visual studio 14.0 is required aka msvs_version=2015.
Also, consider using the command "npm config set msvs_version 2015 --global" instead of the non global version node-gyp install issues.
General Information (Async vs sync function calls)
This driver wrapper was created supporting both synchronous and asynchronous function calls to support both functional and object-oriented programing styles. The general format is shown below:
var result = exampleFunctionSync(arg1);
exampleFunction(
arg1,
function(err) {
console.log('error', err);
},
function(result) {
console.log('success', result);
}
);
Basic Usage (Hello World, reading an analog input):
var ljn = require('@labjack/labjack-nodejs');
var createDeviceObject = ljn.getDevice();
var device = new createDeviceObject();
device.openSync();
console.log('AIN0:', device.readSync('AIN0'));
device.closeSync();
Available functions in the labjack-nodejs library:
After creating a labjack-nodejs library reference:
var ljn = require('@labjack/labjack-nodejs');
you get access to several attributes, the two most important ones are:
- ljn.device()
- ljn.getDevice()
- ljn.getDeviceRef()
- ljn.driver()
- ljn.getDriver()
Look at the lib/labjack_nodejs.js file for what gets exported. There are a few subtle differences between the functions in terms of creating new objects.
In general, LJM functions that require the passing of a device handle are implemented in the device object. Functions that are device-agnostic are implemented in the driver object.
Available Functions in the device object (device.js):
After creating a device object:
var device = new createDeviceObject();
several functions are made available:
Device Opening/Closing
- open/openSync
- close/closeSync
- getHandleInfo/getHandleInfoSync
Reading:
- readRaw/readRawSync
- read/readSync
- readArray/readArraySync
- readMany/readManySync
Writing:
- writeRaw/writeRawSync
- write/writeSync
- writeArray/writeArraySync
- writeMany/writeManySync
Both Directions:
Special/Streaming
- readUINT64/readUINT64Sync
- streamSettings
- streamStart/streamStartSync
- streamRead/streamReadSync
- streamStop/streamStopSync
open():
Uses LJM_Open and LJM_OpenS
device.openSync();
device.openSync('LJM_dtANY', 'LJM_ctANY', 'LJM_idANY');
device.openSync('LJM_dtT7', 'LJM_USB', '470010642');
device.openSync('LJM_dtT7', 'LJM_ETHERNET', '470010642');
device.openSync('LJM_dtT7', 'LJM_WIFI', '470010642');
device.openSync(7, 1, 470010642);
var onSuccess = function(result) {
}
var onError = function(error) {
}
device.open(
'LJM_dtANY',
'LJM_ctANY',
'LJM_idANY',
onError,
onSuccess
);
getHandleInfo():
Uses LJM_GetHandleInfo
devInfo = device.getHandleInfoSync();
devInfo.deviceType;
devInfo.connectionType;
devInfo.serialNumber;
devInfo.ipAddress;
devInfo.port;
devInfo.maxBytesPerMB;
readRaw(data array):
Uses LJM_ReadRaw
read(address 'number' or 'string'):
Uses LJM_eReadAddress, LJM_eReadName, LJM_eReadNameString, and LJM_eReadAddressString.
value = device.readSync('AIN0');
value = device.readSync(0);
value = device.readSync('DEVICE_NAME_DEFAULT');
value = device.read(
'AIN0',
function (res) {
console.log('err:', res);
},
function (res) {
console.log('ain0Reading:', res);
}
);
readMany(addresses 'number' or 'string' array):
Uses LJM_eReadAddresses and LJM_eReadNames
value = device.readManySync(['AIN0', 'AIN1']);
value = device.readManySync([0, 1]);
writeRaw(data array):
Uses LJM_WriteRaw
write(address 'number' or 'string', value 'number' or 'string'):
Uses LJM_eWriteAddress, LJM_eWriteName, LJM_eWriteAddressString, and LJM_eWriteNameString
errRes = device.writeSync('DAC0', 1.0);
errRes = device.writeSync(1000, 1.0);
value = device.writeSync('DEVICE_NAME_DEFAULT', 'NewDeviceName');
errRes = device.write(
'DAC0',
1.0,
function (res) {
console.log('err:', res);
},
function (res) {
console.log('SUCCESS');
});
writeMany(addresses array 'number' or 'string', values array 'number' or 'string')
writeMany(dict array {addr, vals}):
Uses LJM_eWriteAddresses LJM_eWriteNames
errRes = device.writeManySync(['DAC0', 'DAC1'], [1.0, 2.0]);
errRes = device.writeManySync([1000, 1002], [1.0, 2.0]);
close():
Uses LJM_Close
errRes = device.closeSync();
device.close(
function(res){
console.log('Err:', res);
},
function(res){
console.log('closed!');
});
All Relevant 'libLabJackM' Functions:
LJM_Driver (driver.js)
JavaScript wrapper for the rest of the LJM_Driver functions.
var ljn = require('@labjack/labjack-nodejs');
var driver = ljn.driver();
Available Functions & what they use:
- listAll/listAllSync
- listAllExtended/listAllExtendedSync
- errToStr/errToStrSync
- loadConstants/loadConstantsSync
- closeAll/closeAllSync
- readLibrary/readLibrarySync
- readLibraryS/readLibrarySSync
- writeLibrary/writeLibrarySync
- log/logSync
- logS/logSSync
- resetLog/resetLogSync
- controlLog/controlLogSync
- enableLog/enableLogSync
- disableLog/disableLogSync
listAll(deviceType 'number' or 'string', connectionType 'number' or 'string'):
Uses LJM_ListAll and LJM_ListAllS
foundDevices = ljmDriver.listAllSync();
foundDevices = ljmDriver.listAllSync('LJM_dtANY', 'LJM_ctANY');
foundDevices = ljmDriver.listAllSync('LJM_dtT7', 'LJM_ctUSB');
foundDevices = ljmDriver.listAllSync(7, 1);
ljmDriver.listAll(
function (err) {
console.log('Error', err);
},
function (foundDevices) {
console.log('Devices Found:');
console.log(foundDevices);
}
);
errToStr(errorNumber):
Uses LJM_ErrorToString, converts an error number to a human-readable string-error. The errors can be found in the ljm_constants.json file.
console.log(ljmDriver.errToStrSync(0));
console.log(ljmDriver.errToStrSync(200));
console.log(ljmDriver.errToStrSync(1268));
loadConstants():
Uses LJM_LoadConstants
closeAll():
Uses LJM_CloseAll
readLibrary('string' parameter):
Uses LJM_ReadLibraryConfigS, helpful for using LJM's logging features
readLibraryS('string' parameter):
Uses LJM_ReadLibraryConfigStringS, helpful for using LJM's logging features
writeLibrary('string' parameter, value either 'number' or 'string'):
Uses LJM_WriteLibraryConfigS and LJM_WriteLibraryConfigStringS, helpful for using LJM's logging features
logS('number' logLevel, 'string' message to log):
Uses LJM_Log
resetLog():
Uses LJM_ResetLog
All Relevant 'libLabJackM' Functions: