NI-DAQ Cards on Linux

From Mech
Revision as of 20:10, 11 June 2010 by JamesYeung (talk | contribs)
Jump to navigationJump to search

Overview

Project by: James Yeung, Master in Electrical and Computer Engineering, 2010.
Last updated: April 23, 2010

The goal of this project was to get the National Instruments Data Acquisition Cards to work on Linux. More specifically, the two cards that will be documented here are:

This wiki document assumes that you have real-time patched SUSE 11.0 operating system installed. You can follow the guide here.

Installing Drivers and C-API

The supporting software is all available on the National Instruments website. The specific package that we will need is NI-DAQmx 8.0.1. This package contains the drivers, C-API library, documentation on the library functions and example code.

Instructions

  1. Download the NI-DAQmx 8.0.1 here
    • The file that you download is an ISO, or an image of a disk. You could burn it on a CD and run it from a physical disk, but we will be mounting the ISO virtually.
  2. Install necessary supporting packages.
    • Open a terminal window. GNOME Terminal would do.
    • Use zypper to install the packages.
    • >> zypper in kernel-source-rt kernel-syms-rt
  3. Configure SUSE to startup in 4G memory mode.
    • This is not necessary if your system is 32-bit.
    • Go to the grub folder.
    • >> cd /boot/grub
    • Add "set mem=4096" to the entry.
    • >> vi menu.lst
    • Find the entry that you normally boot into and add "mem=4096M" to the end of the line that starts with "kernel".
  4. Restart the computer with 4G memory mode.
  5. Install NI-DAQmx
  6. >> cd ~/Downloads/ >> tar >> cd nidaqmx801 >> mkdir /mnt/daqmx >> mount -t iso9660 -o loop nidaqmx801.iso /mnt/daqmx >> cp /mnt/daqmx/disk . >> cd disk >> sudo ./INSTALL
    • Follow the prompts to install.
  7. Restart the computer.
  8. Restart the computer.
    • Open terminal and type
    • >> nilsdev
    • You should be able to see information about the NI-DAQ cards.

Datasheets

Example Code

To compile: gcc main.c -o main -lnidaqmx

How to Read Analog Inputs

#include <stdio.h>
#include <NIDAQmx.h>
#include <time.h>

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else

int main(void){
	int32		error=0;
	TaskHandle	taskHandle=0;
	int32		read;
	float64		data;
	char		errBuff[2048]={'\0'};
	int		ii;

	/*********************************************/
	// DAQmx Configure Code
	/*********************************************/
	DAQmxErrChk(DAQmxCreateTask("Task",&taskHandle));
	DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle,"AnalogIn/ai0","ChannelName",DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NULL));
	// To read more analog inputs, simply add more channels to the task.

	/*********************************************/
	// DAQmx Start Code
	/*********************************************/
	DAQmxErrChk(DAQmxStartTask(taskHandle));

	/*********************************************/
	// DAQmx Read Code
	/*********************************************/
	for(ii = 0; ii < 2000; ii++){
		DAQmxErrChk(DAQmxReadAnalogF64(taskHandle,1,10.0,DAQmx_Val_GroupByChannel,&data,1,&read,NULL));
		printf("%4d\t%f\n", ii, data);
	}

Error:
	if( DAQmxFailed(error) )
		DAQmxGetExtendedErrorInfo(errBuff,2048);
	if( taskHandle!=0 )  {
		/*********************************************/
		// DAQmx Stop Code
		/*********************************************/
		DAQmxStopTask(taskHandle);
		DAQmxClearTask(taskHandle);
	}
	if( DAQmxFailed(error) )
		printf("DAQmx Error: %s\n",errBuff);
	printf("End of program, press Enter key to quit\n");
	getchar();
	return 0;
}

How to Read Digital Inputs

#include <stdio.h>
#include <NIDAQmx.h>
#include <time.h>

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else

int main(void){
	int32		error=0;
	TaskHandle	taskHandle=0;
	int32		read;
	int32		readBytePerSamp;
	int8		data;
	char		errBuff[2048]={'\0'};
	int		ii;

	/*********************************************/
	// DAQmx Configure Code
	/*********************************************/
	DAQmxErrChk(DAQmxCreateTask("Task",&taskHandle));
	DAQmxErrChk(DAQmxCreateDIChan(taskHandle,"AnalogIn/port0/line0","",DAQmx_Val_ChanPerLine));
	// Use the line below to add multiple lines from the same port
	// DAQmxErrChk(DAQmxCreateDIChan(taskHandle,"AnalogIn/port0/line0:7","",DAQmx_Val_ChanPerLine));

	/*********************************************/
	// DAQmx Start Code
	/*********************************************/
	DAQmxErrChk(DAQmxStartTask(taskHandle));

	/*********************************************/
	// DAQmx Read Code
	/*********************************************/
	for(ii = 0; ii < 2000; ii++){
		DAQmxErrChk(DAQmxReadDigitalLines(taskHandle,1,10.0,DAQmx_Val_GroupByScanNumber,&data,1,&read,&readBytePerSamp,NULL));
		printf("%4d\t%d\n", ii, data);
	}

Error:
	if( DAQmxFailed(error) )
		DAQmxGetExtendedErrorInfo(errBuff,2048);
	if( taskHandle!=0 )  {
		/*********************************************/
		// DAQmx Stop Code
		/*********************************************/
		DAQmxStopTask(taskHandle);
		DAQmxClearTask(taskHandle);
	}
	if( DAQmxFailed(error) )
		printf("DAQmx Error: %s\n",errBuff);
	printf("End of program, press Enter key to quit\n");
	getchar();
	return 0;
}

How to Write Analog Outputs

#include <stdio.h>
#include <NIDAQmx.h>
#include <time.h>

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else

int main(void){
	int32		error=0;
	TaskHandle	taskHandle=0;
	int32		written;
	float64		data;
	char		errBuff[2048]={'\0'};
	int		ii;

	/*********************************************/
	// DAQmx Configure Code
	/*********************************************/
	DAQmxErrChk(DAQmxCreateTask("Task",&taskHandle));
	DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle,"AnalogOut/ai0","ChannelName",DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NULL));
	// To write more analog outputs, simply add more channels to the task.

	/*********************************************/
	// DAQmx Start Code
	/*********************************************/
	DAQmxErrChk(DAQmxStartTask(taskHandle));

	/*********************************************/
	// DAQmx Read Code
	/*********************************************/
	for(ii = 0; ii < 2000; ii++){
		data = ii % 10;
		DAQmxErrChk(DAQmxWriteAnalogF64(taskHandle,1,TRUE,10.0,DAQmx_Val_GroupByChannel,&data,&written,NULL));
		printf("%4d\t%f\n", ii, data);
	}

Error:
	if( DAQmxFailed(error) )
		DAQmxGetExtendedErrorInfo(errBuff,2048);
	if( taskHandle!=0 )  {
		/*********************************************/
		// DAQmx Stop Code
		/*********************************************/
		DAQmxStopTask(taskHandle);
		DAQmxClearTask(taskHandle);
	}
	if( DAQmxFailed(error) )
		printf("DAQmx Error: %s\n",errBuff);
	printf("End of program, press Enter key to quit\n");
	getchar();
	return 0;
}

How to Write Digital Outputs

#include <stdio.h>
#include <NIDAQmx.h>
#include <time.h>

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else

int main(void){
	int32		error=0;
	TaskHandle	taskHandle=0;
	int32		written;
	int8		data;
	char		errBuff[2048]={'\0'};
	int		ii;

	/*********************************************/
	// DAQmx Configure Code
	/*********************************************/
	DAQmxErrChk(DAQmxCreateTask("Task",&taskHandle));
	DAQmxErrChk(DAQmxCreateDOChan(taskHandle,"AnalogIn/port0/line0","",DAQmx_Val_ChanPerLine));
	// Use the line below to add multiple lines from the same port
	// DAQmxErrChk(DAQmxCreateDOChan(taskHandle,"AnalogIn/port0/line0:7","",DAQmx_Val_ChanPerLine));

	/*********************************************/
	// DAQmx Start Code
	/*********************************************/
	DAQmxErrChk(DAQmxStartTask(taskHandle));

	/*********************************************/
	// DAQmx Read Code
	/*********************************************/
	for(ii = 0; ii < 2000; ii++){
		DAQmxErrChk(DAQmxWriteDigitalLines(taskHandle,1,TRUE,10.0,DAQmx_Val_GroupByScanNumber,&data,&written,NULL));
		printf("%4d\t%d\n", ii, data);
	}

Error:
	if( DAQmxFailed(error) )
		DAQmxGetExtendedErrorInfo(errBuff,2048);
	if( taskHandle!=0 )  {
		/*********************************************/
		// DAQmx Stop Code
		/*********************************************/
		DAQmxStopTask(taskHandle);
		DAQmxClearTask(taskHandle);
	}
	if( DAQmxFailed(error) )
		printf("DAQmx Error: %s\n",errBuff);
	printf("End of program, press Enter key to quit\n");
	getchar();
	return 0;
}

Others

More example code can be found in the directory "/usr/local/natinst/nidaqmx/examples/". Full documentation on the nidaqmx API can be found at "/usr/local/natinst/nidaqmx/docs/daqmxcfunc.chm/_main.html"

Useful Links