Вы находитесь на странице: 1из 11

NI-DAQmx C API Visual Basic 6.

0 Support Beta Documentation


This document contains the following sections: Overview System Requirements Whats Installed Getting Started NI-DAQmx Concepts nidaqmx.tlb in Visual Basic 6.0 Adding a Reference to nidaqmx.tlb Creating Tasks Using and Clearing Tasks Getting, Setting, and Resetting Properties Handling Errors Using Enumerations Using Events NI-DAQmx C Reference Help with Visual Basic 6.0 Differences Between the NI-DAQmx C API and the NI-DAQmx Visual Basic 6.0 API Examples Using Visual Basic 6.0 Using C to Create Visual Basic 6.0 Applications Deployment Frequently Asked Questions Appendix A Functions with Array Parameters

Overview
NI-DAQmx 7.4 includes a beta type library that you can use to create applications in Visual Basic 6.0. This type library is named nidaqmx.tlb and is installed and registered when the NI-DAQmx driver is installed with Microsoft Visual C Support selected. This type library provides declarations for NI-DAQmx C API functions and enumerations. Visual Basic 6.0 uses these declarations to determine how to call into the C API DLL.

System Requirements
To install NI-DAQmx C API Visual Basic 6.0 Support Beta, your computer must have the following: NI-DAQmx 7.4 or later Visual Basic 6.0, with Service Pack 6 or later

What Is Installed
When you install NI-DAQmx (7.4 or greater) with Microsoft Visual C Support selected, the following items are installed on your computer: The NI-DAQmx driver (7.4 or greater)includes nicaiu.dll, the DLL that implements the NI-DAQmx C API support. The nidaqmx.tlb type library The NI-DAQmx C Reference Helpavailable from StartProgramsNational InstrumentsNI-DAQNI-DAQmx C Reference Help. Examples o Examples written in C that can be converted to Visual Basic 6.0 are installed in the NI-DAQ\Examples\DAQmx ANSI C Dev directory. o Examples written in Visual Basic 6.0 are available from Knowledge Base 39BF3M1E: Can I Program A Board with NI-DAQmx in Visual Basic 6.0? This help document is available in Knowledge Base 39BF3M1E: Can I Program A Board with NI-DAQmx in Visual Basic 6.0?

Getting Started
Read through this document for detailed information about using the NI-DAQmx C API from within Visual Basic 6.0. Complete the following steps to create an NI-DAQmx application in Visual Basic 6.0: 1. Create a new Standard EXE project. 2. From the Visual Basic 6.0 menu, select ProjectReferences. 3. To add the NI-DAQmx type library, nidaqmx.tlb, to the project, select NI DAQmx C API from the list. If NI DAQmx C API is not available in the list, browse to and select nidaqmx.tlb in the Windows\System32 directory. 4. Write code to call into the C API functions. 5. Build your application. For information about specific NI-DAQmx C API functions, refer to the NI-DAQmx C Reference Help, which you can launch from the Windows Start menu by selecting StartProgramsNational InstrumentsNI-DAQNI-DAQmx C Reference Help. .

NI-DAQmx Concepts
For conceptual information about data acquisition and the NI-DAQmx driver, refer to the following sections in the NI-DAQmx C Reference Help. To locate these sections in the help, click the Contents tab and expand the NI-DAQmx Concepts book. Key NI-DAQmx Concepts Traditional NI-DAQ (Legacy) Versus NI-DAQmx Measurement Fundamentals Common Applications

nidaqmx.tlb in Visual Basic 6.0


Adding a Reference to nidaqmx.tlb
Within each Visual Basic 6.0 project, you must add a reference to nidaqmx.tlb that accesses the NI-DAQmx driver. From within Visual Basic 6.0, click ProjectReferences to open the References dialog box. From the Available References, check NI-DAQmx C API and click OK. Once you reference the type library from the project, you can use NIDAQmx functions in Visual Basic 6.0. For descriptions of each NI-DAQmx function, refer to the following sections in the NIDAQmx C Reference Help: NI-DAQmx C Functions NI-DAQmx C Properties

Once you have added the type library to the project, the DAQmx functions display in the Object Browser. From within Visual Basic 6.0, click ViewObject Browser to launch the Object Browser. When you view the library in the Object Browser, you see that it consists of a single DAQmx module and many global enumerations. The DAQmx module is a flat list that contains all of the DAQmx functions.

Creating Tasks
Most of the operations that you can perform with the NI-DAQmx library, such as acquiring data or generating a signal, require that you first create an NI-DAQmx task. A task is a collection of one or more virtual channels with timing, triggering, and other properties. Conceptually, a task represents a measurement or generation you want to perform. Refer to the NI-DAQmx C Reference Help for more information on NI-DAQmx tasks. There are two ways that you can create a task to use within your Visual Basic 6.0 program. You can programmatically create a new NI-DAQmx task and call various NIDAQmx functions to configure the task; or you can use National Instruments Measurement & Automation Explorer (MAX) to create and configure your task and then

load it within your Visual Basic 6.0 program. After you load a task that was created in MAX, you can call NI-DAQmx functions to further configure the task.
Programmatically Creating and Configuring a Task Complete the following steps to programmatically create and configure a task:

1. Call DAQmxCreateTask to create a new task. 2. Call an appropriate channel creation function, such as DAQmxCreateAIVoltageChan or DAQmxCreateCIPeriodChan to add one or more channels to the task. 3. Call an appropriate timing configuration function, such as DAQmxCfgSampClkTiming, to configure the task timing characteristics.
Using MAX to Create and Configure a Task You can use the NI-DAQmx Visual Basic 6.0 API to load a task that you have already configured and saved in Measurement & Automation Explorer (MAX). You create and configure NI-DAQmx tasks in MAX through an interactive, graphical interface. This approach reduces the amount of code you have to write. You can load tasks saved in MAX and run them in any programming language that has access to an NI-DAQmx library, including Visual Basic 6.0, Visual C, Visual C++, Visual C#, Visual Basic .NET, and LabVIEW.

Complete the following steps to create a task in MAX and load it from within your Visual Basic 6.0 program: 1. From the Start menu, select StartProgram FilesNational InstrumentsMeasurement & Automation Explorer to launch MAX. 2. In the left-hand pane, right-click Data Neighborhood and select Create New. 3. Select NI-DAQmx Task and click Next. 4. Follow the on-screen steps to create and configure the NI-DAQmx task. 5. After the task has been successfully created and saved in MAX, use the DAQmxLoadTask function to load the task in the program.

Using and Clearing Tasks


and DAQmxLoadTask have output (ByRef) parameters that return task handle variables. You typically pass these task handle variables to subsequent NIDAQmx function calls to identify the task onto which you want the driver to execute the function. When you are finished with the task, you must call DAQmxClearTask to free the hardware and software resources associated with the task. After you call DAQmxClearTask, you cannot use the task handle unless you create it again.
DAQmxCreateTask

Caution: Neglecting to call DAQmxClearTask before terminating your Visual Basic 6.0 program debugging session causes the NI-DAQmx driver to keep the task resources reserved. Reusing reserved resources, such as the task name, in subsequent debugging sessions causes errors. To fix this problem, you must restart Visual Basic 6.0 to unload the NI-DAQmx driver and free the resources.

Getting, Setting, and Resetting Properties


The NI-DAQmx driver provides programmatic access to properties that you can use to configure very specific attributes of the driver. The NI-DAQmx C API Visual Basic 6.0 Support Beta includes functions you call to get, set, or reset these properties. For example, the NI-DAQmx C Reference Help contains the SamplesPerChannel property in the List of Timing Properties topic. In Visual Basic 6.0, you can get, set, or reset this property using the following functions:
DAQmxGetSampQuantSampPerChan DAQmxSetSampQuantSampPerChan DAQmxResetSampQuantSampPerChan

Properties that are read-only have only a get function. For a complete list of properties and get/set/reset functions, refer to the NI-DAQmx C Properties section of the NI-DAQmx C Reference Help.

Handling Errors
All of the functions in the NI-DAQmx API return an integer value indicating whether the function executed successfully. A value of 0 indicates success. A positive value indicates a warning. A negative value indicates an error. You can pass this error code to the DAQmxGetErrorString and DAQmxGetExtendedErrorInfo functions to get additional information. All of the NI-DAQmx C API Visual Basic 6.0 Support Beta examples use the DAQmxGetErrorString function. For additional help on the error functions, refer to Error Handling in the NI-DAQmx C Functions section of the NI-DAQmx C Reference Help.

Using Enumerations
The NI-DAQmx C API includes named constants that you use to specify predefined parameter values for some functions. The NI-DAQmx C API Visual Basic 6.0 Support Beta groups these constants into Visual Basic 6.0 enumeration types. The Visual Basic 6.0 editor has built-in support for enumerations. While you are typing a call to a function that is defined in the NI-DAQmx type library, the Visual Basic 6.0 editor recognizes when a parameter is of an enumerated type and displays a list of valid values. The name of each enumeration value is different than the equivalent NI-DAQmx C APInamed constant, even though internally they are both set to the same integer value. Note: If you are copying code from the NI-DAQmx C API to the NI-DAQmx Visual Basic 6.0 API, you must translate the C API constant names to their corresponding Visual Basic 6.0 enumeration value names. For more information, refer to the Using C to Create Visual Basic 6.0 Applications section of this document.

Using Events
nidaqmx.tlb includes functions that provide configuration and control for task Use DAQmxRegisterDoneEvent, DAQmxRegisterEveryNSamplesEvent, or

events.

DAQmxRegisterSignalEvent

to register the event.

The following events are supported: Every N Samples EventOccurs when the specified number of samples is written from the device to the buffer or from the buffer to the device. Counter Output EventOccurs when any of the counters used in the task reaches its terminal count. Digital Change Detection EventOccurs when a digital change is detected on any of the digital lines used in the task. Task Done EventOccurs when the task finishes, either successfully or due to an error. Sample Clock EventOccurs on each pulse of the task's sample clock. Sample Complete EventOccurs when each sample is acquired into the NIDAQmx driver buffer and is ready for reading.

For additional information, refer to the Events topic under Task Configuration/Control in the NI-DAQmx C Functions section of the NI-DAQmx C Reference Help.

NI-DAQmx C Reference Help with Visual Basic 6.0


The function names in the NI-DAQmx C API and the NI-DAQmx Visual Basic 6.0 API are identical. You can use the NI-DAQmx C Reference Help to look up the descriptions of the DAQmx functions in the Visual Basic 6.0 DAQmx API. To launch the help from the Windows Start menu, select StartProgramsNational InstrumentsNI-DAQNIDAQmx C Reference Help. All NI-DAQmx Visual Basic 6.0 functions are covered in the following two sections of the NI-DAQmx C Reference Help: NI-DAQmx C Functions NI-DAQmx C Properties

In addition to the function names being the same in both APIs, in most cases, the names of the parameters are identical as well. The main difference between the NI-DAQmx C API and the NI-DAQmx Visual Basic 6.0 API is the data type of parameters. For example, the NI-DAQmx C API function may contain types such as const char [] or uint32. Because these data types are not valid in Visual Basic 6.0, the nidaqmx.tlb type library specifies the appropriate Visual Basic 6.0 data type. When you use the Object Browser in Visual Basic 6.0, you see that the DAQmx functions use data types that are valid in Visual Basic 6.0.

Differences Between the NI-DAQmx C API and the NIDAQmx Visual Basic 6.0 API
The primary difference between the C API and the Visual Basic 6.0 API is the data type of function parameters. The nidaqmx.tlb type library specifies the translated parameter

data types. In addition to data types, there are several other differences between the two APIs, including the following: Visual Basic 6.0 does not support unsigned numbers. If a function uses an unsigned data type in C, you must use the signed equivalent in Visual Basic 6.0. This can reduce the range of values you can pass for some function parameters. Visual Basic 6.0 does not support 64-bit numbers. If a function uses a 64-bit data type in the C API, you must use a 32-bit number in Visual Basic 6.0. This can reduce the range of values you can pass for some function parameters. When using the nidaqmx.tlb in Visual Basic 6.0, numeric array parameter types display as scalar parameter types in the Object Browser and the editor. For example, if a function parameter is an array of doubles in C, the Object Browser and source editor display it as a ByRef double. o You must refer to the documentation of the function in the C API to determine if a parameter requires an array or not. Appendix A of this document lists all of the functions in the Visual Basic 6.0 API that use arrays. o Pass the first item in the array for the parameter value to pass the entire array to the NI-DAQmx driver. Because the array type is ByRef, the NIDAQmx driver is able to read the entire array of values. Visual Basic 6.0 does not support variable parameter functions. The following variable parameter functions have different parameter sets in C and Visual Basic 6.0. The differences in the parameter sets are more than just simple data type translations.
o DAQmxSetDigitalPowerUpStates

In the NI-DAQmx C API, the variable parameter list allows you to specify combinations of additional channels and states (high or low) to set the channels to when the device powers up or when the device is reset. In the NI-DAQmx Visual Basic 6.0 API, you must use an array to specify the channels and states. Each value in the states array applies to the channel in the channels array at the corresponding index position. In the NI-DAQmx C API, the variable argument list allows you to specify combinations of additional channels, types (current or voltage), and states (value of the current or voltage) to set the channels to when the device powers up or when the device is reset. In the NI-DAQmx Visual Basic 6.0 API, you must use an array to specify the channels, types, and states. Each value in the types and states arrays applies to the channel in the channels array at the corresponding index position. In the NI-DAQmx C API, the variable argument list allows you to specify combinations of additional channels and expiration states

DAQmxSetAnalogPowerUpStates

DAQmxCreateWatchdogTimerTask

to set the channels to when the device powers up or when the device is reset. In the NI-DAQmx Visual Basic 6.0 API, you must use an array to specify the channels and expiration states. Each value in the expiration states array applies to the channel in the channels array at the corresponding index position. The NI-DAQmx C API event registration functions allow you to specify whether an event is synchronous or asynchronous. Visual Basic 6.0 only supports synchronous events. The Visual Basic 6.0 event registration functions omit this parameter.

Examples
Using Visual Basic 6.0
The NI-DAQmx C API Visual Basic 6.0 Support Beta installs the following Visual Basic 6.0 examples:
Analog In/MultipleFiniteAcquisition Analog In/SinglePointAcquisition Analog Out/ContGenVoltWfmIntClk Counter In/CountDigEvents Counter Out/ContDigPulseTrain Digital In/ReadDigChan Digital Out/WriteDigChan

Using C Examples to Create Visual Basic 6.0 Applications


The NI-DAQmx Visual Basic 6.0 type library is based on the NI-DAQmx C API. Therefore, in addition to the installed Visual Basic 6.0 examples, you can also refer to the NI-DAQmx C API examples when creating a new NI-DAQmx application in Visual Basic 6.0. The C examples are installed to the NI-DAQ\Examples\DAQmx ANSI C Dev directory. Complete the following steps to use a C example to create a Visual Basic 6.0 application: 1. Locate a C example similar to the one you want to create in Visual Basic 6.0. 2. Use a text editor to load the example .c file. 3. All of the C examples use a DAQmxErrChk macro. The Visual Basic 6.0 examples use a Visual Basic 6.0 module that provides similar error checking. The DAQmxErrChk function is present in the NIDAQmxErrorCheck.bas module in the Shared folder. 4. Use Visual Basic 6.0 to create a new Visual Basic 6.0 Standard EXE project. 5. Add a reference to nidaqmx.tlb. 6. Add NIDAQmxErrorCheck.bas module to your project. 7. In the .c file, locate all the callsother than DAQmxErrChkthat are prefixed with DAQmx. These are the calls being made to the NI-DAQmx driver.

8. Make the same function calls to the driver in your Visual Basic 6.0 program. 9. In many cases, you are able to simply copy and paste the code from the C program to the Visual Basic 6.0 project, including the DAQmxErrChk macro, if you have added NIDAQmxErrorCheck.bas to your project. After you copy and paste the code, you must complete the following cleanup items: Remove trailing semicolons. Declare variables that are used by the functions. Replace C constant values with Visual Basic 6.0 enumerations. Refer to the Enumerations section of this document for more information. Replace any C statements with Visual Basic 6.0 equivalents. For example, replace while loops with for loops.

Deploying NI-DAQmx Visual Basic 6.0 Applications


When distributing Visual Basic 6.0 applications that contain NI-DAQmx functions, you do not need to redistribute the nidaqmx.tlb type library. However, the deployment target machine must have the NI-DAQmx driver installed. For detailed information on NI-DAQmx deployment, refer to the Deployment topic under Key NI-DAQmx Concepts, located in the NI-DAQmx Concepts section of the NIDAQmx C Reference Help. You can launch the help from the Windows Start menu by selecting StartProgramsNational InstrumentsNI-DAQNI-DAQmx C Reference Help.

Frequently Asked Questions


How do I pass data to a reserved parameter?
A number of DAQmx functions contain parameters that are reserved for future use. In C, you must pass NULL to this parameter. In Visual Basic 6.0, you must pass the number 0 (ByVal 0&).

How do I know whether my function uses a double or a double array?


Identifying whether the parameter is an array might not be possible by using the Object Browser alone. You might have to use the documentation to determine whether a parameter is an array. In most cases, array parameters have an array suffix. Appendix A of this document contains a list of all the functions in the Visual Basic 6.0 API that use an array.

How do I pass an array parameter?


Specify the first item in the array to pass an array as a parameter from Visual Basic 6.0 to the DAQmx C API.

Why do I get Error -200089: Task name specified conflicts with an existing task name when I call DAQmxCreateTask or DAQmxLoadTask?
This error occurs when you neglect to call DAQmxClearTask and then subsequently attempt to create another task with the same name without restarting the program. A common scenario in which this error occurs is while debugging a Visual Basic 6.0 program. If your program creates a task and hits a breakpoint and then you choose to stop debugging before your code calls DAQmxClearTask, you will get error -200089 the next time you run your program. This behavior is particular to Visual Basic 6.0 because Visual Basic 6.0 loads the NI-DAQmx driver into its own process and does not free it when you stop debugging. To work around this problem, ensure that you always call DAQmxClearTask or restart Visual Basic between debug sessions.

Why should I use a type library instead if a .bas file?


Type libraries are compound document files (.tlb files) used in Automation. They contain important information about the types, objects, modules, and interfaces exposed by an Automation server to its clients. Fortunately, a server doesn't need to be Automation-aware to take advantage of a type library. In fact, most C DLLs are not Automation servers. All that is required is that the C DLL declares its functions as members of a module in a type library. An Automation client, such as Visual Basic, can read this information and bind to it as it would any object. No need for Declare statements or hard to remember constants because Visual Basic does all the work. There are several advantages in creating a type library for your DLL. The most important of these is better type safety. But you also get the advantage of better performance, because Visual Basic automatically binds to your functions using earlybinding. In contrast, all Declare statements are late-bound. Furthermore, you gain greater control over the way your DLL is presented to Visual Basic programmers. The type library allows you to provide Visual Basic-friendly names for functions and parameters, along with helpful extras like enumerations and User Defined Types (UDTs). Currently, type libraries are created using scripts written in either the Interface Definition Language (IDL) or the Object Description Language (ODL). These scripts are then compiled using MkTypLib.EXE or MIDL.EXE that come with Visual Studio. Visual C++ takes some of the work out of creating type libraries, because any ODL files that you associate with your DLL project will automatically be compiled with MIDL when you compile your project.

http://support.microsoft.com/kb/189133/EN-US/#kb2

Appendix A Functions with Array Parameters


The following table shows functions that use array parameters in the NI-DAQmx Visual Basic 6.0 API. Functions DAQmxReadAnalogF64 DAQmxReadBinaryI16 DAQmxReadBinaryI32 DAQmxReadCounterF64 DAQmxReadCounterScalarF64 DAQmxReadDigitalLines DAQmxReadRaw DAQmxWriteAnalogF64 DAQmxWriteBinaryI16 DAQmxWriteBinaryI32 DAQmxWriteDigitalLines DAQmxWriteCtrFreq DAQmxWriteCtrTime DAQmxWriteCtrTicks DAQmxWriteRaw DAQmxCreatePolynomialScale DAQmxCreateTableScale DAQmxCalculateReversePolyCoeff DAQmxSwitchGetMultiRelayCount DAQmxSwitchGetMultiRelayPos DAQmxCreateWatchdogTimerTask DAQmxWriteToTEDSFromArray DAQmxSetDigitalPowerUpStates DAQmxSetAnalogPowerUpStates Array Parameter readArray readArray readArray readArray readArray readArray readArray writeArray writeArray writeArray writeArray frequency, dutyCycle highTime, lowTime highTicks, lowTicks writeArray forwardCoeffs, reverseCoeffs prescaledVals, scaledVals forwardCoeffs, reverseCoeffs count relayPos lines, expStates bitStream channelNames, states channelNames, states, channelTypes

Вам также может понравиться