Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to create multiple AI tasks with NI-DAQmx Base 3.2.x in C/C++ ?

Solved!
Go to solution

I am using NI-DAQmx Base 3.2.0f1 on a Linux platform, programming in C, using a USB-6218 device. I have been using the setup successfully with  AI, AO, DI and DO tasks. I am now trying to create two (or more) AI tasks. I can create two AIVoltageChan tasks successfully (without error messages) but reading from either task using DAQmxBaseReadAnalogF64 stalls the program (no error messages, but nothing happens).

 

I have attached the following code and posed the response I get. The following code modifies acquire1Scan.c  to create two tasks:

 

 

#include "NIDAQmxBase.h"
#include <stdio.h>

#define DAQmxErrChk(functionCall) { if( DAQmxFailed(error=(functionCall)) ) { goto Error; } }

int main(int argc, char *argv[])
{
// Task parameters
int32 error = 0;
TaskHandle taskHandleA = 0;
TaskHandle taskHandleB = 0;
char errBuff[2048]={'\0'};

// Channel parameters
char chanA[] = "Dev1/ai0";
char chanB[] = "Dev1/ai15";
float64 min = -10.0;
float64 max = 10.0;

// Timing parameters
uInt64 samplesPerChan = 1;

// Data read parameters
float64 dataA, dataB;
int32 pointsToRead = 1;
int32 pointsRead;
float64 timeout = 10.0;

printf("Init Task A\n");
DAQmxErrChk (DAQmxBaseCreateTask("",&taskHandleA));
DAQmxErrChk (DAQmxBaseCreateAIVoltageChan(taskHandleA,chanA,"",DAQmx_Val_Cfg_Default,min,max,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxBaseStartTask(taskHandleA));
printf("Started Task A \n");


printf("Init Task B\n");
DAQmxErrChk (DAQmxBaseCreateTask("",&taskHandleB));
DAQmxErrChk (DAQmxBaseCreateAIVoltageChan(taskHandleB,chanB,"",DAQmx_Val_Cfg_Default,min,max,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxBaseStartTask(taskHandleB));
printf("Started Task B \n");

DAQmxErrChk (DAQmxBaseReadAnalogF64(taskHandleA,pointsToRead,timeout,DAQmx_Val_GroupByChannel,&dataA,samplesPerChan,&pointsRead,NULL));
printf ("Acquired reading A: %f\n", dataA);

// DAQmxErrChk (DAQmxBaseReadAnalogF64(taskHandleB,pointsToRead,timeout,DAQmx_Val_GroupByChannel,&dataB,samplesPerChan,&pointsRead,NULL));
// printf ("Acquired reading B: %f\n", dataB);


Error:
if( DAQmxFailed(error) )
DAQmxBaseGetExtendedErrorInfo(errBuff,2048);

if( taskHandleA!=0 ) {
DAQmxBaseStopTask(taskHandleA);
DAQmxBaseClearTask(taskHandleA);
}

if( taskHandleB!=0 ) {
DAQmxBaseStopTask(taskHandleB);
DAQmxBaseClearTask(taskHandleB);
}

if( DAQmxFailed(error) )
printf("DAQmxBase Error: %s\n",errBuff);
return 0;
}

 

The code compiles and runs, but  freezes after the first read. If the above code is called aquire2scan, the output I get is:

 

./acquire2Scan
Init Task A
Init Task B
Started Task A
Started Task B

 

 At which point the code freezes without any error messages.

 

 

I belive that you can should be able to create more then one AI task. Any ideas / fixes to this problem?

 

Thanks

Nick

 

 

 

 

0 Kudos
Message 1 of 4
(4,256 Views)
Solution
Accepted by topic author Nick Hudson

No, you cannot create multiple tasks. This is a common error that beginners make. You can only have a single task for a specific hardware resource but I do not understand why you are not getting an error.

 

If you want to read multiple channels, you have to use all of them in the same task. If they are configured the same, the syntaw would be something like Dev1/ai0:3 (for consecutive channels) or Dev1/ai0,Dev1/ai3 (for non-consecutive.

0 Kudos
Message 2 of 4
(4,252 Views)

It is unfortunate that the USB-6218, with 32 Analog input channels considers them all one 'resource', and hence can only create one AI in task. I currently use 12 Analog (6 differential) inputs for high rate (5kHz) buffered reading of a force torque sensor (using the 'Dev1/ai0,....,Dev1/ai6'  syntax). I wanted to add an occasional read of voltage on other channels (at a much lower rate) (~1Hz) but did not want to also have to have this channel buffer (and download data) at 5 kHz.

 

Just to double check, because I am using 12 A/D Channels at 5KHz, the rest of the 20 Channels (if used) on the device also need to be part of the same task and operate at the same high sample rate? There is no  way to get more then one sample rate on one device - even a $1.3 K US-6218 ?

 

I guess the only other solutions in this case it to also pair the USB-6218 with a low cost, low rate USB 6008 for other channels?

 

Thanks again for the  advice,

 

Nick

 

 

 

 

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

The reason you can only have a single task and the reason you cannot have multiple sample rates is that you have a single A/D and a single convert clock. If you were to have a dedicated A/D for each channel and separate clocks, the price would be just a wee bit higher.

 

What is usually done is to acquire at the highest rate and discard data from the other channels.

0 Kudos
Message 4 of 4
(4,230 Views)