Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

When I just compile example code, but It doesn't work

Hello,

 

I'm using the NI USB-6289

 

I want to collect the analog voltage input, so I compiled the example code which provided with DAQmx likes below : 

 

 

==========================================================================

#include <stdio.h>

#include <NIDAQmx.h>

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

int main(void)
{
int32 error = 0;
TaskHandle taskHandle = 0;
int32 read;
float64 data[1000];
char errBuff[2048] = { '\0' };

/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk(DAQmxCreateTask("", &taskHandle));
DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle, "Dev1/ai8", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));
DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle, "", 10000.0, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, 1000));

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

/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle, 1000, 10.0, DAQmx_Val_GroupByChannel, data, 1000, &read, NULL));

printf("Acquired %d points\n", read);

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);
printf("End of program, press Enter key to quit\n");
getchar();
return 0;
}

 

===================================================

 

But, when I execute above code right after the device is turned on, then, the program works well first time, 

 

But, when I execute again, then error message is generated likes below : 

====================================================

DAQmx Error: Some or all of the samples requested have not yet been acquired.

To wait for the samples to become available use a longer read timeout or read la
ter in your program. To make the samples available sooner, increase the sample r
ate. If your task uses a start trigger, make sure that your start trigger is co
nfigured correctly. It is also possible that you configured the task for externa
l timing, and no clock was supplied. If this is the case, supply an external clo
ck.
Property: DAQmx_Read_RelativeTo
Corresponding Value: DAQmx_Val_CurrReadPos
Property: DAQmx_Read_Offset
Corresponding Value: 0

Task Name: _unnamedTask<0>

Status Code: -200284
End of program, press Enter key to quit

 

============================

 

And, when I compile the below code (Continuous voltage collection), it also works well first time when the device is turned on, but when I execute again, it doesn't work!

 

(The error message is not same with the above case)

 

===========================

#include <stdio.h>

#include <NIDAQmx.h>

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

int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);

int main(void)
{
int32 error = 0;
TaskHandle taskHandle = 0;
char errBuff[2048] = { '\0' };

/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk(DAQmxCreateTask("", &taskHandle));
DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle, "Dev1/ai0", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));
DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle, "", 10000.0, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 1000));

DAQmxErrChk(DAQmxRegisterEveryNSamplesEvent(taskHandle, DAQmx_Val_Acquired_Into_Buffer, 1000, 0, EveryNCallback, NULL));
DAQmxErrChk(DAQmxRegisterDoneEvent(taskHandle, 0, DoneCallback, NULL));

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

printf("Acquiring samples continuously. Press Enter to interrupt\n");
getchar();

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);
printf("End of program, press Enter key to quit\n");
getchar();
return 0;
}

int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)
{
int32 error = 0;
char errBuff[2048] = { '\0' };
static int totalRead = 0;
int32 read = 0;
float64 data[1000];

/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle, 1000, 10.0, DAQmx_Val_GroupByScanNumber, data, 1000, &read, NULL));
if (read>0) {
printf("Acquired %d samples. Total %d\r", read, totalRead += read);
fflush(stdout);
}

Error:
if (DAQmxFailed(error)) {
DAQmxGetExtendedErrorInfo(errBuff, 2048);
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
printf("DAQmx Error: %s\n", errBuff);
}
return 0;
}

int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData)
{
int32 error = 0;
char errBuff[2048] = { '\0' };

// Check to see if an error stopped the task.
DAQmxErrChk(status);

Error:
if (DAQmxFailed(error)) {
DAQmxGetExtendedErrorInfo(errBuff, 2048);
DAQmxClearTask(taskHandle);
printf("DAQmx Error: %s\n", errBuff);
}
return 0;
}

 

============================

Acquiring samples continuously. Press Enter to interrupt
DAQmx Error: Attempted to read samples that are no longer available. The request
ed sample was previously available, but has since been overwritten.

Increasing the buffer size, reading the data more frequently, or specifying a fi
xed number of samples to read instead of reading all available samples might cor
rect the problem.
Property: DAQmx_Read_RelativeTo
Corresponding Value: DAQmx_Val_CurrReadPos
Property: DAQmx_Read_Offset
Corresponding Value: 0

Task Name: _unnamedTask<0>

Status Code: -200279

 

============================

 

I think the C code can't initailize perfectly, so I can't collect the voltage! (I'm already try adjusting the sampling rate, and the number of point and so on...)

 

What Can I do???? T.T

 

 

 

0 Kudos
Message 1 of 1
(5,034 Views)