I am using a USB-6009 device with NI-DAQmx Base 2.1.0
I have a control loop where I read 4 analog inputs once, and then do some processing on them, change the analog outputs, and then repeat. I have simplified my code and listed it below. I have encountered a problem where I will occasionally read the wrong voltages. In a loop of 200 times, I will get wrong voltage readings 0-6 times out of 200. I have the following voltages connected to AI0:3 : 0.5, 1.8, 6.9, 2.75. I read those voltages correctly most of the time, but sometimes I will get a reading like: 1.80, 6.90, 2.75, 2.75.
I tried changing my code so that I only read AI0, and changed arraySizeInSamps to 1, and when I do that I *always* read the correct voltage of 0.5 for AI0. It's only when I read multiple channels that I occasionally get wrong readings.
{
int i;
// Task parameters
int32 error = 0;
TaskHandle inTaskHandle = 0;
char errBuff[2048]={'\0'};
// Channel parameters
char chan[128]={'\0'};
float64 min = 0.0;
float64 max = 5.0;
// Data write parameters
float64 timeout = 10.0;
// Data read parameters
float64 data[4];
int32 numSampsPerChan = 1;
uInt32 arraySizeInSamps = 4; int32 pointsRead;
sprintf(chan,"Dev1/ai0:3"); DAQmxErrChk (DAQmxBaseCreateTask("",&inTaskHandle));
min = -10.0;
max = 10.0;
DAQmxErrChk (DAQmxBaseCreateAIVoltageChan (inTaskHandle, chan, "", DAQmx_Val_Diff, min, max, DAQmx_Val_Volts, NULL));
DAQmxErrChk (DAQmxBaseStartTask(inTaskHandle));
for (i=0; i < 200; i++)
{
DAQmxErrChk (DAQmxBaseReadAnalogF64(inTaskHandle,numSampsPerChan,timeout,DAQmx_Val_GroupByScanNumber,data,arraySizeInSamps,&pointsRead,NULL));
NSLog(@"---> %.2f, %.2f, %.2f, %.2f", data[0], data[1], data[2], data[3]);
/*** Do some processing here based on data[] ***/
}
NSLog(@"** DONE read %d samples **", i);
Error:
if( DAQmxFailed(error) )
DAQmxBaseGetExtendedErrorInfo(errBuff,2048);
if( inTaskHandle!=0 ) {
DAQmxBaseStopTask(inTaskHandle);
DAQmxBaseClearTask(inTaskHandle);
}
if( DAQmxFailed(error) )
printf("DAQmxBase Error: %s\n",errBuff);
}