Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Synchronize Data from analog input and digital counter input devices

Hello. I'm using the ANSI C library and have two cards: an NI 9237 and an NI 9401. The NI 9237 has a load cell, the NI 9401 has a linear encoder. I need to record the load and position channels in perfect synchrony. To that end, I adapted the following from an example. I configure the two channels, tie them both to the SampleClock of the analog card. The analog card task gets used to register the EveryNSamples event. The task, and I get the callbacks. But, while I always get the 1000 rows of data from both read functions, the data is not aligned in time. The position data is "ahead" of the load data. I feel like I must be missing a step syncing the data, but I can't find anything in any example where this shouldn't work.

 

Thanks in advance for any insight.

 

 

samplesPerSec_ = 10000; // 10khz

 


DAQmxErrChk(DAQmxCreateTask("AnalogInputTask", &taskHandle));
DAQmxErrChk(DAQmxCreateTask("EncoderTask", &encoderTask));

taskHandles_.push_back(taskHandle);
taskHandles_.push_back(encoderTask);

DAQmxErrChk(DAQmxCreateAIForceBridgeTwoPointLinChan(
  taskHandle,
  "cDAQ9185-1E26063Mod1/ai0",
  "Load",

  -2224.1108,
  2224.1108,
  DAQmx_Val_Newtons,
  DAQmx_Val_FullBridge,
  DAQmx_Val_External,
  10.0,
  350,

  0,

  2.3222,

  DAQmx_Val_mVoltsPerVolt,

  0,
  2224.1108,

  DAQmx_Val_Newtons, 

  NULL

));

 

DAQmxErrChk(DAQmxCreateCILinEncoderChan(
  encoderTask,
  "cDAQ9185-1E26063Mod3/ctr0",
  "Position",
  DAQmx_Val_X4,
  1,
  0.0,
  DAQmx_Val_AHighBHigh,
  DAQmx_Val_Meters,
  0.0000039976,
  0.0,
  NULL
));

 

DAQmxErrChk(DAQmxCfgSampClkTiming(
  taskHandle,
  "/cDAQ9185-1E26063Mod1/ai/SampleClock",

  samplesPerSec_,

  DAQmx_Val_Rising,
  DAQmx_Val_ContSamps,
  1000
));

 

DAQmxErrChk(GetTerminalNameWithDevPrefix(taskHandle, "ai/SampleClock", trigName));

 

DAQmxErrChk(DAQmxCfgSampClkTiming(
  encoderTask,
  "/cDAQ9185-1E26063Mod1/ai/SampleClock",

  samplesPerSec_,

  DAQmx_Val_Rising,
  DAQmx_Val_ContSamps,
  1000
));

 

DAQmxErrChk(DAQmxRegisterEveryNSamplesEvent(
  taskHandles_[0],
  DAQmx_Val_Acquired_Into_Buffer,
  1000,
  0,
  EveryNCallback,
  (void*)this

));

 

 

static int32 CVICALLBACK EveryNTest(
TaskHandle taskHandle,
int32 everyNsamplesEventType,
uInt32 nSamples,
void* callbackData
)
{

 

  DAQmxErrChk(DAQmxReadAnalogF64(
    taskHandles_[0],
    1000, // the number of samples to read
    10.0,
    DAQmx_Val_GroupByScanNumber,
    analogDataBuffer_.data(),
    analogDataBuffer_.size(), // the available size of the buffer
    &analogRead,

    NULL
  ));

 

  DAQmxErrChk(DAQmxReadCounterF64Ex(
    taskHandles_[1],
    1000,
    10.0,
    DAQmx_Val_GroupByScanNumber,
    counterDataBuffer_.data(),
    counterDataBuffer_.size(),
    &counterRead,
    NULL
  ));

 

  .. push data into FIFO queue ..

 

}

 

 

 

 

0 Kudos
Message 1 of 1
(2,233 Views)