Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Data Acquisition and Plotting using USB-6000 and Qt 5

Hi,

This is my first question so please bear with me.

I wanted to know if anyone tried to plot the data(analog voltage In) continuously acquired from the USB-6XXX on the QwtPlot?

If yes what steps should one follows!

I'm using Qt 5 and cmake version 3.5.1 for building the binaries.

This is mydaq.h file,

 

#ifndef MYDAQ_H
#define MYDAQ_H
#include <QtCore>
#include <NIDAQmx.h>

class MyDaq: public QThread {
    Q_OBJECT
public:
    MyDaq();
    virtual ~MyDaq();
    void run();
    TaskHandle taskHandle;
    void get_data(int analogInput);
    int32 EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType,
        uInt32 nSamples, void *callbackData);
    int32 DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);
    int i;
private:
    int someVAr;
    
    
signals:
    // Indicate plotting of data
    void plot_graph( double );
    
};

#endif /* MYDAQ_H */

 

 

and this is the mydaq.cpp file,

 

#include "mydaq.h"
#include <iostream>

using std::cout;
using std::endl;

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

}

MyDaq::~MyDaq() {
    DAQmxStopTask(taskHandle);
    DAQmxClearTask(taskHandle);
}

void MyDaq::
run() {
    cout << "Thread Running" << endl;
    get_data( 1 );
}


int32 CVICALLBACK EveryNCallbackWrapper(TaskHandle taskHandle,
        int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData ) {
    MyDaq *this_ = reinterpret_cast<MyDaq *>(callbackData);
    return this_->EveryNCallback(taskHandle, everyNsamplesEventType,nSamples,
            callbackData);
}
int32 CVICALLBACK DoneCallbackWrapper(TaskHandle taskHandle, int32 status, 
        void *callbackData) {
    MyDaq *this_ = reinterpret_cast<MyDaq *>(callbackData);
    return this_->DoneCallback(taskHandle, status, callbackData);
}

int32 MyDaq::
EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType,
        uInt32 nSamples, void *callbackData) {
	int32       error=0;
	char        errBuff[2048]={'\0'};
	static int  totalRead=0;
	int32       read=0;
	double     data[1000];
	/*********************************************/
	// DAQmx Read Code
	/*********************************************/        
	DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000,10.0,
                DAQmx_Val_GroupByScanNumber,data,1000,&read,NULL));
	if( read>0 ) {
		printf("Acquired %d samples. Total %d\r",(int)read,
                        (int)(totalRead+=read));
                // plot_graph() signal will be caught by GUI for plotting the data.
// for( int j = 0; j < 999 ; j++ ) // emit plot_graph(data [j]); fflush(stdout); } Error: if( DAQmxFailed(error) ) { DAQmxGetExtendedErrorInfo(errBuff,2048); /*********************************************/ // DAQmx Stop Code /*********************************************/ DAQmxStopTask(taskHandle); DAQmxClearTask(taskHandle); printf("DAQmx Error: %s\n",errBuff); } return 0; } int32 MyDaq:: 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 MyDaq:: get_data(int analogInput) { int32 error=0; // TaskHandle taskHandle=0; char errBuff[2048]={'\0'}; /*********************************************/ // DAQmx Configure Code /*********************************************/ DAQmxErrChk (DAQmxCreateTask("",&taskHandle)); DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","", DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL)); DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0, DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000)); DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle, DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallbackWrapper,NULL)); DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallbackWrapper, NULL)); /*********************************************/ // DAQmx Start Code /*********************************************/ DAQmxErrChk (DAQmxStartTask(taskHandle)); printf("Acquiring samples continuously. Press Enter to interrupt\n"); getchar(); 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"); } void MyDaq:: logData( double data ) { cout << "data:" << data<< endl; }

MainWindow simply creates a thread of "MyDaq" and on button click starts it. I'm able to log the data acquired from the USB-6000 but when I try to emit signal "plot_graph( double )" containing data which is supposed to be caught by the slot of Gui for plotting the input voltage, it crashes. Emitting signal from the callback function always result in crash.

 

Any help will be useful.

Thanks and regards,

Prathik.Smiley Happy

 

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

Hi community,

Can anyone please tell why emitting signal from callback function results in crash. If I read analog input inside while loop and emit signal from while loop, it works.

Please find the code snippet below,

 

    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",
            DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
    DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,
            DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));
    while( stop ) {
        DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000,10.0,
                DAQmx_Val_GroupByScanNumber,data,1000,&read,NULL));
        
	if( read>0 ) {
            printf("Acquired %d samples. Total %d\r",(int)read,
                    (int)(totalRead+=read));
            // plot_graph signal will be caught by GUI for plotting the data
                for(int  j =0; j < 99; j++) {
                     emit plot_graph(data[j]);
                }            
            fflush(stdout); 
	}
    }

I'm able to plot graph(time vs voltage) but I don't know if using "DAQmxReadAnalogF64(..) " inside while loop (instead of callback function) to read analog input is a correct way to do things.

If anyone can point in right direction it will be of great help.

Thanks,

Prathik

 

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