C/C++ for LJM - Windows, Mac, Linux
C/C++ examples for the LJM library. Also contains some header files:
LJM_Utilities.h
- contains useful functionsstream/LJM_StreamUtilities.h
- contains useful stream functionsLabJackMModbusMap.h
- contains declarations for register addresses, names, and types
Prerequisites
A T-series LabJack Device
LJM Library - Install the LJM Library
C/C++ Compiler - We use Microsoft Visual Studio on Windows and gcc on macOS and Linux.
Operating System - Windows, macOS, or Linux.
Subsections
Latest Release Example Package
Latest Beta Example Package
GitHub
Our latest example changes are available in our GitHub repository.
Instructions
Go to your device Quickstart Tutorial and follow the steps to install LabJack software and confirm basic operation.
Download/extract the
.zip
file attached at the top of this page.Navigate to the extracted
LabJackM
folder.Open and run
.c
/.cpp
source files using your code editor of choice.
Windows
Newer versions of Visual Studio work with these .sln
and .vcproj
files, though you will need to run the automatic upgrade process when opening them.
For Visual Studio 2017, you may get the following error:
Error C1083 Cannot open include file: 'stdio.h': No such file or directory
In this case, you need to switch the project SDK version from 8.1 to 10:
Open the
Properties
window for the project (e.g. in the Solution Explorer, right click on the project - not the solution)Change
Configuration Properties
→General
→Target Platform Version
to something like10.0.14393.0
Mac/Linux
On Mac/Linux, the .zip format unfortunately removes the execution permissions from the build scripts. To use the examples on Mac/Linux, you may need to apply executable permissions. For example:
tar zxvf C_C++_LJM_2019-05-20.zip
cd C_C++_LJM_2019-05-20
chmod a+x make_all.sh basic/make.sh more/*/make.sh scons-local-2.5.1/scons.py
LJM Overview
With the LJM library, pretty much everything you might want to do with a device is accomplished by writing and/or reading some registers. Look at the T-series Datasheet or the Modbus Map to determine what registers you need to write and read, then use eWriteName (or eWriteNames) to write the desired registers and eReadName (or eReadNames) to read the desired registers. The following examples in the Basic folder are a great place to start:
LJM_eWriteName Example
LJM_eWriteNames Example
LJM_eReadName Example
LJM_eReadNames Example
Write Read Loop with Config
Where is an example to do XYZ?
You will find lots of examples in this archive, but there is not an example for everything the LabJack can do. The reason for this stems from the "Overview" section above. Most operations simply involve writing and reading different registers, so you really just need examples that show you how to write and read any register. If we had examples for every operation, they would just be copies of the example "Write Read Loop with Config" with different registers. The typical workflow to do almost anything beside stream is:
Look at the T-series Datasheet or the Modbus Map to determine what registers you need to write and read.
Use the Register Matrix in Kipling to test writing and reading your desired registers and confirm you see what you expect to see. This step is optional and not always applicable.
Use eWriteName (or eWriteNames) to write the desired registers and eReadName (or eReadNames) to read the desired registers. Or just use "Write Read Loop with Config", which provides the basic structure used by many user applications.
Stream mode and other operations that don't fit in "Write Read Loop with Config" will usually have specific examples, and if something seems to be missing let us know.
Compiling
To compile a C or C++ program that uses LJM see the compilation instructions, which include instructions for compiling programs including:
General instructions
Visual Studio instructions
Using included make.sh build scripts
Using Xcode
Code Snippet
#include <stdio.h>
#include <LabJackM.h>
#include "LJM_Utilities.h"
// Available in the C/C++ examples download. Provides the ErrorCheck function.
int main()
{
int err, handle;
double value = 0;
const char * NAME = {"SERIAL_NUMBER"};
// Open first found LabJack
err = LJM_Open(LJM_dtANY, LJM_ctANY, "LJM_idANY", &handle);
ErrorCheck(err, "LJM_Open");
// Call LJM_eReadName to read the serial number from the LabJack.
err = LJM_eReadName(handle, NAME, &value);
ErrorCheck(err, "LJM_eReadName");
printf("eReadName result:\n");
printf(" %s = %f\n", NAME, value);
// Close device handle
err = LJM_Close(handle);
ErrorCheck(err, "LJM_Close");
return LJME_NOERROR;
}