Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get the digital logic state using nidaqmx?

I've got some code that uses the legacy Nidaq driver.  There is one function I'm having a problem converting to Nidaqmx - Dig_In_Line.  What I need is the "state" variable from Dig_In_Line ( the 4th argument ), where "state" gets the digital logic state of a specific line, I need to do that using the new driver, the Nidaqmx.

 

I've been searching these boards and the Internet, along with the documentation files that came with the drivers, and the only thing I've come up with is using DAQmxReadDigitalLines.  BUT, I'm not able to get the current state of each line from this. 

 

So, what I'm looking for is how to get the digital logic state of each line using the Nidaqmx driver.  If anyone can help me out, I would really appreciate it.

0 Kudos
Message 1 of 4
(3,680 Views)

Hi manontheedge,

 

The DAQmxReadDigitalLines should do what you need, I would take a look at the following example to see what this might look like:

  Start >> All Programs >> National Instruments >> NI-DAQ >> Text-based Code >> ANSI C Examples >> Digital >> Read Values >> Read Digital Chan

The format of the data returned by this function is a 1D array that depends on fillMode input:

 

 Interleaving.PNG

Thanks for posting and welcome to the NI forums!

 

-John

John Passiak
0 Kudos
Message 2 of 4
(3,647 Views)

hi John, thank you very much for the reply. I have some more questions...

 

 

if I'm using the legacy driver, I can do this: 

 

********************************************

********************************************

status = DIG_In_Line(1, 3, 2, &state); 

if( state == 0 ) 

    // there's a signal detected ... this works perfectly

********************************************

********************************************

 

I made port = 3 and line = 2 just to show a more concise example

 

 

however, using Nidaqmx I'm having several problems:

********************************************

********************************************

TaskHandle taskHandle2=0;

uInt8 data[100];

int32 read,bytesPerSamp;

 

 

int x1 = DAQmxCreateTask("",&taskHandle2);

int x2 = DAQmxCreateDIChan(taskHandle2,"Dev1/port2/line0","",DAQmx_Val_ChanForAllLines);

 

 

int x3 = DAQmxStartTask(taskHandle2);

 

 

int x4 = DAQmxReadDigitalLines(taskHandle2,1,10.0,DAQmx_Val_GroupByChannel,data,100,&read,&bytesPerSamp,NULL);

 

for( int i = 0; i < 100; i++ )

{

    if( data[i] == 0 )

    {

        changed = 1;

    }

    if( data[i] == 1 )

    {

        changed = 0;

    }

}

 

int x5 = DAQmxStopTask(taskHandle2);

int x6 = DAQmxClearTask(taskHandle2);

********************************************

********************************************

 

so for this, I'm essentially looking to do the exact same thing as Dig_In_Line.  The port is now 2, the line is 0 ... this code is in a function that is called repeatedly.

 

One problem is that AFTER one time through all this code, each further iteration gives the error code -200587 for DAQmxStartTask AND DAQmxReadDigitalLines.  The rest continue to return 0.  This error code means, from my understanding that the digital lines are reserved.  I'm not understanding though why StopTask and ClearTask aren't taking care of this error.

 

Also, when I'm looping through data, the input isn't being recognized, like it is with Dig_In_Line, this also isn't making sense to me.  For the mx code, I essentially just took the code from the example. 

 

Anyway, any help or tips would be greatly appreciated.  Thanks.

0 Kudos
Message 3 of 4
(3,628 Views)

 Hi manontheedge,

 

The error you are receiving (-200587) does indicate that a resource is reserved.  This could likely be caused by Traditional DAQ, which does not release control of a DAQ Device until it is Reset.  So, if switching from Traditional DAQ to DAQmx, you would need to reset the Traditional DAQ Driver.  You can do this in Measurement and Automation Explorer by right-clicking on the Traditional NI-DAQ category as seen below:

 

device_reset.PNG

 

 

You should just need to check data[0] for the value of the line in your task, I would imagine that at the end of your for loop changed would be equal to 0 every time since you do not have data for the 100th sample.  Since you only have one channel in the task and are acquiring one sample at a time, data[0] would be the only element with actual data.

 

-John
Message Edited by John P on 06-04-2009 09:16 PM
John Passiak
0 Kudos
Message 4 of 4
(3,601 Views)