Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

DAQmx ANSI C read Analog 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 analog recording session of 10 seconds, with a sample rate of 10 samples a second, we would expect 100 samples.
  • For a analog recording session of 60 seconds, with a sample rate of 10 samples a second, we would expect 600 samples.

 

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

  • For a analog recording session of 10 seconds, with a sample rate of 10 samples a second, we got 76 samples.
  • For a analog recording session of 60 seconds, with a sample rate of 10 samples a second, we got 481 samples.

 

I then tried reducing the sample rate:

  • For a analog recording session of 10 seconds, with a sample rate of 1 samples a second, we got 11 samples.
  • For a analog recording session of 10 seconds, with a sample rate of 1 samples a second, we would expect 10 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 analog input. The prototype for the function will look something like this:

 

__declspec(dllexport) signed long readAnalog(const char* deviceId, int line, int sampleRate, int numberOfSamples, DOUBLE_CALLBACK analogReadingCallback, 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.
  • analogReadingCallback, 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 readAnalog function is:

  • The function should be able to read at a given sample rate, for example 25 times per second.
  • 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.

//

 

readAnalog(){

 

while shouldRead() {

 

doAnalogRead()

 

for each reading {

analogReadingCallback(reading);

}

 

if shouldRead() {

sleep()

}

}

}

 

doAnalogRead(){

 

 

DAQmxCreateTask("",&taskHandle);

DAQmxCreateAIVoltageChan(taskHandle, analogChannelId, analogChannelName, DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL);

DAQmxCfgSampClkTiming(taskHandle, "", 1000, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, 0);

DAQmxStartTask(taskHandle);

DAQmxReadAnalogF64(taskHandle, numberOfSamples, 10.0, DAQmx_Val_GroupByChannel, data, numberOfSamples, &numberOfSamplesRead, NULL);

DAQmxStopTask(taskHandle);

DAQmxClearTask(taskHandle);

}

 

In the future I will need to extend my c library to provide functions for writing DigtalOutput. As these functions will be in a library it is not possible to run the reading sessions a C program that will stopped.

I should probably also mention that in my first draft of the program I tried to create named tasks based on what type of reading was happening and on which channel/line. Prior to doing each reading I would then check to see if the task already exists, if it does, load it, stop it, and the clear it and the re-create it. This didn't seem to work. I kept getting errors about tasks with duplicate names. I then resorted to just give every task a name of "". I still have to go through the check it is exists, stop it, clear it, re-create it routine but it seems slightly more robust. Am I missing something here?

 

Thanks in advance for any help you can provide.

 

Cameron

0 Kudos
Message 1 of 2
(4,330 Views)

I have a different but related question.

 

I am trying to read 3 differential inputs (ai0, ai1, and ai3). Should I keep all three channels under one task? (this is true based on the software from NI) And how do I do it? I am trying to read 10 samples every 10ms (at 10000 or 1000 samples/second). Copy directly from the sample code, I think I could create/stop/clear one task at time, but I do not think that's the right way to do it.

 

Also, how do I have a list of available devices( to choose from), say, Dev1, Dev2, and Dev3 if I have 3 USB-6008 connected to my computer?

 

I guess a more adequate question is where I could find the documents for the APIs provided by NI. In this case, I think the parameters for DAQmxReadAnalogF64 should be able to tell which input I am reading.

 

I am new to NI products. Any help is highly appreciated. 

0 Kudos
Message 2 of 2
(1,358 Views)