From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

DAQmxRegisterEveryNSamplesEvent how to pass the data to the main application

I want to write a GUI Application (C++, Qt) with using this function. I used the sample code which comes with the NI installation:

 

C:\Users\Public\Documents\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Events\Every N Samples

 

But I'm not able to bring the data to my main application, as can't access any functions or variables on my application, as well as qDebug() calls also don't work.

 

Here the sample code I use to test:

 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
#include <nidaqmx.h>

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
initNI();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::initNI()
{
int32 error=0;
TaskHandle taskHandle=0;
char errBuff[2048]={'\0'};

qDebug() << "Hallo";
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAIThrmcplChan(taskHandle,"cDAQ1Mod2/ai0","",0.0,100.0,DAQmx_Val_DegC,DAQmx_Val_K_Type_TC,DAQmx_Val_BuiltIn,25.0,""));
DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"cDAQ1Mod1/ai5","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",1000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));

DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,100,0,EveryNCallback,NULL));
DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,NULL));

/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));
//qDebug() << DAQmxStartTask(taskHandle);

qDebug() << "Acquiring samples continuously. Press Enter to interrupt\nTaskhandle: " << taskHandle << "\n";
getchar();

Error:
if( DAQmxFailed(error) )
{
DAQmxGetExtendedErrorInfo(errBuff,2048);
qDebug() << QString("Fehler: %1").arg(errBuff);
}
if( taskHandle!=0 ) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if( DAQmxFailed(error) )
qDebug() << QString("DAQmx Error: %1\n").arg(errBuff);
printf("End of program, press Enter key to quit\n");
getchar();
return;
}

int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)
{
int32 error=0;
char errBuff[2048]={'\0'};
static int totalRead=0;
int32 read=0;
float64 data[2000];
uInt32 i;
qDebug() << "Callback";
/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,-1,10.0,DAQmx_Val_GroupByScanNumber,data,2000,&read,NULL));

if( read>0 ) {
for (i = 0 ; i < 2*read ; i++ ) {
printf("%.5f\n", data[i]);

}
qDebug() << QString("Acquired %1 samples. Total %2\r").arg(read).arg(totalRead+=read);
//printf("Acquired %d samples. Total %d\r",read,totalRead+=read);
fflush(stdout);
Hallo(); // this function cannot be called
}

Error:
if( DAQmxFailed(error) ) {
DAQmxGetExtendedErrorInfo(errBuff,2048);
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
qDebug() << QString("DAQmx Error: %s\n").arg(errBuff);
}
return 0;
}

int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData)
{
int32 error=0;
char errBuff[2048]={'\0'};

// Check to see if an error stopped the task.
DAQmxErrChk (status);

Error:
if( DAQmxFailed(error) ) {
DAQmxGetExtendedErrorInfo(errBuff,2048);
DAQmxClearTask(taskHandle);
printf("DAQmx Error: %s\n",errBuff);
}
return 0;
}

void MainWindow::Hallo()
{
qDebug() << "Hallo Callback";
}

0 Kudos
Message 1 of 1
(464 Views)