NI-DMM 20.0 .NET Reference

Using Memory-Optimized and Async Methods to Fetch and Read

  • Updated2023-02-21
  • 3 minute(s) read

The Fetch and Read methods are in the DmmMeasurement and DmmWaveformAcquisition classes. A session must be open and configured correctly for reading. In case of fetch, the session must be open, configured correctly, and initiated before the actual fetch. Reading is equivalent to initiating the acquisition and fetching the results. Calling Read() combines Initiate() and Fetch() into one call. Read() is suitable for simple acquisitions. Initiate() and Fetch() are better suited for complex applications.

The Read and Fetch methods can be categorized into:

  • Normal methods
  • Memory-optimized methods
  • Async methods
  • Async memory-optimized methods

Normal overloads have the option of specifying the number of points to fetch and maximum time allowed for the operation to complete. Each of the overloads has a corresponding memory-optimized overload and async overload.

Memory-Optimized Methods

Memory-optimized methods perform the fetch operation on pre-allocated memory.

If you fetch or read large amounts of data, and your application processes data between each call, and the acquired data is not necessary after processing, you can use the memory-optimized method overloads of the reader classes to improve the performance of your application.

Memory-optimized methods take an object that stores the acquired data. These overloads do not allocate memory if the buffer object passed is large enough to hold the amount of data being fetched or read, which prevents the overhead of .NET memory allocation and garbage collection. As a result, performance will improve for applications that fetch or read large amount of data.

VB.NET

Dim reading As Double() = New Double(999) {}
Dim dmmSession As New NIDmm("DMM-4072", False, False)
dmmSession.Configure(DmmMeasurementFunction.DCVolts, 10, 0.0001)
dmmSession.Trigger.MultiPoint.SampleCount = 2000
dmmSession.Measurement.Initiate()

Dim actualNumberOfPoints As Integer 

'Passing the reading for the first time.
dmmSession.Measurement.MemoryOptimizedFetchMultiPoint(1000, actualNumberOfPoints, reading)

'Process the reading using the 'reading' variable, then fetch next few samples into the 
'same variable, which reuses rather than reallocates memory. 
'Ensure that the data in the 'reading' variable is no longer needed.
dmmSession.Measurement.MemoryOptimizedFetchMultiPoint(1000, actualNumberOfPoints, reading)
C#

double[] reading = new double[1000];
NIDmm dmmSession = new NIDmm("DMM-4072", false, false);
dmmSession.Configure(DmmMeasurementFunction.DCVolts, 10, .0001);
dmmSession.Trigger.MultiPoint.SampleCount = 2000;
dmmSession.Measurement.Initiate();

int actualNumberOfPoints;
//Passing the reading for the first time.
dmmSession.Measurement.MemoryOptimizedFetchMultiPoint(1000, out actualNumberOfPoints, ref reading);

//Process the reading using the 'reading' variable, then fetch next few samples into the 
//same variable, which reuses rather than reallocates memory. 
//Ensure that the data in the 'reading' variable is no longer needed.
dmmSession.Measurement.MemoryOptimizedFetchMultiPoint(1000, out actualNumberOfPoints, ref reading);

Async Methods

For every non-asynchronous fetch and read method, there is a method which does the same operation asynchronously. We follow the event-based asynchronous pattern. These asynchronous methods perform the fetch or read operation in a separate worker thread and return the data through OperationCompleted events. Each of these asynchronous methods has a corresponding OperationCompleted event. After an async method completes execution, its corresponding event occurs. You must handle this event to get the result of the asynchronous operation. The event handler to this event should take two parameters, one of type Object and another of type DmmMeasurementEventArgs<T>.

The following code shows how to read a sample number of points asynchronously.

VB.NET

Dim dmmSession As NIDmm
Dim result As Double()
Public Sub FetchAsync()
    dmmSession = New NIDmm("DMM-4072", False, False)
    AddHandler dmmSession.Measurement.ReadMultiPointCompleted, AddressOf Measurement_ReadMultipointCompleted
    dmmSession.Configure(DmmMeasurementFunction.DCVolts, 10, 0.0001)
    dmmSession.Trigger.MultiPoint.SampleCount = 10
    dmmSession.Measurement.ReadMultiPointAsync(10, Nothing)
End Sub 

Sub Measurement_ReadMultipointCompleted(ByVal sender As Object, ByVal e As DmmMeasurementEventArgs(Of Double()))
    If e.[Error] IsNot Nothing Then
        result = e.Reading
    End If
    dmmSession.Close()
End Sub
C#

NIDmm dmmSession;
double[] result;
public void Fetch()
{
    dmmSession = new NIDmm("DMM-4072", false, false);
    dmmSession.Measurement.ReadMultiPointCompleted += new EventHandler<DmmMeasurementEventArgs<double[]>>(Measurement_ReadMultiPointCompleted);
    dmmSession.Configure(DmmMeasurementFunction.DCVolts, 10, .0001);
    dmmSession.Trigger.MultiPoint.SampleCount = 10;
    dmmSession.Measurement.ReadMultiPointAsync(10, null);
}
void Measurement_ReadMultiPointCompleted(object sender, DmmMeasurementEventArgs<double[]> e)
{
    if (e.Error == null)
    {
        result = e.Reading;
    }
    dmmSession.Close();
}

Async Memory-Optimized Methods

There are asynchronous versions of memory-optimized methods. You use these asynchronous methods similarly to the way you use non-memory-optimized asynchronous methods. The only difference between memory-optimized asynchronous methods and non-memory-optimized asynchronous methods is that the asynchronous memory-optimized read or fetch methods take an additional buffer object that stores the data.

Log in to get a better experience