Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

When I acquire data using USB6008 and NI_DAQmx Base in VC++, Shall I start and stop the task in ONTimer function?

void Ctest2Dlg:nTimer(UINT nIDEvent)
{
   DAQmxErrChk (DAQmxBaseStartTask(taskHandle));
   DAQmxErrChk (DAQmxBaseReadAnalogF64 taskHandle,pointsToRead,timeout,DAQmx_Val_GroupByScanNumber,data,100,&pointsRead,NULL));
   
  DAQmxBaseStopTask (taskHandle);
...............
  }
 
If I do this, the program will halt in about half minute. But if I do not start and stop task in OnTmer function, the program will not halt and the data acquired is not corrected.
 
Please help me! Thanks
0 Kudos
Message 1 of 6
(8,024 Views)

Hello,

How do you have the task configured?  If it is in Finite mode, then the five minutes represents the time it takes to acquire the pointsToRead you have specific.  Please take a look at the NI-DAQ Base C Function Help for specifics about the DAQmxBaseReadAnalogF64 function.  This help downloads with the NI-DAQmx Base driver.  Please refer to the driver readme related to the version you have to find it specific location.

Typical structure would start by using DAQmxBaseCreateAIVoltageChan to create a channel or DAQmxBaseCreateTask to refer to an already created task in Measurement and Automation Explorer (MAX).  Then DAQmxBaseCfgSampClkTiming or DAQmxBaseCfgImplicitTiming would be used to setup the acquisition mode. Then the task would be started, read from, and stopped.  I understand that you could be doing portions of this process in other function prior to ONTimer.

From your statement “If I do this, the program will halt in about half minute. But if I do not start and stop task in OnTmer function, the program will not halt and the data acquired is not corrected.”, it sounds like you might prefer operating in a continuous mode so that samples are acquired together and can then be validated for correctness.  If this is not the issue please provide more information about the configuration.

Samantha
National Instruments
Applications Engineer
0 Kudos
Message 2 of 6
(8,008 Views)

Dear Samantha,

Thank you so much. The following is the whole thing. Maybe the reason of the problem is that I am using NI_DAQmx Base , not NI_DAQmx. Thanks.

I started from the example and did some modifications. First I met -200284 error and I found it is the caused by wrong number of data to be read. I changed to finite namber data read.

Then I met sixth reading problem, at that time I use a button clicking to read the data.when I read for the sixth time, It will occur (I forgot the phenomena). I added a

stop task function and I thought I solved the problem. I knew for a finite number data reading, stop function is not needed, but I do not have any other method so I tried it. "sixth reading problem" solved, but When I move the data reading function to ontimer function recently. The program will halt -- take 99% of CPU time after 30 second of reading (sometime several minutes, sometimes several seconds). If I move the start task back to OnInitialUpdate() function and cancel stop function, halt error will not occur but the data I read in not correct. That is the whole thing. The following is the code relative to the data read.

Initialization;

void Ctest2Dlg:nInitialUpdate()

{.....

DAQmxErrChk (DAQmxBaseCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxBaseCreateAIVoltageChan(taskHandle,chan,"",DAQmx_Val_RSE,min,max,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxBaseCfgSampClkTiming(taskHandle,clockSource,sampleRate,DAQmx_Val_Rising,DAQmx_Val_ContSamps ,samplesPerChan));

.....
}

Data read

void Ctest2Dlg:nTimer(UINT nIDEvent)

{

.....

if(taskHandle != 0)
{
DAQmxErrChk (DAQmxBaseStartTask(taskHandle));

DAQmxErrChk (DAQmxBaseReadAnalogF64(taskHandle,pointsToRead,timeout,DAQmx_Val_GroupByScanNumber,data,100,&pointsRead,NULL));

DAQmxBaseStopTask (taskHandle);
}

.....

}

 

Definition of parameter:

#include "NIDAQmxBase.h"

//////////////////////////////////////////////////

// Task parameters
int TaskOnOff = 0;
int32 error = 0;
TaskHandle taskHandle = 0;
char errBuff[2048]={'\0'};
time_t startTime;
bool32 done=0;

// Channel parameters
char chan[] = "Dev1/ai0,Dev1/ai1,Dev1/ai2,Dev1/ai3,Dev1/ai4";
float64 min = -10.0;
float64 max = 10.0;

// Timing parameters
char clockSource[] = "OnboardClock";
uInt64 samplesPerChan = 1;
float64 sampleRate = 1000.0;

// Data read parameters
#define bufferSize (uInt32)1000
float64 data[100];
int32 pointsToRead = 1;
int32 pointsRead;
float64 timeout = 3.0;
int32 totalRead = 0;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 Kudos
Message 3 of 6
(7,996 Views)

Hello,

The program is halting because the stop task is used.  When a task is stopped this clears all configurations previously set, thus the read function cannot be executed again until initialization code section is run again.  I understand that you were getting error -200284 … please refer to the KnowledgeBase (KB) linked below for more information on how to solve this error. 

AE KB 336EKTKD: Why Do I Get Error -200284 When I Set a Small Sampling Rate in DAQmx?

If you are interested in continuously acquiring data, I would suggest using continuous mode and solve the buffer issue with the linked KB.  You stated that when you move the start task back to the main function the halt is eliminated, but the data that you read is not correct.  What makes it incorrect?  Are the values offset or do the values not correlate to the specific time you wanted to sample at?  Please supply more information about why you feel the values are incorrect.

You are able to use the DAQmx driver if you application has hardware that is supported by the DAQmx driver and your operating system is also supported.  Read the readme of the driver to make sure.  However, I do not believe this is the source of the problem.

Samantha
National Instruments
Applications Engineer
0 Kudos
Message 4 of 6
(7,969 Views)
Dear samantham ,
 
Thank you so much for your reply!
But I got a little confused. I don't think StopTask command will clear the configuration, only  Cleartask will. Because when I start and stop a task each time in OnTimer function, the program will work well sometimes as 10 minute long.  " when you move the start task back to the main function the halt is eliminated, but the data that you read is not correct. ". In this case, when I changed the input voltage, the data reading will not change, obviously the data is not correct.
 
Any way I solved all the problem by using  DAQmx driver. I made a mistake that I thought I read somewhere that USB6008 can not use DAQmx driver, and also  DAQmx base comes with the device so I though NI recommend to use it. I am using windows XP. and it claimed that in DAQmx base 2.2 readme file.
 
NI-DAQmx Base 2.2 supports:
  • USB-92xx, USB-600x, and USB-6501 devices on Windows 2000/XP, Windows Vista x86, and Windows Vista x64 (LabVIEW and ANSI C API support)

Devices Supported by NI-DAQmx Base 2.2 for Windows

  • NI USB-9211 4 AI Channels, 24-bit thermocouple input module
  • NI USB-9215 4 AI Channels, ±10 VDC, 16-bit simultaneous sampling analog input module
  • NI USB-6008 12-bit, 10 kS/s, 8 AI channel, 12 DIO line, 1 event counter device --------------------------------------------------
  • NI USB-6009 14-bit, 48 kS/s, 8 AI channel, 12 DIO line, 1 event counter device
  • NI USB-6501 24 DIO line, 1 event counter device
I got confused when I read the following words in the same document:

NI-DAQmx

NI-DAQmx is the latest NI-DAQ driver with new VIs and functions and development tools for programming measurement devices. Install and use NI-DAQmx if the following situations apply:

  • You are using Windows 2000/XP/Vista

 

When I use NI-DAQmx, I did not meet any problem, everything is fine. Now from my understanding that NI-DAQmx Base 2.2 shouid be used on the system except windows. It also suport windows, but the suport is not good. Am I right? Thanks again.

Best regards

Leonard

0 Kudos
Message 5 of 6
(7,966 Views)

Hello,

Yes, NI-DAQmx Base is support on Windows systems, however there is much more functionality in the NI-DAQmx driver compared to NI-DAQmx Base.  Sorry about the confusion with DAQmx Clear versus DAQmx Stop.  Your fifth post is correct.

Samantha
National Instruments
Applications Engineer
0 Kudos
Message 6 of 6
(7,829 Views)