LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

DAQmx DAQmxRegisterEveryNSamplesEvent when is called.

Solved!
Go to solution

HI, I am confued about when is the event DAQmxRegisterEveryNSamplesEvent  called :

Here is howa create my task, with many channels:

 

DAQmxCreateTask("",&device.taskHandle);

 

DAQmxCreateAIVoltageChan(device.taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10,10,DAQmx_Val_Volts,NULL);

DAQmxCreateAIVoltageChan(device.taskHandle,"Dev1/ai1","",DAQmx_Val_Cfg_Default,-10,10,DAQmx_Val_Volts,NULL);

DAQmxCreateAIVoltageChan(device.taskHandle,"Dev1/ai2","",DAQmx_Val_Cfg_Default,-10,10,DAQmx_Val_Volts,NULL);

 

samplesRate = 1000;

DAQmxCfgSampClkTiming(device.taskHandle,"",samplesRate,DAQmx_Val_Rising,DAQmx_Val_ContSamps,100);

                      
DAQmxRegisterEveryNSamplesEvent(device.taskHandle,DAQmx_Val_Acquired_Into_Buffer,10,0,EveryNCallback,NULL);                     

 

DAQmxRegisterDoneEvent(device.taskHandle,0,DoneCallback,NULL);
                      

data = malloc(100*numChannels*sizeof(float64));                   
DAQmxStartTask(device.taskHandle);

}

 

int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)  

{

    int numRead = 0;
    DAQmxReadAnalogF64(device.taskHandle,100,10.0,DAQmx_Val_GroupByChannel ,data,100 * numChannels,&numRead,NULL);

}

 

 

When is here fired EveryNCallback, I tough when the buffer data has 10 samples, but when i call this, it has the 100 samples readed for each channel.

If the sample rate is 1000 samples per seocnd per channel it would be 0.3 seconds for 100 samples per channel(3). And if

EveryNCallback is called every 10 samples then it should be called every 0.030 seconds ??? or I am wrong.

 

Thank you very much in advance.

 

 

 

 

 

0 Kudos
Message 1 of 7
(6,043 Views)

You have set EverNCallback to fire every 10 samples/channel acquired, but inside the function you have asked to read 100 samples/channel, so the first time the function is fired when 10 samples have been acquired, but it will be stuck in the read function until 100 samples are acquired. 

EveryNcallback receives the number of available samples as a parameter and you should pass that parameter to the read function so that it does not loose any time (the callback should return as soon as possible).



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 7
(6,040 Views)

Thank you for your quick answer. I have one more question. When DAQmxRegisterEveryNSamplesEvent is called in this example.

It will read 10 samples from each channel or it reads just the first 10 available samples from the first channel, and the next time

the event is called should be read the 10 samples from the second channel and so on. Thank you again.

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

All 'samples' here are to be inteded as 'samples per channel'. That is, you must pass an array that is at least nSamples * nChannels wide. The way samples are stored in the array, wether grouped or interleaved, depends on the option you set in DAQmxReadAnalogF64 function.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 4 of 7
(6,026 Views)

Thank you again. Another question about how to get the Information from the Channels. It is posible to call the DAQmxReadAnalogF64() in a loop without calling

DAQmxCfgSampClkTiming and DAQmxRegisterEveryNSamplesEvent??. For example so:

 

DAQmxCreateTask("",&taskHandle);

 

DAQmxCreateAIVoltageChan("Dev1/a01","",DAQmx_Val_Cfg_Default,-10,10,DAQmx_Val_Volts,NULL);
DAQmxCreateAIVoltageChan("Dev1/a02","",DAQmx_Val_Cfg_Default,-10,10,DAQmx_Val_Volts,NULL);   

 

DAQmxGetTaskAttribute(taskHandle,DAQmx_Task_NumChans,&numChannels);
DAQmxStartTask(taskHandle);

while(connected)
{

      //Here I get 100 samples per channel and then wait 1000 ms without calling DAQmxCfgSampClkTiming and DAQmxRegisterEveryNSamplesEvent
     DAQmxReadAnalogF64(device.analog.taskHandle,100,10.0,DAQmx_Val_GroupByChannel ,data,100 * numChannels,&numRead,NULL);
    
    Delay(1000);
}

 

Thank you again in advance.

0 Kudos
Message 5 of 7
(6,011 Views)
Solution
Accepted by topic author lrojas

In theory it should work, but you will have no warranty on the scan rate. You can query actual scan rate by calling DAQmxGetTimingAttribute (taskHandle, DAQmx_SampClk_Rate, &rate, 0); but if you want to modify it you must use DAQmxCfgSampClkTiming.

 

If you want to acquire on demand, you could set your task as usual (including timing set with finite amount of samples) and finish task preparation with DAQmxTaskControl (taskHandle, DAQmx_Val_Task_Commit); After this point you can have your loop or timed process that on every run calls DAQmxStartTask and reads measures



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 6 of 7
(6,001 Views)

//------It works using qt and c++, continous measurement--------------------------------------------------------------------------------------------------------------------------------------
#include <NIDAQmx.h>
//#include "C:/Program Files (x86)/National Instruments/Shared/ExternalCompilerSupport/C/include/NIDAQmx.h"
//C:/Program Files (x86)/National Instruments/Shared/ExternalCompilerSupport/C/include
//C:/Program Files (x86)/National Instruments/Shared/ExternalCompilerSupport/C/lib32/msvc

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

//--------------------------------------------------------------------------------------------------------------------------------------------

class Itechin_TPCS : public QMainWindow , public Ui::Itechin_TPCSClass // Multi Inherit yapıldı : bu sayede connect ler çalışmaya başladı,
{
Q_OBJECT

public:
Itechin_TPCS(QWidget *parent = 0);
~Itechin_TPCS();

private:

private slots:
int Acq_IntClk(void);
int Cont_Acq_IntClk(void);

}; //Ui::Itechin_TPCSClass ui;

#endif // ITECHIN_TPCS_H

 

int Itechin_TPCS::Cont_Acq_IntClk(void)
{
// DAQmxReadAnalogF64(taskHandle,numSampsPerChan,timeout,fillMode,data,arraySizeInSamps*numChannels,&sampsPerChanRead,NULL));
TaskHandle taskHandle=0;
int32 numSampsPerChan = 100; //The number of samples(1024..2^10 bit), per channel, to read. her kanaldan okunacak maximum okuma sayısı, ust limiti verir
float64 timeout = 0.1; //The amount of time, in seconds, to wait for the function to read the sample(s). To specify an infinite wait, pass -1 (DAQmx_Val_WaitInfinitely). This function returns an error if the timeout elapses.
bool32 fillMode = DAQmx_Val_GroupByChannel; //DAQmx_Val_GroupByScanNumber for cont. --DAQmx_Val_GroupByChannel

int32 numChannels = 4; //Toplam kanal sayısı
uInt32 arraySizeInSamps = numSampsPerChan
*numChannels; //The size of the array(max 1024), in samples, into which samples are read. arraySizeInSamps >=numSampsPerChan
float64 data[400]; //data[?] = data[arraySizeInSamps]. The array to read samples into, organized according to fillMode,
int32 sampsPerChanRead; //The actual number of samples read from each channel.

//DAQmxCfgSampClkTiming(taskHandle,"",rate,activeEdge,sampleMode,sampsPerChan));
int32 activeEdge = DAQmx_Val_Rising; //
int32 sampleMode = DAQmx_Val_FiniteSamps; //
float64 rate = 10000.0 * (1.0/numChannels); //The sampling rate in samples per second per channel, toplam 10kS/s so 4 channel varsa 10000/4= 2500 Hz olur set değeri
uInt64 sampsPerChan = numSampsPerChan; //sampsPerChan = numSampsPerChan

int32 error=0;
char errBuff[2048]={'\0'};


/*********************************************/
// DAQmx Configure Code
/*********************************************/
(DAQmxCreateTask("",&taskHandle));

(DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-5.0,5.0,DAQmx_Val_Volts,NULL));
(DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai1","",DAQmx_Val_Cfg_Default,-5.0,5.0,DAQmx_Val_Volts,NULL));
(DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai2","",DAQmx_Val_Cfg_Default,-5.0,5.0,DAQmx_Val_Volts,NULL));
(DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai3","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));

//DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",rate,activeEdge,sampleMode,sampsPerChan));
//(DAQmxCfgSampClkTiming(taskHandle,"",rate,activeEdge,DAQmx_Val_ContSamps,sampsPerChan));
//---------------------------------------------------------------------------------------------------------------------
//DAQmxTaskControl (taskHandle, DAQmx_Val_Task_Verify);
//DAQmxTaskControl (taskHandle, DAQmx_Val_Task_Commit); // https://forums.ni.com/t5/LabWindows-CVI/DAQmx-DAQmxRegisterEveryNSamplesEvent-when-is-called/td-p/32...
////*** Values for the Mode parameter of DAQmxTaskControl ***
//#define DAQmx_Val_Task_Start 0 // Start
//#define DAQmx_Val_Task_Stop 1 // Stop
//#define DAQmx_Val_Task_Verify 2 // Verify
//#define DAQmx_Val_Task_Commit 3 // Commit
//---------------------------------------------------------------------------------------------------------------------
/*********************************************/
// DAQmx Start Code
/*********************************************/
(DAQmxStartTask(taskHandle));

int64 e1, e2; double time, frekans;
do{
e1 = getTickCount();
QCoreApplication::processEvents(QEventLoop::AllEvents); // processes events only from the GUI thread's queue, sadece GUI den event alır

/*********************************************/
// DAQmx Read Code
/*********************************************/
(DAQmxCfgSampClkTiming(taskHandle,"",rate,activeEdge,DAQmx_Val_ContSamps,sampsPerChan));
DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,numSampsPerChan,timeout,fillMode,data,arraySizeInSamps,&sampsPerChanRead,NULL));
//DAQmxReadAnalogScalarF64 ( taskHandle, timeout, data, NULL);
//DAQmxReadCounterF64 ( taskHandle, numSampsPerChan, timeout, data, arraySizeInSamps, &sampsPerChanRead, NULL);

//qDebug() << "chan:1" << data[0] << endl; // 0-9
//qDebug() << "chan:2" << data[10] << endl; // 10-19
//qDebug() << "chan:3" << data[20] << endl; // 20-29
//qDebug() << "chan:4" << data[30] << endl; // 30-39

//qDebug() << "&sampsPerChanRead" << sampsPerChanRead << endl;

switch ( pushButton->isDown() )
{
case true:
DAQmxTaskControl (taskHandle, DAQmx_Val_Task_Stop);
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
break;
}

e2 = getTickCount();
frekans = getTickFrequency();
time = (e2 - e1)/ frekans; // result is second
qDebug() << "time(ms)=>" << time*1000 << endl;

}while(1);

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

if( taskHandle!=0 )
{
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}

if( DAQmxFailed(error) )
{
printf("DAQmx Error: %s\n",errBuff);
}

//printf("End of program, press Enter key to quit\n");
//getchar();
return 0;
}

0 Kudos
Message 7 of 7
(4,951 Views)