Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

How to increase sampling rate using NIDAQmx functions in VC++ code with USB6211

I have written following code for a ReadUSB621xAI() function in my VC++ real time measurement programme. This to read An Input From USB DAQ 6211. This code was taken from NIDAQmx examples.
 
void COnlineMeasurement::ReadUSB621xAI()
{
// int i;
 char    chan[12];
 int32    error ;
 error = 0;
 taskHandle = 0;
 max = 10.0;
 min = -10.0;
 samplesPerChan =-1;
 pointsToRead = -1;
 timeout = 10.0;
 char errBuff[2048]={'\0'};
 
 strDAQDeviceNo=MyConfig.DAQDeviceNo;
 sprintf(chan,"%s","Dev"+strDAQDeviceNo+"/ai0:1");

 DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
 DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,chan,"",DAQmx_Val_Diff ,min,max,DAQmx_Val_Volts,NULL));
 DAQmxErrChk (DAQmxStartTask(taskHandle));
 DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,pointsToRead,timeout,DAQmx_Val_GroupByChannel,curdata,samplesPerChan,&pointsRead,NULL));

  Error:
 if(DAQmxFailed(error))DAQmxGetExtendedErrorInfo(errBuff,2048);
 if( taskHandle!=0 )
 {
  DAQmxStopTask(taskHandle);
  DAQmxClearTask(taskHandle);
 }
MSG msg;
   if(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
   {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
   }   
}
------ -  - - - - - -
This function is called in a 'while' loop? Sampling rate obtained is only 20 S/sec. I need to increase it to at least 100 S/ses.
I wonder if I am doing something wrong. when I see in Measurement and Automation Explorer I find sampling rate of the same device very high.
0 Kudos
Message 1 of 6
(3,043 Views)
Where are you configuring your sample clock? The sampling rate is set when you configure your sample clock. Please see the DAQmxCfgSampClkTiming function.

Example: DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000));
Regards,

Chris Delvizis
National Instruments
0 Kudos
Message 2 of 6
(3,024 Views)

Dear Cris D,

Thank you for your reply. Based on some hints that I got from some other discssion forum and from you I have changed my code. The  increase in sampling rate is from 47mSec to about 16mSec per set. But this is also too slow. I require below 10 mSec per set.

I must tell you that I  started working on NiDAQmx only a few weeks ago.

I used the following code. Am I still making some mistake?

AItaskHandle=0;

DAQmxCfgSampClkTiming(AItaskHandle,NULL,10000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000);\\This makes no difference. DAQmxCreateTask("",&AItaskHandle);
DAQmxCreateAIVoltageChan(AItaskHandle,Dev3\ai0:1,"",DAQmx_Val_Diff ,-10.0,10.0,DAQmx_Val_Volts,NULL);\\only two channels
DAQmxStartTask(AItaskHandle);

while(Condition)

{

DAQmxReadAnalogF64(AItaskHandle,pointsToRead,timeout,DAQmx_Val_GroupByChannel,curdata,samplesPerChan,&pointsRead,NULL);

//Engg unit conversion, Grph plotting, writing to a textfile

}

if( AItaskHandle!=0 )
   {
   DAQmxStopTask(AItaskHandle);
   DAQmxClearTask(AItaskHandle);
   }

Looking to see your reponse.

Alok Gupta

0 Kudos
Message 3 of 6
(3,018 Views)
Alok,
 
Since your task handle is 0, configuring your sample timing this way will do nothing.   You have to configure timing after you create your task and channel.  In your case, unless you want to reuse your task over and over again, you don't have to explicitly create your task.  If you call create channel, it will poplulate your task handle for you.
 
Hope that helps.
0 Kudos
Message 4 of 6
(3,012 Views)

Dear Cris,

As per your suggestion I blocked the Taskhandle=0 statement, Secondly I have repostioned the timing function call after start task.

 DAQmxCreateTask("",&AItaskHandle);
 DAQmxCreateAIVoltageChan(AItaskHandle,chanAI,"",DAQmx_Val_Diff ,-10.0,10.0,DAQmx_Val_Volts,NULL);
 DAQmxStartTask(AItaskHandle);
 DAQmxCfgSampClkTiming(AItaskHandle,NULL,10000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,10);

while(Condition)

{

DAQmxReadAnalogF64(AItaskHandle,pointsToRead,timeout,DAQmx_Val_GroupByChannel,curdata,samplesPerChan,&pointsRead,NULL);

}

I give below few lines of data as taken from my text file where I am writing each sample within the while loop

Cycle    Time      Force      Disp             Velocity
1           0.063     -101.14    -12.38           0.00
1           0.078     -94.10      -12.39          -0.22
1           0.094     -93.91      -12.39          -0.21
1           0.109     -95.86      -12.39          -0.22

Where is the mistake in my code?

0 Kudos
Message 5 of 6
(3,002 Views)
Alok,
 
I'm not sure what you were trying to convey from your data set, but I have some more hints for you. 
 
1)  Don't call this function from a while loop.  Every iteration of this function is creating and destroying a new task and only taking one sample set.  I'm sure this isn't what you want.  Once you have your task set up and started, you can call read in a while loop and it will read data from the buffer for post processing.  I'm guessing that you wanted to read data and have another task process the data as you get it?
 
2)  Configure your timing after you create your channel but before you start your task.
0 Kudos
Message 6 of 6
(2,991 Views)