05-07-2015 12:00 PM - edited 05-07-2015 12:19 PM
Hi All
I was able to get the following code to compile, run and view the voltage data as expected. when reading the voltage (RSE) on Dev0/ai65
...National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Analog In\Measure Voltage\Cont Acq-Int Clk\ContAcq-IntClk.c
* I think that I am using RSE but I have no clue since I do not see an argument to specify
I am trying to read two different data points:
1. View the voltage (RSE) on Dev0/ai65
2. View the voltage difference (Differential) between Dev0/ai17 and Dev0/ai25
Can someone point me to an example (or web link) where two different data points are being read.
*edit - I am trying to complete this task using C++
Thanks
Chad
Solved! Go to Solution.
05-07-2015 01:01 PM
This was my last attempt that failed
#include <stdio.h>
#include <NIDAQmx.h>
#include <iostream>
using namespace std;
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle1, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle2, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
int32 CVICALLBACK DoneCallback(TaskHandle taskHandle1, int32 status, void *callbackData);
int32 CVICALLBACK DoneCallback(TaskHandle taskHandle2, int32 status, void *callbackData);
int main(void)
{
cout << "\t***************************************************\n";
cout << "\t Starting the NI DAQmx data logger\n";
cout << "\t***************************************************\n\n";
int32 error = 0;
TaskHandle taskHandle1 = 0;
TaskHandle taskHandle2 = 0;
char errBuff[2048] = { '\0' };
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk(DAQmxCreateTask("task1", &taskHandle1));
DAQmxErrChk(DAQmxCreateTask("task2", &taskHandle2));
DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle1, "Dev1/ai65", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));
DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle2, "Dev1/ai17", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));
DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle1, "", 10000.0, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 1000));
DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle2, "", 10000.0, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 1000));
DAQmxErrChk(DAQmxRegisterEveryNSamplesEvent(taskHandle1, DAQmx_Val_Acquired_Into_Buffer, 1000, 0, EveryNCallback, NULL));
DAQmxErrChk(DAQmxRegisterEveryNSamplesEvent(taskHandle2, DAQmx_Val_Acquired_Into_Buffer, 1000, 0, EveryNCallback, NULL));
DAQmxErrChk(DAQmxRegisterDoneEvent(taskHandle1, 0, DoneCallback, NULL));
DAQmxErrChk(DAQmxRegisterDoneEvent(taskHandle2, 0, DoneCallback, NULL));
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk(DAQmxStartTask(taskHandle1));
DAQmxErrChk(DAQmxStartTask(taskHandle2));
printf("Acquiring samples continuously. Press Enter to interrupt\n");
getchar();
Error:
if (DAQmxFailed(error))
DAQmxGetExtendedErrorInfo(errBuff, 2048);
if (taskHandle1 != 0) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle1);
DAQmxStopTask(taskHandle2);
DAQmxClearTask(taskHandle1);
DAQmxClearTask(taskHandle2);
}
if (taskHandle2 != 0) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle1);
DAQmxStopTask(taskHandle2);
DAQmxClearTask(taskHandle1);
DAQmxClearTask(taskHandle2);
}
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)
{
//cout << "\nStarting EveryNCallback" << "\n";
//cout << "line 59" << "\n";
TaskHandle taskHandle1 = 0;
TaskHandle taskHandle2 = 0;
int32 error = 0;
char errBuff[2048] = { '\0' };
static int totalRead1 = 0;
static int totalRead2 = 0;
int32 read1 = 0;
int32 read2 = 0;
float64 data1[1000];
float64 data2[1000];
/*********************************************/
// DAQmx Read Code
/*********************************************/
cout << "Start DAQmx Read Code Loop \n";
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle1, 1000, 10.0, DAQmx_Val_GroupByChannel, data1, 1000, &read1, NULL));
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle2, 1000, 10.0, DAQmx_Val_GroupByChannel, data2, 1000, &read2, NULL));
// for (int i = 0; i <= 10; i++)
// {
// cout << data[i] << "\n";
// }
//cout << "Here is data = " << data << "\n";
//cout << "Here is data[2] = " << data[2] << "\n"; // write the data to a file
if (read1>0) {
cout << "Acquired " << read1 << " samples \n";
totalRead1 += read1;
cout << "Acquired " << totalRead1 << " Total samples \r\n\n";
cout << data1 << "," << data2 << "\n";
fflush(stdout);
// cout << "the next line is stdout \n";
// cout << stdout << "\n";
}
Error:
if (DAQmxFailed(error)) {
DAQmxGetExtendedErrorInfo(errBuff, 2048);
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle1);
DAQmxStopTask(taskHandle2);
DAQmxClearTask(taskHandle1);
DAQmxClearTask(taskHandle2);
printf("DAQmx Error: %s\n", errBuff);
}
return 0;
}
int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData)
{
cout << "\nStarting DoneCallback" << "\n";
cout << "line 99" << "\n";
int32 error = 0;
char errBuff[2048] = { '\0' };
// Check to see if an error stopped the task.
DAQmxErrChk(status);
Error:
if (DAQmxFailed(error))
{
TaskHandle taskHandle1 = 0;
TaskHandle taskHandle2 = 0;
cout << "line 109" << "\n";
DAQmxGetExtendedErrorInfo(errBuff, 2048);
DAQmxClearTask(taskHandle1);
DAQmxClearTask(taskHandle2);
printf("DAQmx Error: %s\n", errBuff);
}
return 0;
}
05-07-2015 02:23 PM
This code is a start and it works
#include <iostream> using namespace std; #include <iostream> #include <stdio.h> #include <NIDAQmx.h> #include <time.h> using namespace std; #define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else int main(void) { cout << "\t***************************************************\n"; cout << "\t Starting the NI DAQmx data logger\n"; cout << "\t***************************************************\n\n"; int loop; for (loop = 0; loop < 3; loop++) { int32 error = 0; TaskHandle taskHandle1 = 0; TaskHandle taskHandle2 = 0; int32 read1; int32 read2; float64 data1[1000]; float64 data2[1000]; char errBuff[2048] = { '\0' }; // DAQmx analog voltage channel and timing parameters DAQmxErrChk(DAQmxCreateTask("task1", &taskHandle1)); DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle1, "Dev1/ai25", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL)); DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle1, "", 10000.0, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, 1000)); DAQmxErrChk(DAQmxCreateTask("task2", &taskHandle2)); DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle2, "Dev1/ai17", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL)); DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle2, "", 10000.0, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, 1000)); // DAQmx Start Code DAQmxErrChk(DAQmxStartTask(taskHandle1)); // DAQmx Read Code DAQmxErrChk(DAQmxReadAnalogF64(taskHandle1, 1000, 10.0, DAQmx_Val_GroupByChannel, data1, 1000, &read1, NULL)); for (int i = 0; i < read1; i++) cout << data1[i] << " "; cout << "\n\n"; DAQmxStopTask(taskHandle1); DAQmxClearTask(taskHandle1); // DAQmx Start Code DAQmxErrChk(DAQmxStartTask(taskHandle2)); // DAQmx Read Code DAQmxErrChk(DAQmxReadAnalogF64(taskHandle2, 1000, 10.0, DAQmx_Val_GroupByChannel, data2, 1000, &read2, NULL)); for (int i = 0; i < read2; i++) cout << data2[i] << " "; cout << "\n\n"; DAQmxStopTask(taskHandle2); DAQmxClearTask(taskHandle2); // Stop and clear task Error: if (DAQmxFailed(error)) DAQmxGetExtendedErrorInfo(errBuff, 2048); if (taskHandle1 != 0) { DAQmxStopTask(taskHandle1); DAQmxStopTask(taskHandle2); DAQmxClearTask(taskHandle1); DAQmxClearTask(taskHandle2); } if (DAQmxFailed(error)) printf("DAQmx Error: %s\n", errBuff); } // end of loop cout << "\t***************************************************\n"; cout << "\t Stopping the NI DAQmx data logger\n"; cout << "\t***************************************************\n\n"; return 0; }