Randomly crashing using the daq.ni.NIDAQmx Matlab class

9 views (last 30 days)
Hi all,
I'm currently trying to interface our NI X-Series hardware (PCIe-6363) using Matlab. Workstation is a dual-core Windows 7 (32-bit) machine with DAQmx 9.3 and Matlab 2011a installed.
Unfortunately Matlab's Data Acquisition Toolbox does not provide access to all the DAQmx-driver functionality I need. Accordingly, as proposed in various DAQmx examples on the Matlab file exchange I tried to directly interface the DAQmx dll via Matlab's loadlibrary functionality. In general loading the library and querying information from the hardware works without a problem. Once I'm trying to create tasks things become messy though. Matlab crashes randomly...
I also tried using the daq.ni.NIDAQmx class as proposed here
hoping for more robustness.
Unfortunately, using the handle returned by
[err_code, task_handle] = daq.ni.NIDAQmx.DAQmxCreateTask( char(0), uint64(0) )
again leads to random crashes. I'm not doing anything fancy yet, just reading some AI from a channel and plotting the output.
%##########################################################################
%
% Test script to evaluate accessing DAQmx functionaliy via the shared
% library interface.
%-------------------------------------------------------------------------
%##########################################################################
clc;
clear all;
close all;
clear global;
%%Load DAQmx constants
%DAQmxInitialize();
DAQmx_Val_WaitInfinitely = -1;
DAQmx_Val_GroupByChannel = 0;
DAQmx_Val_FiniteSamps = 10178;
DAQmx_Val_Rising = 10280;
DAQmx_Val_Task_Verify = 2;
DAQmx_Val_Volts = 10348;
DAQmx_Val_Diff = 10106;
%%Reset the device
display( '### Resetting Device ###' );
device_name = 'Dev1';
[err_code] = daq.ni.NIDAQmx.DAQmxResetDevice('Dev1');
%DAQmxDisplayErrMsg( err_code );
%%Get device info
display( '### Getting Device Info ###' );
[DevTypeSize,~] = daq.ni.NIDAQmx.DAQmxGetDevProductType( device_name, ...
char(0), ...
uint32(0));
[err_code,DevType] = daq.ni.NIDAQmx.DAQmxGetDevProductType( device_name, ...
blanks(DevTypeSize), ...
uint32(DevTypeSize) );
%%Create a task
display( '### Creating Task ###' );
th = uint64(0);
[err_code, th] = daq.ni.NIDAQmx.DAQmxCreateTask( char(0), uint64(0) );
%DAQmxDisplayErrMsg( err_code );
task_handle = uint64(th);
display( ['Created TaskHandle (' num2str( task_handle ) ')']);
display( ['### Getting Task Name ###' '(' num2str( task_handle ) ')'] );
task_name = char(zeros(1,1024));
[err_code, task_name] = daq.ni.NIDAQmx.DAQmxGetTaskName( task_handle, char(1024), uint32(1024) );
%DAQmxDisplayErrMsg( err_code );
display( ['TaskHandle (' num2str( task_handle ) ') = ' task_name]);
%%Configure the task as AI
display( ['### Creating AI Channel ###' '(' num2str( task_handle ) ')']);
% Add an analog input channel for testing
ai_config.PhysicalChannel = 'Dev1/ai0';
ai_config.ChannelName = char(0);
ai_config.TerminalConfig = int32(DAQmx_Val_Diff);
ai_config.MinValue = double(-5);
ai_config.MaxValue = double(5);
ai_config.Units = int32(DAQmx_Val_Volts);
ai_config.CustomScaleName = char(0);
[err_code] = daq.ni.NIDAQmx.DAQmxCreateAIVoltageChan( task_handle, ...
ai_config.PhysicalChannel, ...
ai_config.ChannelName, ...
ai_config.TerminalConfig, ...
ai_config.MinValue, ...
ai_config.MaxValue, ...
ai_config.Units, ...
ai_config.CustomScaleName );
%DAQmxDisplayErrMsg( err_code );
%%Verify the task
display( ['### Veryfing Task ###' '(' num2str( task_handle ) ')'] );
[err_code] = daq.ni.NIDAQmx.DAQmxTaskControl( task_handle, ...
int32(DAQmx_Val_Task_Verify) );
%DAQmxDisplayErrMsg( err_code );
%%Configure the channel
display( ['### Configuring Channel ###' '(' num2str( task_handle ) ')'] );
ai_config.SampleRate = double( 1000 );
ai_config.ClockSourceTerminal = 'OnboardClock';
ai_config.ActiveEdge = int32(DAQmx_Val_Rising);
ai_config.SampleMode = int32(DAQmx_Val_FiniteSamps);
ai_config.SamplesToAcquire = uint64( 1000 );
[err_code] = daq.ni.NIDAQmx.DAQmxCfgSampClkTiming( task_handle, ...
ai_config.ClockSourceTerminal, ...
ai_config.SampleRate, ...
ai_config.ActiveEdge, ...
ai_config.SampleMode, ...
ai_config.SamplesToAcquire );
%DAQmxDisplayErrMsg( err_code );
%%Trigger data acquisition
display( ['### Starting Task ###' '(' num2str( task_handle ) ')'] );
[err_code] = daq.ni.NIDAQmx.DAQmxStartTask( task_handle );
%DAQmxDisplayErrMsg( err_code );
display( ['### Waiting... ###' '(' num2str( task_handle ) ')'] );
time_to_wait = double(-1);
[err_code] = daq.ni.NIDAQmx.DAQmxWaitUntilTaskDone( task_handle, time_to_wait );
%DAQmxDisplayErrMsg( err_code );
%%Read the data into local buffer
display( ['### Reading Data ###' '(' num2str( task_handle ) ')'] );
ai_config.FillMode = uint32(DAQmx_Val_GroupByChannel);
ai_config.Timeout = double(DAQmx_Val_WaitInfinitely);
ai_config.NumSamplesPerChannel = int32(1000);
ai_config.BufferSize = uint32(1000);
ai_config.Data = double(zeros(ai_config.BufferSize, 1 ));
ai_config.SampsPerChanRead = int32(0);
ai_config.ReservedBuffer = uint32(0);
[err_code, ...
ai_config.Data, ...
ai_config.SampsPerChanRead, ...
ai_config.ReservedBuffer] = ...
daq.ni.NIDAQmx.DAQmxReadAnalogF64( task_handle, ...
ai_config.NumSamplesPerChannel, ...
ai_config.Timeout, ...
ai_config.FillMode, ...
ai_config.Data, ...
ai_config.BufferSize, ...
ai_config.SampsPerChanRead, ...
ai_config.ReservedBuffer );
%DAQmxDisplayErrMsg( err_code );
%%Stop the acquisition task
display( ['### Stopping Task ###' '(' num2str( task_handle ) ')'] );
[err_code] = daq.ni.NIDAQmx.DAQmxStopTask( task_handle );
%DAQmxDisplayErrMsg( err_code );
%%Display results
display( ['### Plotting ###' '(' num2str( task_handle ) ')'] );
figure(1);
time_step = double(1.0/ai_config.SampleRate);
start_step = time_step;
stop_step = double(time_step*ai_config.NumSamplesPerChannel);
plot( start_step:time_step:stop_step, ...
ai_config.Data );
xlabel('Time [s]');
ylabel('Measurement [V]');
%%Clean up
display( ['### Cleaning Up ###' '(' num2str( task_handle ) ')'] );
[err_code] = daq.ni.NIDAQmx.DAQmxClearTask( task_handle );
%DAQmxDisplayErrMsg( err_code );
task_handle = [];
th = [];
clear th task_handle;
[err_code] = daq.ni.NIDAQmx.DAQmxResetDevice( device_name );
%DAQmxDisplayErrMsg( err_code );
%%--- end of file
Note that at times the full code sequence executes flawlessly and at times Matlab crashes completely. When matlab crashes, seems to be random again (usually when passing the task_handle to a daq.ni.NIDAQmx-function).
I'd greatly appreciate if anyone would be willing to copy the code and test it with their own hardware or could offer some advice/hints. It's not clear to me at the moment if my code is faulty, there is a problem with the DAQmx-drivers (accessing the hardware via MAX works without a problem) or if this is a Matlab/DAQmx bug.
Thank you very much!
Best, Christoph

Answers (2)

Sean de Wolski
Sean de Wolski on 8 Jul 2011
Is the DAQ system plugged in and working BEFORE you start MATLAB? If it is, is it unplugged at all during the MATLAB session?
This could cause a crash. Just a thought...
  1 Comment
Chris
Chris on 11 Jul 2011
Hi & thanks for the idea,
The NI-card is directly mounted inside our workstation and operational before starting Matlab. Using the NI "Measurement & Automation Explorer" never lead to any problems e.g. creating tasks, reading input etc.
The hardware and the NI-drivers seem to work fine.
Digging a bit deeper in the NIDAQmx class of Matlab I found that the class is forwarding my calls to a mex interface for example:
[status, taskHandle]= mexNIDAQmx('DAQmxCreateTask', taskName, taskHandle);
[status, data]= mexNIDAQmx('DAQmxGetTaskName', taskHandle, data, bufferSize);
etc. (see NIDAQmx.m)
Righ now I'm suspecting that something might be going wrong with the memory holding the TaskHandle allocated on the mex/C-side of things.
Anyone ever had similar issues with interfacing an external C-library from matlab (E.g. crashes when passing back and forth pointers to memory allocated in C between Matlab and a library)?
Best,
Christoph

Sign in to comment.


Thomas
Thomas on 30 Sep 2011
Hi Chris,
I am experiencing the same problem as you (Matlab2008b 32bit, Nidaqmx 9.4, NI M-Series 6259-PCI, Win7 64bit). Until around 3 months ago everything went fine (Analog In/Out, DIO, Counting). Now Matlab crashes everytime I try to create a task after loading the Nidaqmx DLL. After execution of the following lines Matlab crashes with the Windows message 'Matlab stopped working'.
[notf,warnings]=loadlibrary('nicaiu.dll','nidaqmx.h');
task1=uint32(1);
taskname='dummy_task';
a = calllib('nicaiu','DAQmxCreateTask',taskname,task1);
Using the MAX Explorer of NI the card works fine.
Chris, could you find a solution to your problem? Is there anyone else having this problem or an idea how to solve it?
Regards, Thomas

Community Treasure Hunt

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

Start Hunting!