Multifunction DAQ

Showing results for 
Search instead for 
Did you mean: 

On demand encoder measurement

Go to solution

I'm having a little trouble writing an on-demand position encoder measurement in C.  My undertanding from the documentaion is that the steps required are simply to create the channel, start the task and then call the read function DAQmxReadCounterF64 any number of times.  Using the AngularPosition-Buff-Cont example as a base, I am using the following code.  However, when I run this, nothing is written to the data array when DAQmxReadCounterF64 is called.  An equivalent task configured in NI MAX reads the postion fine.  I'm presuming it must be a simple mistake on my part but I don't see it.  Does anyone have any suggestions?



#include <stdio.h>
#include "NIDAQmx.h"

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

int main(void)
	int         error=0;
	TaskHandle  taskHandle=0;
	int32       read;
	float64     data[1];
	double test_data;

	char        errBuff[2048]={'\0'};

	// DAQmx Configure Code
	DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
	DAQmxErrChk (DAQmxCreateCIAngEncoderChan(taskHandle,"Dev1/ctr0","",DAQmx_Val_X4,0,0.0,DAQmx_Val_AHighBHigh,DAQmx_Val_Degrees,24,0.0,""));

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

	printf("Continuously reading. Press Ctrl+C to interrupt\n");
	while( 1 ) {
		// DAQmx Read Code
		DAQmxErrChk (DAQmxReadCounterF64(taskHandle,1,10.0,data,1,&read,0));

		printf("Acquired %d samples.  Value: %f \n",(int)read, test_data);


	if( DAQmxFailed(error) )
	if( taskHandle!=0 ) {
		// DAQmx Stop Code
	if( DAQmxFailed(error) )
		printf("DAQmx Error: %s\n",errBuff);
	printf("End of program, press Enter key to quit\n");
	return 0;




0 Kudos
Message 1 of 3
Accepted by topic author Arbitrary_name

I have no specific knowledge of the C API, but you may need to call a different Read function.  I'm assuming that from among the arguments, 1 is the # samples to read, 10 is the timeout in sec, and 'data' is a pointer to a block of preallocated memory.


If so, then I think the Read function you're calling expects to copy multiple samples from the task buffer into an array space designated by your 'data' variable.  However, because you're in on-demand mode, no task buffer was ever allocated.  Thus, *this particular* Read function finds no data to copy.


There's probably a different Read function you need when running an on-demand task.  I'd expect the function declaration to make clear that it's only capable of returning a single counter value.



-Kevin P

0 Kudos
Message 2 of 3

Hi Kevin, You are absolutely right, there is a scalar counter read function I'd missed.  Thank you so much for your help.

0 Kudos
Message 3 of 3