This section explains how you can use the Traditional NI-DAQ (Legacy) and the NI-DAQmx .NET API to perform asynchronous I/O.
Traditional NI-DAQ (Legacy)
You use the CWDAQ controls to set up events such as the AcquireData event for performing asynchronous I/O. The data acquired is returned as one of the parameters of the AcquireData event.
Private Sub CWAI1_AcquiredData(ScaledData As Variant, BinaryCodes As Variant)
'process acquired data here
Private Sub Start_Click()
'set configuration parameters
'configure the settings and start the operation
The NI-DAQmx .NET API provides support for synchronous and asynchronous I/O.
In a synchronous operation, the reader does not return until the required numbers of data points are returned.
The previous section explained how to read 1000 data points using the finite synchronous Read method. The following example demonstrates how to set up the same operation asynchronously.
Public Sub StartAI()
' Create a new task
myTask = New Task("aiTask")
' Create a channel myTask.AIChannels.CreateVoltageChannel(physicalChannelComboBox.Text, "", CType(-1, AITerminalConfiguration), -10.0, 10.0, AIVoltageUnits.Volts)
' Configure timing specs for a finite acquisition
myTask.Timing.ConfigureSampleClock("", 1000, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, 1000)
reader = New AnalogMultiChannelReader(myTask.Stream)
' begin an asynchronous analog input operation
reader.BeginReadMultiSample(1000, New AsyncCallback(AddressOf ReadCallback), Nothing)
Catch ex As DaqException
Private Sub ReadCallback(ByVal ar As IAsyncResult)
' get the data acquired from the asynchronous operation
Dim data As Double() = reader.EndReadMultiSample(ar)
' Use data here…
Catch ex As DaqException
In the previous code snippet, the task and channel are configured in the same way as the synchronous read operation is. The only difference is the Read method that is used. BeginReadMultiSample schedules the read operation to occur in a background worker thread. You specify a function, which is ReadCallback in this example, to be called when that background worker thread completes the read operation. Once the 1000 data points are acquired, the ReadCallback method is called. This is where you can access the acquired data.
Setting up the analog input operation in this way ensures that a read operation that does not return immediately--perhaps the read operation is waiting for a trigger signal. Setting up the analog input operation in this way does not block the main UI thread and does not make the UI unresponsive.
Asynchronous I/O is the most efficient way to set up continuous AI operations. By setting up the AI operation in its own dedicated thread, you make more efficient use of system resources. Any delays in the read operation do not make the user interface unresponsive. However, realize that asynchronous I/O does not make the application faster. By making efficient use of resources, the user interface and the application do not become unresponsive.
Asynchronous I/O is similar to events, but asynchronous I/O serves a different purpose than events. The next section describes support for events in NI-DAQmx.