Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

How to log corresponding time of readouts using the C code

So I'm using the example C-code (contacquireNchan.c) that can be downloaded with daqmxBase driver.

 

I modified the code to get four output values since I'm measuring four pressure transducers.

 

How can I make 'time' (maybe in sec) to be the fifth array output.

 

What I want is like this

 

Time (sec)                    Transducer 1 (voltage)                     Transducer 2 (voltage)                     Transducer 3 (voltage)                     Tranducer 4 (voltage)

 

Right now what I have is the four tranducer output values..... of course it is an array of no.

 

I tried to play with the code but since it's not the source code, I wasn't able to log 'time' data.

 

Your help is appreciated.

 

Thanks

0 Kudos
Message 1 of 14
(6,924 Views)

Hey kichew,

 

What do you want to do with the array when you are done collecting it? Do you want to graph it?

 

I do not think there is an easy way to get the timestamp for the specific data point at the time the data is sampled without some sort of timing card. However, there are a few workarounds that you can look into. First, you could get the time before and after the DAQmxBaseReadAnalogF64 function. You could increment from the start time a set amount based on your sampling rate you have set. Another possible option if you are wanting to plot the data is to use the PlotWaveform function and set an initial X value to the start time and increment it a set amount for each time. Another simple way would be to just take the total time for the read divided by the number of samples and increment each iteration by that much.  You would want to append this data to the array of all the data points you have collected.

 

Hope this helps!

 

Joe S

Applications Engineer

National Instruments

 

0 Kudos
Message 2 of 14
(6,916 Views)

Hey Joe

 

I'd like to try any one of the three methods you proposed.

 

As for your question, yes I'm trying to get a plot of the tranducers performance versus time.

 

Say if I want to go with the simplest method you suggested; the last one.

How can I get the total time for the read. I know the number of samples and I can divide the

total time by the number of samples and like you said increase each readout by that much.

But how can i get the beginning and the end of (essentiall the total time) of the iteration?

 

Your earliest response is greatly appreciated.

 

Kichew

0 Kudos
Message 3 of 14
(6,909 Views)

Hey Kichew,

 

I think the most simple and accurate way to do this would be to create two variables (StartTime and EndTime) and then get the time in seconds right before and right after you call DAQmxBaseReadAnalogF64. Then you can subtract EndTime from StartTime to get the total time of execution and then divide by the number of samples. 

 

The exact timing increment won't be exact, but it should be fairly close and sounds like it will work for what you are doing.

 

Joe S

Applications Engineer

National Instruments

 

0 Kudos
Message 4 of 14
(6,906 Views)

Hi Joe,

 

So those two variables can be obtained from the where? Like you said it might be fair enough to assume the increment between readouts are equal. I may also have a lag and lead at the beginning and end of the readouts. But where can the code get those variables from? From the computer timer?

 

Thanks again.

Kichew

0 Kudos
Message 5 of 14
(6,902 Views)

Hey kichew,

 

You would want to use the gettimeofdaym function. I think  something like this should do the trick for you:

 

timeval sampleTime;
gettimeofday(&sampleTime, NULL);
double startTime=sampleTime.tv_sec+(sampleTime.tv_usec/1000000.0);
//DAQmxBaseReadAnalogF64
gettimeofday(&sampleTime, NULL);
double endTime=sampleTime.tv_sec+(sampleTime.tv_usec/1000000.0);
double elapsedTime = endTime - startTime;

 

You will also need to include the time.h library at the start of your file (#include <time.h> ).

 

Hope this works for you!

 

Joe Salisbury

Applications Engineer

National Instruments

 

 

0 Kudos
Message 6 of 14
(6,899 Views)

Hi Joe,

 

I've tried your suggestion but I'm not able to compile it.... lots of errors. Essentially with declaration of sampleTime, timeval etc...

 

Here is my code. What do you suggest?

 

#include "NIDAQmxBase.h"
#include <stdio.h>
#include <time.h>

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


int main(int argc, char *argv[])
{
    // 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/ai1, Dev1/ai2, Dev1/ai3, Dev1/ai4";
    float64     min = -10.0;
    float64     max = 10.0;

    // Timing parameters
    char        clockSource[] = "OnboardClock";
    uInt64      samplesPerChan = 10;
    float64     sampleRate = 1000.0;
 
    // Data read parameters
    #define     bufferSize (uInt32)1000
    float64     data[bufferSize*4];
    int32       pointsToRead = bufferSize;
    int32       pointsRead;
    float64     timeout = 10.0;
    int32       totalRead = 0;


    printf("Press Ctrl-C to exit\n");
    DAQmxErrChk (DAQmxBaseCreateTask("",&taskHandle));
    DAQmxErrChk (DAQmxBaseCreateAIVoltageChan(taskHandle,chan,"",DAQmx_Val_Diff,min,max,DAQmx_Val_Volts,NULL));
    DAQmxErrChk (DAQmxBaseCfgSampClkTiming(taskHandle,clockSource,sampleRate,DAQmx_Val_Rising,DAQmx_Val_ContSamps,samplesPerChan));
    DAQmxErrChk (DAQmxBaseCfgInputBuffer(taskHandle,200000)); //use a 100,000 sample DMA buffer
    DAQmxErrChk (DAQmxBaseStartTask(taskHandle));

    
    FILE * pFile;
    pFile = fopen ("Output.txt","a\n");

    // The loop will quit after 1o seconds

    startTime = time(NULL);
    while( time(NULL)<startTime+10) {
        DAQmxErrChk (DAQmxBaseReadAnalogF64(taskHandle,pointsToRead,timeout,DAQmx_Val_GroupByScanNumber,data,bufferSize*4,&pointsRead,NULL));
        totalRead += pointsRead;
 // DAQmxErrChk (DAQmxBaseStopTask(taskHandle));
        printf("Acquired %d samples. Total %d\n",pointsRead,totalRead);
        // Just print out the first 10 points of the last data read
        for (i = 0; i < 10; ++i)
           {fprintf (pFile,"%f\t%f\t%f\t%f\n",data[4*i],data[4*i+1],data[4*i+2],data[4*i+3]);
            printf ("data1[%d] = %f\tdata2[%d] = %f\tdata3[%d]= %f\tdata4[%d] = %f\n",i,data[4*i],i,data[4*i+1],i,data[4*i+2],i,data[4*i+3]);
           }
    }
    printf("\nAcquired %d total samples.\n",totalRead);

    fclose (pFile);
 // DAQmxErrchk (DAQmxBaseStopTask(taskHandle));

Error:
    if( DAQmxFailed(error) )
        DAQmxBaseGetExtendedErrorInfo(errBuff,2048);
    if(taskHandle != 0) {
        DAQmxBaseStopTask (taskHandle);
        DAQmxBaseClearTask (taskHandle);
    }
    if( DAQmxFailed(error) )
        printf ("DAQmxBase Error %d: %s\n", error, errBuff);
    return 0;
}

0 Kudos
Message 7 of 14
(6,885 Views)

Hey Kichew,

 

Where are you getting errors at? What are those errors?

 

You might want to try something like this. It might be a bit easier to implement:

 

time_t start,end;
double dif; // The time in seconds that it took for your read to happen

time (&start);
// Do DAQmx Read
time (&end);
dif = difftime (end,start);

 

Let me know if this helps!

 

Joe S

Applications Engineer

National Instruments

 

0 Kudos
Message 8 of 14
(6,879 Views)

Dear Joe,

 

Well it's actually giving me '0' because both the start and end are the same numbers.

 

Aslo I tried with the DAQmx's 'startTime' varialbe and it's the same number as the start and end in your help codes.

 

Is there any way out?

 

Kichew

0 Kudos
Message 9 of 14
(6,874 Views)

Hey Kichew,

 

It's possible that the DAQmxRead is happening so fast, that there is no time difference between the start and end time. If you are still using the same code from earlier, it looks like you are taking 10 samples at a rate of 1000Hz which should happen pretty quickly. Is there anyway that you could acquire more samples or do it at a slower rate? Have you tried running it multiple times to see if any of the times if has a difference between the start and end time? If it is acquiring quicker than the smallest time frame that we can detect, the best thing we might be able to do is just divide that smallest detectable time frame up by the number of samples you take.

 

Joe S

Applications Engineer

National Instruments

 

 

0 Kudos
Message 10 of 14
(6,865 Views)