Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

Digital Input with NI DAQmx using Matlab

Hi,

 

i'm having a problem reading the digital input functions from NI-DAQmx in MATLAB (using calllib functions from the DLL).

I use the  NI USB-6251 BNC device.

 

So, i want to read 4 digital pins (0..3) and stop reading until one of these pins are set HIGH.

 

To do this, i'm going this way:

   1. create DI task

   2. create DI channel

   3. in a while-loop, use the  DAQmxReadDigitalU32 function to read the digital values.

 

but that doesn't work. i also tried triggering and timing, but it's always the same thing:

 

I don't get any values (just zeroes), even if all pins have a 5V input. The pvalues1 variable also returns 0, which means that no values have been read.

 

Digital output and analog input/output works fine!

 

Did i make a mistake in the source code? I couldn't find any useful information on the NI homepage or on google about digital input in Matlab or ANSI C.

 
can anybody help me?

 

Thanks!

 

    Jan P.

 


Here's my source code:



function [ReadHandle, values] = NI_Start_DITask(values)

DAQmx_Val_ChanForAllLines = 1;
DAQmx_Val_FiniteSamps = 10178;
DAQmx_Val_GroupByChannel = 0;
DAQmx_Val_ChanPerLine = 0;
DAQmx_Val_Rising=int32(10280);
DAQmx_Val_Falling=int32(10171);
DIPtr=libpointer('uint32Ptr', 0);

%int32 DAQmxCreateDIChan (TaskHandle taskHandle, const char lines[], const
%char nameToAssignToLines[], int32 lineGrouping);

[a,b,DIPtr] = calllib('myni','DAQmxCreateTask','',DIPtr); % create task
DIPtr=libpointer('uint32Ptr',DIPtr);
b = calllib('myni','DAQmxCreateDIChan',get(DIPtr,'Value'),'Dev1/port0/line0:3','', ...
DAQmx_Val_ChanForAllLines); % create DI channel

d = calllib('myni','DAQmxStartTask',DIPtr);

ReadHandle = DIPtr;



and then in the main function

...

values = zeros(1,1000);
p1=0;

vptr=libpointer('uint32Ptr', values);
pvalues1 = libpointer('int32Ptr',p1);
[ReadHandle, values] = NI_Start_DITask(values); % open function above

pv = libpointer('uint32Ptr', 0);

while 1
calllib('myni','DAQmxReadDigitalU32',ReadHandle,10,10,0,values,4000,...
pvalues1,0);
p1
values

if pv.Value==1
break
elseif any(values)
break
end
drawnow
end

NI_stopAndClearTask(ReadHandle);
NI_ResetHW('Dev1');

Message Edited by ajn3000 on 12-01-2009 01:37 PM
Message Edited by ajn3000 on 12-01-2009 01:37 PM
0 Kudos
Message 1 of 11
(8,048 Views)

i forgot to mention that i get the error code -200088 when i create the DI channel.

i tried adding the "DAQmxIsTaskDone" between creating the task and creating the DI channel, but that function returns the same error code,

even if i use a zero-pointer when opening the DAQmxIsTaskDone function.

 

0 Kudos
Message 2 of 11
(8,016 Views)
problem solved.
0 Kudos
Message 3 of 11
(8,006 Views)
How did you solve it?
0 Kudos
Message 4 of 11
(7,830 Views)
I'm getting the same error (-200088) whenever I try to add any channels. How did you fix it?
0 Kudos
Message 5 of 11
(7,419 Views)

I finally got it working - no more error -200088 for me!

 

I had to pass the initial task handle as empty - uint32([]). This is different from code here. This is the code that worked for me:


 

taskhDO0 = [];
[err,b,taskhDO0] = calllib('myni','DAQmxCreateTask','DO0',uint32(taskhDO0))
[err,b,c,d] = calllib('myni','DAQmxCreateDOChan',taskhDO0,'Dev1/port0/line0','',DAQmx_Val_ChanPerLine)

 

 

I am using matlab 7.8.0 (R2009a) on XP. 

 

0 Kudos
Message 6 of 11
(7,397 Views)


nathancolorado wrote:

I finally got it working - no more error -200088 for me!

 

I had to pass the initial task handle as empty - uint32([]). This is different from code here. This is the code that worked for me:


 

taskhDO0 = [];
[err,b,taskhDO0] = calllib('myni','DAQmxCreateTask','DO0',uint32(taskhDO0))
[err,b,c,d] = calllib('myni','DAQmxCreateDOChan',taskhDO0,'Dev1/port0/line0','',DAQmx_Val_ChanPerLine)

 

 

I am using matlab 7.8.0 (R2009a) on XP. 

 


 
 Odd, when I tried that I got error -200604 ("NULL pointer was passed for a required parameter.") when creating the task. I'm running MATLAB R2009b 32-bit on Win 7 32-bit, NI-DAQmx 9.1.0.
 
 

 

Message Edited by lid on 06-01-2010 07:17 PM
0 Kudos
Message 7 of 11
(7,147 Views)
The code that works for me is posted here.
Message 8 of 11
(7,142 Views)

nathancolorado wrote:
The code that works for me is posted here.

 

 

Thanks for the reply! Using that code, I get: 

 

??? Error using ==> DAQmxCheckError at 24

DAQmx error - NULL pointer was passed for a required parameter.


Error in ==> DAQmxCreateDOChan at 28

DAQmxCheckError(lib,err); 

0 Kudos
Message 9 of 11
(7,139 Views)

nathancolorado wrote:
The code that works for me is posted
here.

 

 

Also, which version of NI-DAQmx are you using? Thanks!

0 Kudos
Message 10 of 11
(7,130 Views)