Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

strange multichannel acquisition

Hello!

I wrote a C program to
continuously acquire 6 channels using a USB-6009 at a sampling frequency of 5000 Hz, saving them in a buffer and creating a single binary file, containing all of them.

The program works quite well, but there is some problem - in the buffer, I think - I can't find.
The fact is when I read the 6 channels (for example with Mathcad, recreating 6 signals from the binary file, and this thing works with other examples), I have this problems:

- in the first acquisition the first channel is very strange (it has something like a "digital distorsion"), and it contaminates the fifth channel;

- in the second acquisition the channels are all "shifted" by four positions (the first is in the fifth place and so on);

- the third acquisition is like the first, the fourth is like the second and so on.

Here's the code, are there errors in the dimension of parameters or array (I'm desperate!)?

---

    CString campioniS;

    CFile salva;

    char outputfile[255];
    sprintf(outputfile,"%s.bin",outputfileWavPrefix);
    salva.Open(outputfile ,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
   
    // Task parameters
    int32       error = 0;
    TaskHandle  taskHandle = 0;
    char        errBuff[2048]={'\0'};
    int32       i,j;
    time_t      startTime;
    bool32      done=0;

    // Channel parameters
    char        chan[] = "Dev1/ai0:5";
    const int    nchannels = 6;
    float64     min = -10.0;
    float64     max = 10.0;

    // Timing parameters
    char        clockSource[] = "OnboardClock";
    uInt64      samplesPerChan = 1000;    // 1000
    float64     sampleRate = 5000.0;

    // Data read parameters
    #define     bufferSize (uInt32)1000    // 1000
    float64     data[bufferSize*nchannels];
    int32       pointsToRead = bufferSize;
    int32       pointsRead;
    float64     timeout = 10.0;
    int32       totalRead = 0;

   
    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,chan,"",DAQmx_Val_RSE ,min,max,DAQmx_Val_Volts,NULL));
    DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,clockSource,sampleRate,DAQmx_Val_Rising,DAQmx_Val_ContSamps,samplesPerChan));
    DAQmxErrChk (DAQmxCfgInputBuffer(taskHandle,100000)); //use a 100,000 sample DMA buffer
    DAQmxErrChk (DAQmxStartTask(taskHandle));

    FILE * fp;
    fp = fopen("test.log","w");

    startTime = time(NULL);
    while( time(NULL)<startTime+5 )
    {
        DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,pointsToRead,timeout,DAQmx_Val_GroupByScanNumber,data,bufferSize*nchannels,&pointsRead,NULL));
        totalRead += pointsRead;
        campioniS.Format("%d", totalRead);
        m_label.SetWindowText(campioniS);

        salva.Write(data, pointsRead*nchannels*8);

    }

    salva.Close();

---


Thanks to all,
Alessandro
0 Kudos
Message 1 of 6
(3,714 Views)

Hello Alessandro

The program seems to work correctly. As you said you can downsize you parameter according to the main acquisition rules. To prevent buffer overflow, the number of samples per channel to read can no longer be equal to the buffer size. It's good practice to set the number of samples per channel to read to one-fourth or one-half the buffer size for a continous buffered acqusition. It's important to monitor the number of available samples per channel in the buffer to make sure the buffer is emptying fast enough. In the worst case the buffer might overflow and generate error.

Let me know if it would work correctly

regards

Matteo
0 Kudos
Message 2 of 6
(3,682 Views)

Thanks Matteo,

but I'm not sure I understood your tip:
  I changed

int32       pointsToRead = bufferSize;

with
int32       pointsToRead = bufferSize/4;
(and so on: /2, /8...)

but this doesn't solve the problem.

Was it right or not?

Thank you for more help :)!
Alessandro




0 Kudos
Message 3 of 6
(3,669 Views)

Hello Alessandro

Can you confirm that your acquired data are correct even if the graphical plot didn't work correctly?

Once we verified that parametric configuration was well done, i suggest that the next step is to investigate the data writing instruction suitability. From your code i noticed that you are performing a continuos acquisition for 5 seconds (?). We can try to make a finite data acqusition a verify if it produce any results.

regards

Matteo
0 Kudos
Message 4 of 6
(3,632 Views)
Well I think so... I acquired some sinusoidal waves and they seemed to be right...

The acquisition is now for 5 seconds, but it should be variable by the user with a Stop button...

How can I make the test you wrote about ("We can try to make a finite data acqusition a verify if it produce any results")?


Thanks,
Alessandro
0 Kudos
Message 5 of 6
(3,615 Views)
I try changing the value in the DAQmxCfgInputBuffer function, but the problem remains:

sometimes the first (of six) channel is in the right position, sometimes is in the fifth place.

Maybe I didn't choose the right values for the functions... any help?

---
    CFile salva;
    char outputfile[255];
    sprintf(outputfile,"%s.bin",outputfileWavPrefix);
    salva.Open(outputfile ,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);

    int32       error = 0;
    TaskHandle  taskHandle = 0;
    char        errBuff[2048]={'\0'};
    int32       i,j;
    time_t      startTime;
    bool32      done=0;

    char        chan[] = "Dev1/ai0:5";
    const int    nchannels = 6;
    float64     min = -10.0;
    float64     max = 10.0;

    char        clockSource[] = "OnboardClock";
    uInt64      samplesPerChan = 1000;
    float64     sampleRate = 5000.0;

    #define     bufferSize (uInt32)1000
    float64     data[bufferSize*nchannels];
    int32       pointsToRead = bufferSize;
    int32       pointsRead;
    float64     timeout = 10.0;
    int32       totalRead = 0;

    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,chan,"",DAQmx_Val_RSE ,min,max,DAQmx_Val_Volts,NULL));
    DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,clockSource,sampleRate,DAQmx_Val_Rising,DAQmx_Val_ContSamps,samplesPerChan));
    DAQmxErrChk (DAQmxCfgInputBuffer(taskHandle,5000000));
    DAQmxErrChk (DAQmxStartTask(taskHandle));

     FILE * fp;
    fp = fopen("test.log","w");

    startTime = time(NULL);
    while( time(NULL)<startTime+5 )
    {
 
        DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,pointsRead,timeout,DAQmx_Val_GroupByScanNumber,data,bufferSize*nchannels,&pointsRead,NULL));
        totalRead += pointsRead;
        campioniS.Format("%d", totalRead);
        m_label.SetWindowText(campioniS);

        salva.Write(data, pointsRead*nchannels*8);
   }
---



0 Kudos
Message 6 of 6
(3,553 Views)