Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Accurate Time Interval

Hi,

 

I faced the following problem, I am reading voltage from NiDaq using System.Timers.Timer and I am not sure about accuracy of its interval.

 

void SysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{

      double[] _data = analogInReader.ReadSingleSample();
      ......

}

 

 

Anyway I will need to get information every 2 or 4 ms. I heard that NIdaq has buffer and internal timer. So is there any opportunity to store values inside NiDaq (for example, pairs <datetime, value> ) and ask nidaq every 50 millisecond for all values during this interval. Then I will need to plot it on the chart so I need to get (x,y) point where x- date,time, y - value.

 

Could you give me an example?

 

Thank you in advance

 

Pit

 

0 Kudos
Message 1 of 5
(3,524 Views)

Hello Petrucho,

 

Yes, you can do a buffered measurement that will have an accurate time interval with DAQmx. There are a couple of .NET examples with the DAQmx driver for Analog input tasks. In order to find these examples, navigate to Start >> Programs >> National Instruments >> NI-DAQ >> Text-Based Code Support. This will open a folder containing all the text based examples. Navigate to Analog In >> Measure Voltage. In this folder, all the buffered continous measurement examples begin with Cont.

 

From here this should so you how to acquire the data an a certain sample rate. From here you can reorganize the data to create a 2d array to place the information on a graph.


Jim St
National Instruments
RF Product Support Engineer
0 Kudos
Message 2 of 5
(3,505 Views)

Hi Jim,

 

Thanx a lot for your answer. I am not very good in C# so I am still having a little missunderstanding. I initialize my task and than ask for reader.BeginReadWaveform method. In the AnalogInCallback method I do my processing, but I would like to do reading every 2 ms. Because of the accuracy of windows.timer I can not do it with the windows timer so I will have to read buffer every 20 ms. Could you help me how to do it because I couldn t find it in code.

 

Thanx a lot

 

Pit

0 Kudos
Message 3 of 5
(3,478 Views)

Hi Jim,

 

I managed to do everything but I have a problem. Please look at the code below:

 

myTask = new Task();

AIChannel myChannel = myTask.AIChannels.CreateVoltageChannel(physicalChannelComboBox.Text, "",


AITerminalConfiguration.Rse, Convert.ToDouble(minimumValueNumeric.Value),
  Convert.ToDouble(maximumValueNumeric.Value), AIVoltageUnits.Volts);


myTask.Timing.ConfigureSampleClock("", Convert.ToDouble(rateNumeric.Value),
SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, Convert.ToInt16(samplesNumeric.Value));

myTask.Control(TaskAction.Verify);

runningTask = myTask;

analogInReader = new AnalogSingleChannelReader(myTask.Stream);

analogInReader.SynchronizeCallbacks = true;
analogInReader.BeginReadWaveform(numberOfSamples, myAsyncCallback, myTask);

private void AnalogInCallback(IAsyncResult ar)
{
try
{
if (runningTask == ar.AsyncState && ar.IsCompleted)
{
int iteration;
//data.Timing = WaveformTiming.
DateTime start = DateTime.Now;
data = analogInReader.EndReadWaveform(ar);

............

Processing

............

In the AnalogInCallback function I do my processing and if I use functions which need a lot of time for accomplishment I receive an error - values which you try to read are already overwritten - please expand buffer or increase freaquency. So could you give me an advice how to pause task while I am doing such long operations - I tried to use task.stop/start methods but I did not succed.

 

Thanx a lot

 

Pit

 

0 Kudos
Message 4 of 5
(3,425 Views)

Hi Petrucho,

 

I was able to use the myTask.Stop() and myTask.Start() to pause the acquisition. I am basing my code off the ContAcqVoltageSamples_IntClk.2008 C# example. Here is my try block in the AnalogInCallback

 

 

try
            {
                if (runningTask == ar.AsyncState)
                {
                    // Read the available data from the channels
                    data = analogInReader.EndReadWaveform(ar);
                                    
                    // Plot your data here
                    dataToDataTable(data, ref dataTable);
                    myTask.Stop();
                    System.Threading.Thread.Sleep(2000);
                    myTask.Start();
                    analogInReader.BeginMemoryOptimizedReadWaveform(Convert.ToInt32(samplesPerChannelNumeric.Value),
                        analogCallback, myTask, data);                                    
                  
                }

 

In your case, System.Threading.Thread.Sleep(2000) should be replaced with your processing steps. Try this and let me know how it works. Thanks.

Steven K.
National Instruments
Software Engineer
0 Kudos
Message 5 of 5
(3,397 Views)