//Use t he follo wing li ne to op en the f irst fo und LabJ ack U3
//over USB and get a h andle to the dev ice.
//The g eneral f orm of the open functio n is:
//OpenL abJack ( DeviceT ype, Con nectionT ype, Ad dress, F irstFoun d, *Han dle)
//Open the firs t found LabJack U3 over USB.
lngErro rcode = OpenLab Jack (LJ _dtU3, L J_ctUSB , "1", T RUE, &ln gHandle );
Second, a list of requests is built in the UD driver usi ng AddRequest calls. This does not involve any low-level communi cation with
the devi ce, and thus the execution time is relatively instantaneous:
//Reque st that DAC0 be set to 2.5 volt s.
//The g eneral f orm of the AddR equest f unction is:
//AddRe quest (H andle, IOType, Channel, Value, x1, Use rData)
lngErro rcode = AddRequ est (lng Handle, LJ_ioPU T_DAC, 0 , 2.50, 0, 0);
//Reque st a rea d from AIN3 (FI O3), ass uming i t has be en enabl ed as
//an an alog lin e.
lngErro rcode = AddRequ est (lng Handle, LJ_ioGE T_AIN, 3 , 0, 0, 0);
Third, the list of requests is processed and executed using a Go call. In this step, the driver determines which low-level commands
must be executed to process all the requests, calls those low-level functions, and stores the results. This example consists of two
requests, one analog input read and one a nalog output write, which can both be handled in a single low-level Feedback call
(Section 5.2.5):
//Execu te the r equests .
lngErro rcode = GoOne ( lngHandl e);
Finally, GetResult calls are used to retrieve the results (errorcodes and values) that were stored by the driver during the Go call.
This does not involve any low-level communication with the device, and thus the execution ti me is relatively instantaneous:
//Get t he resul t of th e DAC0 r equest j ust to check fo r an err orcode.
//The g eneral f orm of the GetR esult fu nction is:
//GetRe sult (Ha ndle, I OType, C hannel, *Value)
lngErro rcode = GetResu lt (lngH andle, L J_ioPUT _DAC, 0, 0);
//Get t he AIN3 voltage . We pa ss the a ddress to dblVa lue and the
//volta ge will be retu rned in that var iable.
lngErro rcode = GetResu lt (lngH andle, L J_ioGET _AIN, 3, &dblVal ue);
The AddRequest/Go/GetResult method is often the most efficient. As shown above, multiple requests ca n be executed wi th a
single Go() o r GoOne() call, and the drive r might be able to optimize the requests into fewer lo w-level calls. The other option is to
use the eGet or ePut functions which combi ne the AddRequest/Go/GetResult into one call. The above code would then lo ok like
(assuming the U3 is already open):
//Set D AC0 to 2 .5 volt s.
//The g eneral f orm of the ePut functio n is:
//ePut (Handle, IOType , Channe l, Value , x1)
lngErro rcode = ePut (l ngHandle , LJ_ioP UT_DAC, 0, 2.50 , 0);
//Read AIN3.
//The g eneral f orm of the eGet functio n is:
//eGet (Handle, IOType , Channe l, *Valu e, x1)
lngErro rcode = eGet (l ngHandle , LJ_ioG ET_AIN, 3, &dbl Value, 0 );
In the case of the U3, the first example using add/go/get handles both the DAC command and AIN read in a single low-level ca ll,
while in the second example using ePut/eGet two low-level commands are used. Examples in the following documentation will use
both the add/go/get method and the ePut/eGet method, and they are generally interchangeable. See Section 4.3 for more
pseudocode examples.
All the request and result functi ons always have 4 common parameters, and some of the functions have 2 extra parameters:
Handle – This is an input to all request/result functio ns that tells the function what LabJack it is talking to. The handle is
obtained from the OpenL abJack function.
IOType – Thi s is an input to all request/result functions that specifies what type of action is being done.
Channel – This is an input to all request/result functions that generally specifies which cha nnel of I/O i s being written/read,
although with the config IOTypes special constants are passed for channel to specify what is being configured.
Value – This is an inp ut or output to all reque st/result functions that is used to write or read the value for the item being
operated on.
x1 – This parameter is only used in some of the request/result functions, and is used whe n extra information is needed for
certain IOTypes.
UserData – This parameter is only used in some of the request/result functions, and is data that is simply passed along with
the request, and returned unmodified by the result. Can be used to store any sort of information with the request, to allow a
generic parser to determine wha t should be done when the results are received.
4.1.1 - Function Flexibility
The driver is designed to be flexible so that it can work with various different LabJacks with different capabilities. It is also
designed to work with different development platforms with different capabi lities. For thi s reason, many of the functions are
repeated with different forms of parameters, although their internal functionality remains mostly the same. In this documentation, a
group of functions will often be referred to by their shortest name. For example, a reference to Add or A ddRequest most likely
refers to any of the three variations: AddRequest(), AddRequestS() or AddRequestSS(). In the sample code, a lternate functions (S
or SS versions) can generally be substituted as desired, changing the parameter types accordingly. All samples here are wri tten in
pseudo-C. Functions with an “S” or “SS” appended are provided for programming languag es that can’t include the LabJackUD.h
file and therefore can’t use the constants i nclud ed. It is generally poor programming form to hardcode numbers i nto function calls, i f
for no other reason than it is hard to read. Functi ons with a single “S” replace the IOType parameter with a const char * which is a
string. A string can then be passed with the name of the desired constant. Functions with a double “SS” replace both the IOType
and Channel wi th strings. OpenLabJackS replaces both DeviceType and ConnectionType with strings since both take constants.
For example: In C, where the LabJackUD.h file can be included and the constants used directly:
AddRequ est(Hand le, LJ_ ioGET_CO NFIG, LJ _chHARD WARE_VER SION,0,0 ,0);
The bad way (hard to read) when LabJackUD.h cannot be included:
AddRequ est(Hand le, 100 1, 10, 0 , 0, 0);
The better way when LabJackUD.h cannot be include d, is to pass strings:
AddRequ estSS(Ha ndle, “ LJ_ioGET _CONFIG” , “LJ_c hHARDWAR E_VERSIO N”,0,0, 0);
Continuing on this vein, the function Stri ngToConstant() is useful for error handling routines, or with the GetFirst/Next functions
which d o not take strings. The StringToConstant() function takes a string and returns the numeric constant. So, for example:
LJ_ERRO R err;
err = A ddReques tSS(Han dle, “LJ _ioGETCO NFIG”, “LJ_chHA RDWARE_V ERSION” ,0,0,0);
if (err == Stri ngToCon stant(“L JE_INVAL ID_DEVI CE_TYPE” ))
do so me error handli ng..
Once again, this is much clearer than: