Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

How often should a task be created and how should they be implemented in the code.

I have a general question on when and how often should tasks be created.

 

I am creating a task everytime I do a acquisition. My device is a USB-6210. I do this in a function I call so everytime the function starts out with a :

DAQmxCreateTask ("myAcqTask", &acq_taskHandle); 

and ends with:

 DAQmxClearTask (acq_taskHandle);
to close the task.

I do this because the task changes so much everytime I call it. In the two functions where I am configuring it:

DAQmxCreateAIVoltageChan(acq_taskHandle,physicalChannel, "acq_channel",DAQmx_Val_Diff, minVal, maxVal, DAQmx_Val_Volts,"");

DAQmxCfgSampClkTiming( acq_taskHandle, "OnboardClock", 1.0/sweepsInterval,DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, number_of_scans);

 

Difference are:

physicalChannel -- I might set it to ai0, ai1, ai2 ........

minVal and maxVal are different each time because I am varying the signal and want to get the best resolution

sweepsInterval is different depending on the measurement

number_of_scans is also changed at different times 

 

My question is: Is it wise to create and clear the task everytime or should I create the task only once when the application starts and clear the task when the application closes? Should I create a task for each channel or is it ok to use the one task to do all of my acquisitions for each channel (ai0-ai7)?

 

My application seems to be working well the way I have it but I am interested in find out if there would be a better way of doing this. Looking for a little advice from users who have used these DAQmx devices more than I. I remember reading somewhere that it was not a good idea to create and delete these tasks all of the time. Since I have not seen any problems I am not sure if that is valid or not.

 

 

I listed all of the code in my function below so you might have a better picture on what I am doing.

 

thanks in advance,

Don Pearce

 

unsigned short CollectArrayData_6289(float64 sweepsInterval, uInt64 number_of_scans, unsigned short list_index,
 float64 *Data_readings, double input_voltage)
{
 TaskHandle acq_taskHandle;
 char DeviceName[CHAN_TEXT_SIZE];
 char AcqMessage[ACQ_MESSAGE_LEN];
 int32 returnAcq;
 char physicalChannel[CHAN_TEXT_SIZE];
 char input_name[CHAN_TEXT_SIZE];
 float64 minVal, maxVal;
 int32 read;
 unsigned short retVal=COLLECT_ARRAY_DATA_OK;
 
 
 strcpy(DeviceName,"USB-6210");
 // create a new task
 returnAcq = DAQmxCreateTask ("myAcqTask", &acq_taskHandle);
 if( 0 != returnAcq)
 {
  DAQmxGetErrorString (returnAcq, AcqMessage, ACQ_MESSAGE_LEN);
  strcat(AcqMessage,"\n");
  Print_Debug_Message(AcqMessage);
  WriteToSelectedFile( AcqMessage, APPEND_STRING_SELECTFILE, OPEN_AND_CLOSE_FILE);
  retVal=COLLECT_ARRAY_DATA_NOK;
 }
 // create a new Analog Input Voltage Channel
 strcpy( physicalChannel,DeviceName);
 GetChannelName(list_index, input_name);
 strncat( physicalChannel, input_name,CHAN_TEXT_SIZE);
 
 GetVoltageInputName(input_voltage,&minVal,&maxVal);
 returnAcq = DAQmxCreateAIVoltageChan(acq_taskHandle,physicalChannel, "acq_channel",DAQmx_Val_Diff, minVal, maxVal, DAQmx_Val_Volts,"");
 if( 0 != returnAcq)
 {
  DAQmxGetErrorString (returnAcq, AcqMessage, ACQ_MESSAGE_LEN);
  strcat(AcqMessage,"\n");
  Print_Debug_Message(AcqMessage);
  WriteToSelectedFile( AcqMessage, APPEND_STRING_SELECTFILE, OPEN_AND_CLOSE_FILE);
  retVal=COLLECT_ARRAY_DATA_NOK;
 }

 // Configure the clock for the acquisiiton rate
 returnAcq = DAQmxCfgSampClkTiming( acq_taskHandle, "OnboardClock", 1.0/sweepsInterval,DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, number_of_scans);
 if( 0 != returnAcq)
 {
  DAQmxGetErrorString (returnAcq, AcqMessage, ACQ_MESSAGE_LEN);
  strcat(AcqMessage,"\n");
  Print_Debug_Message(AcqMessage);
  WriteToSelectedFile( AcqMessage, APPEND_STRING_SELECTFILE, OPEN_AND_CLOSE_FILE);
  retVal=COLLECT_ARRAY_DATA_NOK;
 }

 returnAcq = DAQmxStartTask (acq_taskHandle); 
 if( 0 != returnAcq)
 {
  DAQmxGetErrorString (returnAcq, AcqMessage, ACQ_MESSAGE_LEN);
  strcat(AcqMessage,"\n");
  Print_Debug_Message(AcqMessage);
  WriteToSelectedFile( AcqMessage, APPEND_STRING_SELECTFILE, OPEN_AND_CLOSE_FILE);
  retVal=COLLECT_ARRAY_DATA_NOK;
 }
  
 
 returnAcq = DAQmxReadAnalogF64(acq_taskHandle, -1, 5, DAQmx_Val_GroupByChannel, Data_readings, number_of_scans, &read,0);
 if( 0 != returnAcq)
 {
  DAQmxGetErrorString (returnAcq, AcqMessage, ACQ_MESSAGE_LEN);
  strcat(AcqMessage,"\n");
  Print_Debug_Message(AcqMessage);
  WriteToSelectedFile( AcqMessage, APPEND_STRING_SELECTFILE, OPEN_AND_CLOSE_FILE);
  retVal=COLLECT_ARRAY_DATA_NOK;
 }
 
 // Clear the task
 DAQmxClearTask (acq_taskHandle);
 
 return(retVal);
}

 

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

In general, you are better off creating one task for one acquisition. You are right in making a single task for multiple channels as you will not be able to run multiple AI tasks simultaneously on the same hardware. The driver will throw an error if you try multiple AI task at the same time stating the resource is reserved.

The thing to be careful of is if you are doing a continuous acqusition you do not want to create and clear the task everytime you acquire...you want to configure the task and do your DAQmx Read in a loop.

Hope this helps!

--Michelle

National Instruments
0 Kudos
Message 2 of 2
(2,766 Views)