Skip to content

JoKeyser/Plain-Matlab-NI-DAQmx-examples

Repository files navigation

Plain Matlab NI-DAQmx examples

Examples how to use a National Instruments (NI) DAQ card with plain Matlab, without the Data Acquisition Toolbox.

Who is this for?

For anyone who wants to use their NI input/output cards with Matlab (via the NI-DAQmx driver).

Further, the examples illustrate some very basic steps on how to use Matlab's powerful function of importing dll files.

Current status and roadmap

So far, this project contains the files and descriptions by Jens Roesner and Nathan Tomlin.

The next steps are to improve the documentation, remove/improve any redundant/outdated code, and generalize the code to run on Linux.

Installation

  • FIXME: Add links for Windows and MacOS?
  • FIXME: Merge advice from Jens and Nathan.

Driver installation

Follow the NI's instructions to install the DAQmx driver for your platform.

  • For Linux, follow Installing NI Drivers and Software on Linux. For example on Ubuntu, once you installed/configured the NI repo, you should be able to sudo apt install ni-daqmx (and the recommended ni-hwcfg-utility). After installation, run sudo dkms autoinstall and reboot.

You can check the installation with the Hardware Configuration Utility (on Linux, run ni-hwcfg-utility): The utility should have discovered your NI device(s) and list them without indication of errors.

Advice from Jens Roesner:

Start by loading the NI dll: funclist = loadlibrary('nicaiu.dll', 'nidaqmx.h', 'alias', 'myni')

(You can choose any alias as long as it is a legal variable name.)

If your version of Matlab is too old to provide the loadlibrary function directly, download it here: http://www.codeproject.com/dll/MatlabGenericDll.asp?df=100&forumid=26247&exp=0&select=972623. After importing, you see the list of available functions. You can also display them by libfunctionsview('myni'). That's basically it. By using the National Instruments NI-DAQmx C Reference manual and libfunctionsview('myni') you have access to those functions. One special thing you need to keep in mind is the fact that pointers work differently in Matlab than in C. This problem can be solved by using the Matlab function libpointer. Sometimes Matlab does not require a pointer when C would. But I found that it does not hurt to specify a pointer in this case. Whether Matlab expects a pointer can be seen in the output created by libfunctionsview('myni').

Advice from Nathan Tomlin:

I had no header file (.h) on my computer, so I had to download NIDAQ from the NI website, I got this version: nidaq910f0_downloader.exe.

After installing, the C help files were then in:
C:\Program Files\National Instruments\NI-DAQ\docs\cdaqmx.chm
and the header file in:
C:\Program Files\National Instruments\NI-DAQ\DAQmx ANSI C Dev\include.

I copied the files nicaiu.dll and nidaqmx.h files to my working directory, but you can also just add their paths to the code.

Usage

The general syntax for calling DAQmx functions is:

[outputarguments] = calllib(alias, NIDAQmx_functionname, inputarguments)

Overview of scripts

The following examples were written by Jens Roesner, featuring simple analog input (AI); advanced, synchronous AI; and simple analog output (AO):

These following examples were written by Nathan Tomlin:

Known limitations and issues

The scripts in this project are based on Matlab's loadlibrary() and calllib(), which have known limitations.

  • FIXME: Matlab's missing support of library functions to work with function pointers means that callbacks are impossible, right?

Some known issues are tracked in file known_issues.md.

Compatibility

There are no guarantees that this code will work with your hardware or software.

  • Jens Roesner wrote his initial examples in 2005 for NI's M-Series cards.
  • Nathan Tomlin tested his code on Matlab 7.8.0 (R2009a) on Windows XP with a NI PCI 6220 board.
  • Johannes Keyser is testing this code on Matlab 9.7.0 (R2019b) with a PCIe-6321 card (X-Series), connected to a BNC-2110 terminal block.

Alternatives

The code in this project may not be up to your requirements.

Previous work and further resources

This project is based on code by Jens Roesner (2005) and Nathan Tomlin (2010). For some "historical context": Jens Roesner's code pre-dates the widespread support by Matlab's Data Acquisition Toolbox.

Further information

  • The NI-DAQmx C Reference Help describes the NI-DAQmx library functions, which you can use with NI data acquisition and switch devices to develop instrumentation, acquisition, and control applications.

Authors

Contributing

This project is shared as-is, with the hope to be useful.

If you find a problem, or you would like to add features, the easiest way is to open an issue or a pull request on GitHub, at https://github.com/JoKeyser/Plain-Matlab-NI-DAQmx-examples.

Licensing

Jens Roesner retro-actively licensed his code under GPL-3.0-or-later, via personal communication with Johannes Keyser.

Nathan Tomlin based his code on Jens Roesner's code and licensed it under the BSD-2-Clause license.

The documentation files are licensed under CC0-1.0.

To clarify the licensing of each file, this repository is REUSE compliant. For the legal texts, see folder LICENSES.

About

Examples how to use National Instruments (NI) DAQ cards in Matlab, without the Data Acquisition toolbox

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages