Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Continuous Sampling with OverWrite Enabled

I have created an application that queries an analog input channel for the most recent sample on a non-realtime basis.
I am running into the error "Attempted to read samples that are no longer available".  However, I have set the
system to ignore buffer overwrite.  The following snippet of code describes the setup of the channel and task:

    //Open the channel and configure the timing
    if(!iRetVal) iRetVal = DAQmxCreateAIVoltageChan(m_thDaqTask, strChannelName, "", DAQmx_Val_Diff, iMinVoltage,
                                                        iMaxVoltage, DAQmx_Val_Volts, 0);
    if(!iRetVal) iRetVal = DAQmxCfgSampClkTiming(m_thDaqTask, 0, m_dSampleRate, DAQmx_Val_Rising, DAQmx_Val_ContSamps,
                                                        uiSampsPerChan);
   

    //Setup the read position and start the task - also, set the task to overwrite old data even if
    //we haven't viewed it - we only care about the most recent sample
    if(!iRetVal) iRetVal = DAQmxSetReadOverWrite(m_thDaqTask, DAQmx_Val_OverwriteUnreadSamps);
    if(!iRetVal) iRetVal = DAQmxSetReadRelativeTo(m_thDaqTask, DAQmx_Val_MostRecentSamp);
    if(!iRetVal) iRetVal = DAQmxSetReadOffset(m_thDaqTask, 0);
    if(!iRetVal) iRetVal = DAQmxStartTask(m_thDaqTask);

I then grab samples at random intervals using the following function:

    //Get the reading from the hardware
    if(!iRetVal) iRetVal = DAQmxReadAnalogF64(m_thDaqTask, m_uiAveragingLength, /*dTimeOut*/-1, DAQmx_Val_GroupByScanNumber,
                                                pdRawBuffer, uiNumRawSamples, (int32 *)&iSamplesRead, 0);

I particuarly notice that when I pause the system in the debugger for long enough to allow the buffer to overfill, I am guaranteed to get the error.

Thanks for any help that can be offered.

-Kyle R. Breton
0 Kudos
Message 1 of 2
(3,686 Views)
Kyle,

I have not been able to reproduce this behavior. I used the following code (very similar to yours). When setting the read relative property and letting the buffer overflow, I do not get an error, nor do I get a nonzero return value from the analog read. When I eliminate the three lines following the comment, I do get error -200279 (Attempted to read samples that are no longer available).

How are you getting this error? Do you have error handling implemented elsewhere in the code? Please let us know!

#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;
    char        errBuff[2048]={'\0'};
    int32       read=0;
    float64     data[1000];

    /*********************************************/
    // DAQmx Configure Code
    /*********************************************/
    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev2/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));


    //Comment out these lines to get an error    
    DAQmxErrChk (DAQmxSetReadOverWrite(taskHandle, DAQmx_Val_OverwriteUnreadSamps));
    DAQmxErrChk (DAQmxSetReadRelativeTo(taskHandle, DAQmx_Val_MostRecentSamp));
    DAQmxErrChk (DAQmxSetReadOffset(taskHandle, 0));
   
    /*********************************************/
    // DAQmx Start Code
    /*********************************************/
    DAQmxErrChk (DAQmxStartTask(taskHandle));

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

    DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000, -1, DAQmx_Val_GroupByScanNumber, data, 1000, &read, 0));

   
Error:
    printf("Error Number: %d\n\n",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;
}


Regards,
Ryan Verret
Product Marketing Engineer
Signal Generators
National Instruments
0 Kudos
Message 2 of 2
(3,665 Views)