From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

DAQmx ANSI C read Digital Input using NI-USB-6008

Hi Folks,

 

I have a NI DAQmx ANSI C problem which I hope someone will be able to help me with. Before I delve in to the problem I should state that I have a NI-USB-6008 device, which will be running on Windows 7, with which I wish to read digital input and analog input voltage. I will be writing an ANSI C library that will be compiled as a DLL using Microsoft Visual Studio 2010. The library will then be invoked via Java using JNA. I am an experienced Java programmer who is fairly comfortable programming in C. I have looked at the examples provided in C:\Programme\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\ and searched the forums, but havent found what I'm looking for yet. Appologies if this post is not in the correct forum. 

 

I have a program which does most of what I need it to, but I am getting some unexpected results.

 

When calling the functions I'll outline below here are the results I am getting:

  • For a digital recording session of 10 seconds, with a sample rate of 10 samples a second, I would expect 100 samples. 
  • For a digital recording session of 60 seconds, with a sample rate of 10 samples a second, I would expect 600 samples.

However after writing some unit tests I was able to confirm:

  • For a digital recording session of 10 seconds, with a sample rate of 10 samples a second, I got 95 samples.
  • For a digital recording session of 60 seconds, with a sample rate of 10 samples a second, I got 593 samples.

 

I then tried reducing the sample rate:

  • For a digital recording session of 10 seconds, with a sample rate of 1 samples a second, I would expect 10 samples.
  • For a digital recording session of 10 seconds, with a sample rate of 1 samples a second, I got 11 samples.

 

From these results I suspect that the problem lies in having to use sleep to provide the sampling rate that I am interested in.

 

I should point out that I have no issues with the Java integration, that seems to work fine.

 

Is there any NI DAQmx functions for the NI-USB-6008 device which will prevent me from calling sleep every n milliseconds?
Am I using the correct API calls?
Is my NI-USB-6008 device capable of doing what I need it to?

 

 

I want to expose a function which will read digital input. The prototype for the function will look something like this:

 

__declspec(dllexport) signed long readDigital(const char* deviceId, int line, int sampleRate, int numberOfSamples, INT_CALLBACK digitalReadingCallback, SHOULD_READ_CALLBACK shouldReadCallback, LOG_CALLBACK logCallback);

 

where:-

  • deviceId, the device id is the id of the NI-USB-6008, eg Dev0.
  • line, the numeric identifier for the particular pin on the device.
  • sampleRate, how many times per second to perform a reading.
  • numberOfSamples the number of samples to take upon each reading.
  • digitalReadingCallback, a callback function which should be called with each value that is read.
  • shouldReadCallback, a callback function which should be used to establish if measurements should still be made.
  • logCallback, a callback which can be used to pass log messages back to the Java program.

 

The desired behaviour for my readDigital function is:

  • The function should be able to read at a given sample rate, for example 25 times per second.
  • The function should should only invoke the digitalReadingCallback upon a change in the value from the digital input port.
  • It should be possible to stop the function reading from Java via means of the shouldReadCallback.

My current implementation involves:

 

//

// Please not that this is the jist of the code that I am running some code has been missed out to try to improve the readability for this post.

//

 

readDigital(){

 

DAQmxCreateTask("",&taskHandle);

DAQmxCreateDIChan(taskHandle,digitalLineId,digitalLineName,DAQmx_Val_ChanForAllLines);

DAQmxStartTask(taskHandle);

 

while shouldRead() {

 

DAQmxReadDigitalLines(taskHandle,numberOfSamples,10.0,DAQmx_Val_GroupByChannel,data,dataSizeInBytes,&numberOfSamplesReadPerLine,&numberOfBytesPerSample,NULL);()

 

for each reading {

 

if we have no previous data {

digitalReadingCallback(value)

}

else{

if value is different to previous value {

digitalReadingCallback(value)

}

else {

do nothing

}

}

}

 

 

if shouldRead() {

sleep()

}

}

 

DAQmxStopTask(taskHandle);

 

DAQmxClearTask(taskHandle);

 

}



Thanks in advance for any help you can provide.

 

Cameron

0 Kudos
Message 1 of 2
(3,711 Views)

As you can see in the specs for the 6008, the digital I/O is software timed. You simply cannot specify a sample rate. You take one sample at a time and expect some serious jitter in the sample rate due to windows.

0 Kudos
Message 2 of 2
(3,702 Views)