How to implement contAcquireNChan.c example from National Instrument with a mex file in matlab 2014b in mac OS

1 view (last 30 days)
I am trying to build a mex file to acquire data through a NIDAQ USB-6009 device but I am facing troubles when trying to compile the mex file. The NI drivers installed are the 14.0 version which supports 64 bits architecture according to NI (the example compiles and runs in a C Command Line Tool project in Xcode 6.1.1 on my computer)
Here is my code:
#include <math.h>
#include <matrix.h>
#include <mex.h>
#include "NIDAQmxBase.h"
#include <stdio.h>
#include <time.h>
#define DAQmxErrChk(functionCall) { if( DAQmxFailed(error=(functionCall)) ) { goto Error; } }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Task parameters
int32 error = 0;
TaskHandle taskHandle = 0;
char errBuff[2048]={'\0'};
int32 i,j;
time_t startTime;
bool32 done=0;
// Channel parameters
char chan[] = "Dev1/ai0, Dev1/ai1";
float64 min = -10.0;
float64 max = 10.0;
// Timing parameters
char clockSource[] = "OnboardClock";
uInt64 samplesPerChan = 1000;
float64 sampleRate = 5000.0;
// Data read parameters
#define bufferSize (uInt32)1000
float64 data[bufferSize * 2];
int32 pointsToRead = bufferSize;
int32 pointsRead;
float64 timeout = 10.0;
int32 totalRead = 0;
printf("Example will halt automatically.\n");
DAQmxErrChk (DAQmxBaseCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxBaseCreateAIVoltageChan(taskHandle,chan,"",DAQmx_Val_Cfg_Default,min,max,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxBaseCfgSampClkTiming(taskHandle,clockSource,sampleRate,DAQmx_Val_Rising,DAQmx_Val_ContSamps,samplesPerChan));
DAQmxErrChk (DAQmxBaseCfgInputBuffer(taskHandle,200000)); //use a 100,000 sample DMA buffer
DAQmxErrChk (DAQmxBaseStartTask(taskHandle));
// The loop will quit after 10 seconds
startTime = time(NULL);
while( time(NULL)<startTime+10 ) {
DAQmxErrChk (DAQmxBaseReadAnalogF64(taskHandle,pointsToRead,timeout,DAQmx_Val_GroupByScanNumber,data,bufferSize*2,&pointsRead,NULL));
totalRead += pointsRead;
printf("Acquired %ld samples. Total %ld\n",pointsRead,totalRead);
// Just print out the first 10 points of the last data read
for (i = 0; i < 10; ++i)
printf ("data0[%ld] = %f\tdata1[%ld] = %f\n",i,data[2*i],i,data[2*i+1]);
}
printf("\nAcquired %ld total samples.\n",totalRead);
Error:
if( DAQmxFailed(error) )
DAQmxBaseGetExtendedErrorInfo(errBuff,2048);
if(taskHandle != 0) {
DAQmxBaseStopTask (taskHandle);
DAQmxBaseClearTask (taskHandle);
}
if( DAQmxFailed(error) )
printf ("DAQmxBase Error %ld: %s\n", error, errBuff);
}
And the errors I get while compiling:
Error using mex
Undefined symbols for architecture x86_64:
"_DAQmxBaseCfgInputBuffer", referenced from:
_mexFunction in helloWorld.o
"_DAQmxBaseCfgSampClkTiming", referenced from:
_mexFunction in helloWorld.o
"_DAQmxBaseClearTask", referenced from:
_mexFunction in helloWorld.o
"_DAQmxBaseCreateAIVoltageChan", referenced from:
_mexFunction in helloWorld.o
"_DAQmxBaseCreateTask", referenced from:
_mexFunction in helloWorld.o
"_DAQmxBaseGetExtendedErrorInfo", referenced from:
_mexFunction in helloWorld.o
"_DAQmxBaseReadAnalogF64", referenced from:
_mexFunction in helloWorld.o
"_DAQmxBaseStartTask", referenced from:
_mexFunction in helloWorld.o
"_DAQmxBaseStopTask", referenced from:
_mexFunction in helloWorld.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Answers (2)

Geoff Hayes
Geoff Hayes on 6 Mar 2015
Antoine - how are you building/compiling the above code? The error messages are telling you that you are trying to use functions that haven't been linked with your compiled objects. When you build/compile the function, you will have to link to the libraries that have the _DAQmxBaseCfgInputBuffer, _DAQmxBaseCfgSampClkTiming, etc. functions. I suspect that this linking is taken care of in the Xcode project, so you may want to look more closely at how that has been set up and determine which libraries you will need to include in your
mex helloWorld.c
build command.

Antoine
Antoine on 6 Mar 2015
Edited: Antoine on 6 Mar 2015
Sorry, I forgot to add my building line, here it is:
mex -v helloWorld.c -I"/Applications/National Instruments/NI-DAQmx Base/includes" -L"/Library/Frameworks/nidaqmxbase.framework" -L"/Library/Frameworks/nidaqmxbaselv.framework"
I am referring to this link http://digital.ni.com/public.nsf/allkb/86402BA624A44543862571000001D8BF for the libraries to add.

Categories

Find more on Instrument Control Toolbox Supported Hardware in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!