Oct 26, 2006 3. NI-DAQmx C Reference Help. The NI-DAQmx C Reference Help is the best source of information when programming an NI-DAQmx application in a text based environment. It provides explanations for all NI-DAQmx functions and properties. The NI-DAQmx C Reference Help can be found under Start Programs National Instruments NI-DAQ.
The PyDAQmx module uses ctypes to interface with the NI-DAQmxdll. We thus advise users of PyDAQmx to read and understand thedocumentation of ctypes.
Three core modules are defined, and one higher-level object-oriented module:
- PyDAQmx.DAQmxTypes maps the types defined by National Instruments tothe corresponding ctypes types (TaskHandle,DAQmxEveryNSamplesEventCallbackPtr, etc.).
- PyDAXmx.DAQmxConstants imports all the constants defined inNIDAQmx.h (DAQmx_Val_Cfg_Default, DAQmx_Val_Rising,etc.).
- PyDAQmx.DAQmxFunctions imports all the functions defined inNIDAQmx.h (DAQmxCreateTask(),DAQmxCfgSampClkTiming(), etc.).
- PyDAQmx.Task provides an object-oriented interface to NIDAQmxtaskHandle objects. See the section Task object.
Argument types¶
All the types defined by NI in the NIDAQmx.h file are translated toctypes, and can be found in the PyDAQmx.DAQmxTypes module:
The module automatically converts variables to the right type. If a libraryfunction requires a pointer, use the byref() function to passparameters by reference.
For example the following C source:
will translate into Python as:
When looking at the C API help file or the examples provided by NI, there is analmost one-to-one relationship between the C and Python code:
- Constants can be imported from PyDAQmx.DAQmxConstants
- Variables that are not pointers can be used directly, as they will beautomatically converted by ctypes
- For pointers, first declare them and then use byref() to pass byreference
- NULL in C becomes None in Python
If numpy is installed, PyDAQmx uses numpy arrays asdataArrays instead of a ctypes array, as this is more efficient.
For example, to read a 1000 long array of float64:
C code:
PyDAQmx without numpy:
PyDAQmx with numpy (recommended):
Example¶
To consider a complete example, let’s look at the Acq-IntClk.c examplefrom the AI category(AnalogIn/MeasureVoltage/Acq-IntClk/Acq-IntClk.c):
This translates into Python as:
Note
This exemple is for Python 2. In order to use it with Python 3, you should replace strings with binary strings (for example b'Dev1/ai0')
Task object¶
The PyDAQmx package introduces an object-oriented interface to theNIDAQmx package. Basically, you replace the taskHandle mechanism with aTask object. Each function of NIDAQmx that works with ataskHandle is a method of the Task object. The method namesare the same as the NIDAQmx function names without the DAQmx at thebeginning, and the taskHandle argument of the function is omitted.
The above example now reads:
Note
DAQmxClearTask() is automatically called when a Taskinstance is garbage collected, obviating the need to clean up manually.