This series references two main types of reference manuals that ship with NI-DAQmx.
Traditional NI-DAQ (Legacy)
You use the Reset method on the CWDAQ controls to set the controls to their default state and release any resources that were reserved during the configuration. In most cases, you do not need to call Reset explicitly. Visual Basic 6.0 automatically releases any resources when the application exits. If Traditional NI-DAQ (Legacy) resources need to be freed at run time, assign Nothing to the object. This behavior has changed in Visual Basic .NET.
NI-DAQmx
The .NET Framework uses garbage collection for managing its resources. Garbage collection is useful because it removes the burden of tracking the lifetimes of any objects allocated at run time. However, garbage collection is not deterministic. The .NET Framework does not guarantee exactly when the resources associated with an object marked for collection are released, even if the Task is set to Nothing.
You use the Dispose method to deterministically release any NI-DAQ resources that are reserved when the task is configured.
[VB.NET]
'Clear the task and release any resources used by the task
daqTask.Dispose()
Error Handling in Visual Basic .NET is different than error handling in Visual Basic 6.0.
Traditional NI-DAQ (Legacy)
The CWDAQ ActiveX control provides error notifications by raising the DAQError and DAQWarning events.
[VB 6.0]
Private Sub Start_Click()
CWAI1.Device = 1 'use device 1
'Use channel 0, with a range of +/- 10 volts in differential mode
CWAI1.Channels.Add "0", 10#, -10#, CWAIInputModes.cwaiDIFF
'Configure the sample clock
CWAI1.ScanClock.Frequency = 1000
CWAI1.NScans = 1000
CWAI1.ChannelClock.ClockSourceType = cwaiInternalCS
'Specify the task to be finite
CWAI1.StopCondition.Type = cwaiNoActiveCondition
'Other code
CWAI1.Configure
CWAI1.Start
End Sub
Private Sub CWAI1_DAQError(ByVal StatusCode As Long, ByVal ContextID As Long, ByVal ContextDescription As String)
MsgBox "Error: " & StatusCode & vbCrLf & "Context: " & ContextDescription & vbCrLf & CWDAQTools1.GetErrorText(StatusCode)
End Sub
Private Sub CWAI1_DAQWarning(ByVal StatusCode As Long, ByVal ContextID As Long, ByVal ContextDescription As String)
MsgBox "Warning: " & StatusCode & vbCrLf & "Context: " & ContextDescription & vbCrLf & CWDAQTools1.GetErrorText(StatusCode)
End Sub
NI-DAQmx
Visual Basic .NET provides an object-oriented solution to signaling and responding to unexpected problems while your program is running, through structured exception handling. The NI-DAQmx .NET API throws a DAQException whenever the driver returns an error. The object provides information about the error number, error message, the stack, etc. This is done through the Try-Catch-Finally block.
[VB.NET]
Try
' 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
myTask.Timing.ConfigureSampleClock("", 1000, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, 1000)
reader = New AnalogMultiChannelReader(myTask.Stream)
' Read the data
Dim data As Double(,) = reader.ReadMultiSample(1000)
Catch ex As DaqException
MessageBox.Show(ex.Message)
Finally
myTask.Dispose()
End Try
In the previous example, if any NI-DAQmx errors occur within the block of code after the first Try statement, the error is caught as a DAQException. All the errors in the NI-DAQmx .NET library are indicated by throwing DAQException objects. In this example, the Catch block displays a message box showing the error message. In this example, the Finally block ensures that the NI-DAQmx resources are cleaned up regardless of whether an exception occurred or not.
The NI-DAQmx .NET API provides an event for NI-DAQ warnings since warnings do not require the application to stop execution. Errors do require the application to stop execution. The follow code snippet demonstrates how to use DAQWarning event.
'define the daqsystem and initialize it
Private WithEvents daqSystem As DaqSystem = DaqSystem.Local
.
.
.
Public Sub DaqWarningEventHandler(ByVal sender As Object, ByVal e As DaqWarningEventArgs) Handles daqSystem.DaqWarning
'display or log warning message
End Sub
Refer to the NI-DAQmx .NET Help for more information about the DaqException object and DaqWarning event. For more information about structured exception handling and Try-Catch-Finally in Visual Basic .NET, refer to MSDN.
This section lists other topics in this series:
See Also:
Transition from Traditional NI-DAQ (Legacy) to NI-DAQmx using Microsoft Visual Basic .NET
Transition from Traditional NI-DAQ (Legacy) to NI-DAQmx using Microsoft Visual Basic .NET: Part Two
Transition from Traditional NI-DAQ (Legacy) to NI-DAQmx using Microsoft Visual Basic .NET: Part Three