Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Writing an Every N Samples Event for AO on NI-DAQ in C

230 views
Skip to first unread message

Astro Mark

unread,
Jun 17, 2008, 4:40:07 PM6/17/08
to
Hi there,
 
I'm trying to write some code that writes out to my NI-6731 board quickly (1 kHz) but I need to be able to change the output voltage in real time.  I.E. I don't know what voltage I need to output until just before I need to send it out.  Later, I will read these values from a file, but for now, I can write it internally in the callback routine.  I wrote some code based on the example ContAcq-IntClk-EveryNSamplesEvent.c so that every N Samples it writes a different set of samples.
 
The problem is that my code does not work.  I get the error:
 
DAQmx Error: Measurements: DAQmx Every N Samples Event is not supported within non-buffered tasks.
 
To receive Every N Samples Event notifications, configure your task to use buffering.
Task Name: _unnamedTask<0>
 
Status Code: -200848
 
I am pretty sure my code is set up for buffering tasks, so I am quite confused.  The code is designed to be slow right now, but eventually I will speed it up.  For now, I just want it to work!  If you could take a look at it and help me sort out what the problem is, I would really appreciate it.
 
Cheers
 

#include <stdio.h>
#include <math.h>
#include "C:\NI-DAQ\DAQmx-ANSI-C-Dev\include\NIDAQmx.h"
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int32 CVICALLBACK EveryNSamplesCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
void Cleanup (void);
static TaskHandle taskHandle=0;
static int32 totalWrite=0;
static float64 data[4000];
int main(void)
{
int error=0;
char errBuff[2048]={'\0'};
int i=0;
for(;i<2000;i++) {
data[i] = 4;
data[i+2000] = 4;
}
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAOVoltageChan(taskHandle,"Dev1/ao0:1", "",-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",2000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,2000));
/*********************************************/
/* Tell the code to do another event once a certain
number of samples have been written*/
/*********************************************/
DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Transferred_From_Buffer,2000,0,EveryNSamplesCallback,NULL));
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));
/*********************************************/
// DAQmx Write Code
/*********************************************/
DAQmxErrChk (DAQmxWriteAnalogF64(taskHandle,2000,0,10.0,DAQmx_Val_GroupByChannel,data,NULL,NULL));
printf("Generating samples continuously. Press Enter key to interrupt\n");
getchar();
DAQmxErrChk (DAQmxStopTask(taskHandle));
printf("\nWrote %d total samples.\n",totalWrite);
/*********************************************/
// DAQmx Wait Code
/*********************************************/
DAQmxErrChk (DAQmxWaitUntilTaskDone(taskHandle,10.0));
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);
return 0;
}
int32 CVICALLBACK EveryNSamplesCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)
{
int32 error=0;
int32 write;
char errBuff[2048]={'\0'};
int j=0;
float64 data2[4000];
for(;j<2000;j++) {
data2[j] = 2;
data2[j+2000] = 2;
}
/*********************************************/
// DAQmx Write Code
/*********************************************/
DAQmx

Wallace_F

unread,
Jun 18, 2008, 1:10:16 PM6/18/08
to
Hello Astro Mark,
The NI-DAQmx C Reference Help file states that if you configure timing for your task, that the task is a buffered task.  Furthermore, if you don't manually configure the buffer size using DAQmxCfgOutputBuffer, DAQmx automatically configures the buffer when you configure sample timing.  I recommend explicitly configuring the output buffer using the DAQmxCfgOutputBuffer function, and give it the appropriate parameters to see if you can eliminate the error.
Best wishes,
Wallace F.

Zach Hindes

unread,
Jun 19, 2008, 10:10:13 AM6/19/08
to
I tested out my theory of configuring an output buffer size but not
writing any data, and it turns out you get an error: "-200462:
Generation cannot be started, because the output buffer is empty. 
Before starting a buffered generation, write data. ..."  There you have
it!
0 new messages