⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contacq-intclk.c

📁 数据采集DAQ是示例
💻 C
字号:
/*********************************************************************
*
* CVI Example program:
*    ContAcq-IntClk.c
*
* Example Category:
*    AI
*
* Description:
*    This example demonstrates how to acquire a continuous amount of
*    data using the DAQ device's internal clock.
*
* Instructions for Running:
*    1. Select the physical channel to correspond to where your
*       signal is input on the DAQ device.
*    2. Enter the minimum and maximum voltage range.
*    Note: For better accuracy try to match the input range to the
*          expected voltage level of the measured signal.
*    3. Set the rate of the acquisition. Also set the Samples per
*       Channel control. This will determine how many samples are
*       read each time the while loop iterates. This also determines
*       how many points are plotted on the graph each iteration.
*    Note: The rate should be at least twice as fast as the maximum
*          frequency component of the signal being acquired.
*
* Steps:
*    1. Create a task.
*    2. Create an analog input voltage channel.
*    3. Set the rate for the sample clock. Additionally, define the
*       sample mode to be continuous.
*    4. Call the Start function to start the acquistion.
*    5. Read the data in a loop until the stop button is pressed or
*       an error occurs.
*    6. Call the Clear Task function to clear the task.
*    7. Display an error if any.
*
* I/O Connections Overview:
*    Make sure your signal input terminal matches the Physical
*    Channel I/O control. For further connection information, refer
*    to your hardware reference manual.
*
*********************************************************************/

#include <stdlib.h>
#include <cvirte.h>
#include <userint.h>
#include <math.h>
#include <NIDAQmx.h>
#include <DAQmxIOctrl.h>
#include "ContAcq-IntClk.h"

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

static int gRunning=0;
static int panelHandle;

int main(int argc, char *argv[])
{
	if( InitCVIRTE(0,argv,0)==0 )
		return -1;	/* out of memory */
	if( (panelHandle=LoadPanel(0,"ContAcq-IntClk.uir",PANEL))<0 )
		return -1;
	SetCtrlAttribute(panelHandle,PANEL_DECORATION_BLUE,ATTR_FRAME_COLOR,VAL_BLUE);
	SetCtrlAttribute(panelHandle,PANEL_DECORATION_GREEN,ATTR_FRAME_COLOR,VAL_GREEN);
	NIDAQmx_NewPhysChanAICtrl(panelHandle,PANEL_CHANNEL,1);
	DisplayPanel(panelHandle);
	RunUserInterface();
	DiscardPanel(panelHandle);
	return 0;
}

int CVICALLBACK PanelCallback(int panel, int event, void *callbackData, int eventData1, int eventData2)
{
	if( event==EVENT_CLOSE ) {
		gRunning = 0;
		QuitUserInterface(0);
	}
	return 0;
}

int CVICALLBACK RangeCallback(int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
	if( event==EVENT_COMMIT ) {
		double  min,max;

		GetCtrlVal(panel,PANEL_MINVAL,&min);
		GetCtrlVal(panel,PANEL_MAXVAL,&max);
		if( min<max )
			SetAxisScalingMode(panel,PANEL_STRIPCHART,VAL_LEFT_YAXIS,VAL_MANUAL,min,max);
		return 1;
	}
	return 0;
}

int CVICALLBACK StartCallback(int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
	int32       error=0;
	TaskHandle  taskHandle=0;
	char        chan[256];
	uInt32      sampsPerChan,numChannels;
	float64     min,max,rate;
	int32       numRead;
	float64     *data=NULL;
	int         log;
	char        errBuff[2048]={'\0'};

	if( event==EVENT_COMMIT ) {
		GetCtrlVal(panel,PANEL_CHANNEL,chan);
		GetCtrlVal(panel,PANEL_MINVAL,&min);
		GetCtrlVal(panel,PANEL_MAXVAL,&max);
		GetCtrlVal(panel,PANEL_RATE,&rate);
		GetCtrlVal(panel,PANEL_SAMPSPERCHAN,&sampsPerChan);
		SetCtrlAttribute(panel,PANEL_STRIPCHART,ATTR_XAXIS_GAIN,1.0/rate);
		log = (int)log10(rate);
		SetCtrlAttribute(panel,PANEL_STRIPCHART,ATTR_XPRECISION,log);
		SetCtrlAttribute(panel,PANEL_STRIPCHART,ATTR_POINTS_PER_SCREEN,sampsPerChan);

		/*********************************************/
		// DAQmx Configure Code
		/*********************************************/
		DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
		DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,chan,"",DAQmx_Val_Cfg_Default,min,max,DAQmx_Val_Volts,NULL));
		DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",rate,DAQmx_Val_Rising,DAQmx_Val_ContSamps,sampsPerChan));
		DAQmxErrChk (DAQmxGetTaskAttribute(taskHandle,DAQmx_Task_NumChans,&numChannels));

		if( (data=malloc(sampsPerChan*numChannels*sizeof(float64)))==NULL ) {
			MessagePopup("Error","Not enough memory");
			goto Error;
		}

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

		SetCtrlAttribute(panel,PANEL_STRIPCHART,ATTR_NUM_TRACES,numChannels);
		SetCtrlAttribute(panel,PANEL_START,ATTR_DIMMED,1);
		ProcessDrawEvents();
		gRunning = 1;

		while( gRunning ) {
			/*********************************************/
			// DAQmx Read Code
			/*********************************************/
			DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,sampsPerChan,10.0,DAQmx_Val_GroupByScanNumber,data,sampsPerChan*numChannels,&numRead,NULL));

			if( numRead>0 )
				PlotStripChart(panel,PANEL_STRIPCHART,data,numRead*numChannels,0,0,VAL_DOUBLE);
			ProcessSystemEvents();
		}
	}

Error:
	if( DAQmxFailed(error) )
		DAQmxGetExtendedErrorInfo(errBuff,2048);
	if( taskHandle!=0 ) {
		/*********************************************/
		// DAQmx Stop Code
		/*********************************************/
		DAQmxStopTask(taskHandle);
		DAQmxClearTask(taskHandle);

		SetCtrlAttribute(panel,PANEL_START,ATTR_DIMMED,0);
	}
	if( data )
		free(data);
	if( DAQmxFailed(error) )
		MessagePopup("DAQmx Error",errBuff);
	return 0;
}

int CVICALLBACK StopCallback(int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
	if( event==EVENT_COMMIT )
		gRunning = 0;
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -