beautypg.com

Initialization and error handling – Measurement Computing TempBook rev.3.0 User Manual

Page 99

background image

TempBook User’s Manual

Enhanced API Programming Models (TempBook) 10-3

Initialization and Error Handling

This section demonstrates how to initialize the Daq* and use
various methods of error handling. Most of the example
programs use similar coding as detailed here. Functions
used include:

VBdaqOpen&(daqName$)

VBdaqSetErrorHandler&(errHandler&)

VBdaqClose&(handle&)

All Visual Basic programs should include the DaqX.bas file
into their project. The DaqX.bas file provides the necessary
definitions and function prototyping for the DAQX driver
DLL.

handle& = VBdaqOpen&(“TempBook0”)
ret& = VBdaqClose&(handle&)

The Daq* device is opened and initialized with the daqOpen function. daqOpen takes one parameter—
the name of the device to be opened. The device name information can be accessed or changed via the
Daq* Configuration utility located in the operating system’s Control Panel. The daqOpen call, if
successful, will return a handle to the opened device. This handle may then be used by other functions to
configure or perform other operations on the device. When operations with the device are complete, the
device may then be closed using the daqClose function. If the device could not be found or opened,
daqOpen

will return -1.

The DAQX library has a default error handler defined upon loading. However; if it is desirable to change
the error handler or to disable error handling, then the daqSetErrorHandler function may be used to
setup an error handler for the driver. In the following example the error handler is set to 0 (no handler
defined) which disables error handling.

ret& = VBdaqSetErrorHandler&(0&)

If there is a Daq* error, the program will continue. The function’s return value (an error number or 0 if no
error) can help you debug a program.

If (VBdaqOpen&(“TempBook0”) < 0) Then

“Cannot open “TempBook0”

Daq* functions return daqErrno&.

Print “daqErrno& : ”; HEX$(daqErrno&)

End If

The next statement defines an error handling routine that frees us from checking the return value of every
Daq* function call. Although not necessary, this sample program transfers program control to a user-
defined routine when an error is detected. Without a Daq* error handler, Visual Basic will receive and
handle the error, post it on the screen and terminate the program. Visual Basic provides an integer variable
(ERR) that contains the most recent error code. This variable can be used to detect the error source and
take the appropriate action. The function daqSetErrorHandler tells Visual Basic to assign ERR to a
specific value when a Daq*error is encountered. The following line tells Visual Basic to set ERR to 100
when a Daq*error is encountered. (Other languages work similarly; refer to specific language
documentation as needed.)

handle& = VBdaqOpen&(“TempBook0”)
ret& = VBdaqSetErrorHandler&(handle&, 100)

On Error GoTo ErrorHandler

The On Error GoTo command in Visual Basic allows a user-defined error handler to be provided, rather
than the standard error handler that Visual Basic uses automatically. The program uses On Error GoTo
to transfer program control to the label ErrorHandler if an error is encountered.

Daq* errors will send the program into the error handling routine. This is the error handler. Program
control is sent here on error.

ErrorHandler: