Skip to content

LabVIEW¤

Load a 64-bit LabVIEW library in 64-bit Python. A LabVIEW Run-Time Engine ≥ 2017 (of the appropriate bitness) must be installed. The LabVIEW example is only valid on Windows. To load the 32-bit library in 32-bit Python use labview_lib32.dll as the filename.

Note

A LabVIEW library can be built into a DLL using the __cdecl or __stdcall calling convention. Make sure that you specify the appropriate libtype when instantiating the LoadLibrary class for your LabVIEW library. The example library uses the C calling convention __cdecl.

Example¤

Load the example LabVIEW library

>>> from msl.loadlib import LoadLibrary
>>> from msl.examples.loadlib import EXAMPLES_DIR
>>> labview = LoadLibrary(EXAMPLES_DIR / "labview_lib64.dll")
>>> labview
<LoadLibrary libtype=CDLL path=...labview_lib64.dll>
>>> labview.lib
<CDLL '...labview_lib64.dll', handle ... at ...>

Create some data to calculate the mean, variance and standard deviation of

>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Convert data to a ctypes array and allocate memory for the parameters

>>> from ctypes import c_double
>>> x = (c_double * len(data))(*data)
>>> mean, variance, std = c_double(), c_double(), c_double()

Calculate the sample standard deviation (i.e., the third argument is set to 0) and variance. Pass mean, variance, std by reference using byref so that LabVIEW can write to the memory locations

>>> from ctypes import byref
>>> ret = labview.lib.stdev(x, len(data), 0, byref(mean), byref(variance), byref(std))
>>> mean.value
5.0
>>> variance.value
7.5
>>> std.value
2.7386127875258306

Calculate the population standard deviation (i.e., the third argument is set to 1) and variance

>>> ret = labview.lib.stdev(x, len(data), 1, byref(mean), byref(variance), byref(std))
>>> mean.value
5.0
>>> variance.value
6.666666666666667
>>> std.value
2.581988897471611

LabVIEW Source Code¤

labview_lib

labview_lib

#include "extcode.h"
#pragma pack(push)
#pragma pack(1)

#ifdef __cplusplus
extern "C" {
#endif
typedef uint16_t  Enum;
#define Enum_Sample 0
#define Enum_Population 1

/*!
 * stdev
 */
void __cdecl stdev(double X[], int32_t lenX, Enum WeightingSample, 
    double *mean, double *variance, double *standardDeviation);

MgErr __cdecl LVDLLStatus(char *errStr, int errStrLen, void *module);

#ifdef __cplusplus
} // extern "C"
#endif

#pragma pack(pop)