Using Fetch and Read Methods
- Updated2023-02-17
- 3 minute(s) read
Using Fetch and Read Methods
The Fetch and Read methods are present in the ScopeChannelMeasurement class. A session must be opened, configured, and initiated before you can use Fetch and Read methods. Read methods internally invoke the Initiate method.
The Fetch and Read methods can be categorized into synchronous and asynchronous methods.
Using Async Methods
Some input/output (I/O) operations in NI-SCOPE can take a long time relative to other client program operations. In such cases, the NI-SCOPE .NET class library provides asynchronous programming support to execute the I/O operation on a different thread. This permits the client program to perform other operations without waiting for the I/O operation to complete.
The asynchronous I/O API in NI-SCOPE class library uses the Asynchronous Programming Model (APM) to implement the asynchronous I/O. In the APM, an asynchronous operation is implemented as two methods: Begin <Operation> and End <Operation> , where <Operation> is the name of an I/O method that operates synchronously.
After calling the Begin <Operation> method, a client program can continue executing instructions on the calling thread while the specific driver performs an <Operation> on a different thread. For each call to the Begin <Operation> method, the client program calls the End <Operation> method to get the results of the operation.
VB.NET
Private scopeSession As NIScope
Private Sub StartAcquisition()
Dim callBack As New AsyncCallback(AddressOf ProcessRead)
scopeSession = New NIScope("scope1", True, True)
scopeSession.Measurement.AutoSetup()
' Start the asynchronous Read operation.
Dim timeout As New PrecisionTimeSpan(10.0)
Dim asyncResult As IAsyncResult = scopeSession.Channels("0").Measurement.BeginRead(timeout, -1, Nothing, callBack, Nothing)
' …
End Sub
Private Sub ProcessRead(asyncResult As IAsyncResult)
Dim waveforms As AnalogWaveformCollection(Of Double)
Try
' Get the results.
waveforms = scopeSession.Channels("0").Measurement.EndRead(asyncResult)
Catch ex As Exception
' Handle the exception from the asynchronous Read operation.
End Try
' ...
' Process waveforms.
' ...
End SubC#
NIScope scopeSession;
void StartAcquisition()
{
AsyncCallback callBack = new AsyncCallback(ProcessRead);
scopeSession = new NIScope("scope1", true, true);
scopeSession.Measurement.AutoSetup();
// Start the asynchronous Read operation.
PrecisionTimeSpan timeout = new PrecisionTimeSpan(10.0);
IAsyncResult asyncResult = scopeSession.Channels["0"].Measurement.BeginRead(timeout, -1, null, callBack, null);
// …
}
void ProcessRead(IAsyncResult asyncResult)
{
AnalogWaveformCollection<double> waveforms;
try
{
// Get the results.
waveforms = scopeSession.Channels["0"].Measurement.EndRead(asyncResult);
}
catch (Exception ex)
{
// Handle the exception from the asynchronous Read operation.
}
// ...
// Process waveforms.
// ...
}Using Memory Optimization
VB.NET
Using scopeSession As New NIScope("scope1", True, True)
' ...
' Configure scope channel "0" properties.
' ...
' First read operation.
' Here the waveform parameter is null, so the FetchDouble method
' internally allocates memory for the return waveform object.
Dim waveform As AnalogWaveformCollection(Of Double) = scopeSession.Channels("0").Measurement.FetchDouble(New PrecisionTimeSpan(10), 1000, Nothing)
' Second read operation. Notice the waveform is passed as a parameter to the FetchDouble method.
' This time the FetchDouble method reuses memory of waveform object for the returned waveform if possible.
waveform = scopeSession.Channels("0").Measurement.FetchDouble(New PrecisionTimeSpan(10), 1000, waveform)
End UsingC#
using (NIScope scopeSession = new NIScope("scope1", true, true))
{
// ...
// Configure scope channel "0" properties.
// ...
// First read operation.
// Here the waveform parameter is null, so the FetchDouble method
// internally allocates memory for the return waveform object.
AnalogWaveformCollection<double> waveform = scopeSession.Channels["0"].Measurement.FetchDouble(new PrecisionTimeSpan(10), 1000, null);
// Second read operation. Notice the waveform is passed as a parameter to the FetchDouble method.
// This time the FetchDouble method reuses memory of waveform object for the returned waveform if possible.
waveform = scopeSession.Channels["0"].Measurement.FetchDouble(new PrecisionTimeSpan(10), 1000, waveform);
}