[getdata-devel] Example code showing how to write to dirfile file format
Scientific Database Format
Brought to you by:
ketiltrout
|
From: Ben L. <egr...@gm...> - 2012-04-21 12:35:39
|
#include <stdio.h>
#include <NIDAQmx.h>
#include <time.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);
FILE *datafile;
int main(void)
{
int32 error=0;
TaskHandle taskHandle=0;
char errBuff[2048]={'\0'};
//Map names to physical channels
const char physicalChannel1[]="cDAQ1Mod1/ai0";
const char nameToAssignToChannel1[]="temp1";
const char physicalChannel2[]="cDAQ1Mod2/ai0";
const char nameToAssignToChannel2[]="temp2";
const char physicalChannel3[]="cDAQ1Mod3/ai0";
const char nameToAssignToChannel3[]="temp3";
const char physicalChannel4[]="cDAQ1Mod4/ai0";
const char nameToAssignToChannel4[]="prox1";
const char physicalChannel5[]="cDAQ1Mod5/ai0";
const char nameToAssignToChannel5[]="distance1";
const char physicalChannel6[]="cDAQ1Mod5/ai1";
const char nameToAssignToChannel6[]="distance2";
//DAQmxCreateAIThrmcplChan
float64 minVal=0;
float64 maxVal=300;
uInt32 units=DAQmx_Val_DegC; //DAQmx_Val_DegC degrees Celsius
//DAQmx_Val_DegF degrees Fahrenheit
//DAQmx_Val_Kelvins degrees kelvins
//DAQmx_Val_DegR degrees Rankine
uInt32 thermocoupleType=DAQmx_Val_K_Type_TC; //DAQmx_Val_J_Type_TC
//DAQmx_Val_K_Type_TC
//DAQmx_Val_N_Type_TC
//DAQmx_Val_R_Type_TC
//DAQmx_Val_S_Type_TC
//DAQmx_Val_T_Type_TC
//DAQmx_Val_B_Type_TC
//DAQmx_Val_E_Type_TC
uInt32 cjcSource=DAQmx_Val_BuiltIn; //DAQmx_Val_BuiltIn Use a cold-junction compensation channel built into the terminal block.
//DAQmx_Val_ConstVal You must specify the cold-junction temperature.
//DAQmx_Val_Chan Use a channel for cold-junction compensation.
float64 cjcVal=25.0;
const char cjcChannel[]=""; //e.g. "cDAQ1Mod1/ai0"
//AutoZeroMode
int32 setAutoZeroMode=DAQmx_Val_EverySample; //DAQmx_Val_None
//DAQmx_Val_Once
//DAQmx_Val_EverySample
//DAQmxCfgSampClkTiming
float64 rate=100;
int32 activeEdge=DAQmx_Val_Rising; //DAQmx_Val_Rising Acquire or generate samples on the rising edges of the Sample Clock.
//DAQmx_Val_Falling Acquire or generate samples on the falling edges of the Sample Clock.
int32 sampleMode=DAQmx_Val_ContSamps; //DAQmx_Val_FiniteSamps Acquire or generate a finite number of samples.
//DAQmx_Val_ContSamps Acquire or generate samples until you stop the task.
//DAQmx_Val_HWTimedSinglePoint Acquire or generate samples continuously using hardware timing without a buffer. Hardware timed single point sample mode is supported only for the sample clock and change detection timing types.
uInt64 sampsPerChan=100; //The number of samples to acquire or generate for each channel in the task if sampleMode is DAQmx_Val_FiniteSamps. If sampleMode is DAQmx_Val_ContSamps, NI-DAQmx uses this value to determine the buffer size.
//DAQmxRegisterEveryNSamplesEvent
int32 everyNsamplesEventType=DAQmx_Val_Acquired_Into_Buffer; //DAQmx_Val_Acquired_Into_Buffer This event type is only supported for input tasks. Events occur when the specified number of samples are acquired into the buffer from the device.
//DAQmx_Val_Transferred_From_Buffer This event type is only supported for output tasks. Events occur when the specified number of samples are transferred from the buffer to the device.
uInt32 nSamples=50; //The number of samples after which each event should occur.
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAIThrmcplChan(taskHandle,physicalChannel1,nameToAssignToChannel1,minVal,maxVal,units,thermocoupleType,cjcSource,cjcVal,cjcChannel));
DAQmxErrChk (DAQmxCreateAIThrmcplChan(taskHandle,physicalChannel2,nameToAssignToChannel2,minVal,maxVal,units,thermocoupleType,cjcSource,cjcVal,cjcChannel));
DAQmxErrChk (DAQmxCreateAIThrmcplChan(taskHandle,physicalChannel3,nameToAssignToChannel3,minVal,maxVal,units,thermocoupleType,cjcSource,cjcVal,cjcChannel));
DAQmxErrChk (DAQmxSetAIAutoZeroMode(taskHandle,physicalChannel1, setAutoZeroMode));
DAQmxErrChk (DAQmxSetAIAutoZeroMode(taskHandle,physicalChannel2, setAutoZeroMode));
DAQmxErrChk (DAQmxSetAIAutoZeroMode(taskHandle,physicalChannel2, setAutoZeroMode));
DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,physicalChannel4,nameToAssignToChannel4,DAQmx_Val_Cfg_Default,-60.0,60.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCreateLinScale("AnalogProxScale", 2500.0, -10.0, DAQmx_Val_Amps, "mm"));
DAQmxErrChk (DAQmxCreateAICurrentChan(taskHandle,physicalChannel5,nameToAssignToChannel5,DAQmx_Val_RSE,0.0,0.02,DAQmx_Val_FromCustomScale,DAQmx_Val_Default,249.0,"AnalogProxScale"));
DAQmxErrChk (DAQmxCreateAICurrentChan(taskHandle,physicalChannel6,nameToAssignToChannel6,DAQmx_Val_RSE,0.0,0.02,DAQmx_Val_FromCustomScale,DAQmx_Val_Default,249.0,"AnalogProxScale"));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,NULL,rate,activeEdge,sampleMode,sampsPerChan));
DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,everyNsamplesEventType,nSamples,0,EveryNCallback,NULL));
DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,NULL));
/*********************************************/
// DAQmx Start Code
/*********************************************/
/* Sample Clock Rate */
float64 sampClkRate;
DAQmxErrChk (DAQmxGetSampClkRate(taskHandle, &sampClkRate));
float64 dt;
if (sampClkRate>0)
dt=1/sampClkRate;
else dt=0;
/* Start Time */
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (buffer,80,"%d/%m/%Y %X",timeinfo);
printf("channel names:\t\t\t\t\t\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\n",nameToAssignToChannel1,nameToAssignToChannel2,nameToAssignToChannel3,nameToAssignToChannel4,nameToAssignToChannel5,nameToAssignToChannel6);
printf("start times:\t\t\t\t\t\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\n",buffer,buffer,buffer,buffer,buffer,buffer);
printf("dt:\t\t\t\t\t\n");
printf("%0.6f\t\t\t\t\t\n",dt);
printf("data:\t\t\t\t\t\n");
datafile = fopen("testdata.txt","w");
fprintf(datafile,"channel names:\t\t\t\t\t\n");
fprintf(datafile,"%s\t%s\t%s\t%s\t%s\t%s\n",nameToAssignToChannel1,nameToAssignToChannel2,nameToAssignToChannel3,nameToAssignToChannel4,nameToAssignToChannel5,nameToAssignToChannel6);
fprintf(datafile,"start times:\t\t\t\t\t\n");
fprintf(datafile,"%s\t%s\t%s\t%s\t%s\t%s\n",buffer,buffer,buffer,buffer,buffer,buffer);
fprintf(datafile,"dt:\t\t\t\t\t\n");
fprintf(datafile,"%0.6f\t\t\t\t\t\n",dt);
fprintf(datafile,"data:\t\t\t\t\t\n");
fclose (datafile);
DAQmxErrChk (DAQmxStartTask(taskHandle));
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");
getchar();
return 0;
}
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;
uInt32 chansToRead;
DAQmxErrChk (DAQmxGetTaskNumChans(taskHandle, &chansToRead));
uInt32 arraySizeInSamps = chansToRead*nSamples;
float64 *data = new float64[arraySizeInSamps];
/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,-1,10.0,DAQmx_Val_GroupByScanNumber,data,arraySizeInSamps,&read,NULL));
uInt32 i;
uInt32 j;
if( read>0 ) {
datafile = fopen("testdata.txt","a");
for (i = 0 ; i < chansToRead*read ; i+=chansToRead ) {
for (j = 0 ; j < chansToRead ; j++ ) {
if (j<chansToRead-1){
printf("% 5.5E\t", data[i+j]);
fprintf(datafile, "% 5.5E\t", data[i+j]);
}
else{
printf("% 5.5E\n", data[i+j]);
fprintf(datafile,"% 5.5E\n", data[i+j]);
}
}
}
printf("Acquired %d samples. Total %d\r\n",read,totalRead+=read);
fclose (datafile);
fflush(stdout);
}
Error:
if( DAQmxFailed(error) ) {
DAQmxGetExtendedErrorInfo(errBuff,2048);
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
printf("DAQmx Error: %s\n",errBuff);
}
delete [] data;
data = NULL;
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;
}
|