Difference between revisions of "NI-DAQ Cards on Linux"
From Mech
Jump to navigationJump to searchJamesYeung (talk | contribs) |
JamesYeung (talk | contribs) |
||
Line 65: | Line 65: | ||
</li> |
</li> |
||
</ol> |
</ol> |
||
==Example Code== |
|||
To compile: gcc main.c -o main -lnidaqmx |
|||
===How to Read Analog Inputs=== |
|||
<pre> |
|||
#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; |
|||
} |
|||
</pre> |
|||
===How to Read Digital Inputs=== |
|||
<pre> |
|||
#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; |
|||
} |
|||
</pre> |
|||
===How to Write Analog Outputs=== |
|||
<pre> |
|||
#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; |
|||
} |
|||
</pre> |
|||
===How to Write Digital Outputs=== |
|||
<pre> |
|||
#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; |
|||
} |
|||
</pre> |
|||
==Useful Links== |
==Useful Links== |
Revision as of 19:02, 11 June 2010
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:
- NI PCI-6220 16-Bit, 250 kS/s, 16 Analog Inputs
- NI PCI-6713 12-Bit, 1 MS/s per Channel Analog Output Board
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
- 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.
- 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
- 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".
- Restart the computer with 4G memory mode.
- Install NI-DAQmx >> 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.
- Restart the computer.
- Restart the computer.
- Open terminal and type
- >> nilsdev
- You should be able to see information about the NI-DAQ cards.
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; }
Useful Links
- http://decibel.ni.com/content/docs/DOC-2808
- Datasheet for 6220
- /usr/local/natinst/nidaqmx/docs/
- /usr/local/natinst/nidaqmx/examples/