Transition from Traditional NI-DAQ (Legacy) to NI-DAQmx using Microsoft Visual Basic .NET: Part Four

Overview

NOTE: This document is the fourth document in the series, Transition from Traditional NI-DAQ (Legacy) to NI-DAQmx Using Microsoft Visual Basic .NET. Refer to Links to Other Topics in this Series to access other documents in this series.

Contents

Accessing Help in NI-DAQmx

This series references two main types of reference manuals that ship with NI-DAQmx.

  • NI-DAQmx Core Help--references the language agnostic manual for NI-DAQmx. The NI-DAQmx Core Help explains NI-DAQmx concepts and provides a background about the various features and capabilities of the driver. Refer to Start » Programs » National Instruments » NI-DAQ » NI-DAQmx Help to access this manual.
  • NI-DAQmx .NET Help--references the manual for the .NET DAQmx API. This reference is specific to the .NET API and provides documentation for NI-DAQmx classes, methods, and properties. There are also concept topics in this manual which are specific to using the NI-DAQmx .NET API. This help is integrated into the Visual Studio .NET documentation. In Visual Studio .NET, select Help»Contents. To view the NI-DAQmx .NET Library help, select NI Measurement Studio Help»NI Measurement Studio .NET Class Library»Reference»National Instruments.DAQmx. For tasks and concepts, select NI Measurement Studio Class .NET Library»Using the Measurement Studio .NET Class Libraries»Using the Measurement Studio NI-DAQmx .NET Library.

Resource Cleanup

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


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.

Was this information helpful?

Yes

No