This section describes the general layout of a counter application in Visual Basic using Measurement Studio ActiveX controls. This structure applies to most counter operations. This section also describes the most common properties, methods, and events used in counter applications.
Before Getting Started
Before programming your application, you must add the Measurement Studio ActiveX controls to the Visual Basic developing environment. To add these components to your project, follow these steps:
- Right-click on the Visual Basic Toolbox and select Components. A dialog box opens that contains a list of the components installed in your system.
- Select National Instruments CW DAQ. This adds some new icons in your toolbox, as shown in the following figure.
In order to use a control in your application, you must place the control on the form. Once on the form, you can access the control’s properties and methods in the code through the control name. The default names of the counter control and pulse control are CWCounter1 and CWPulse1, respectively.
Setting Counter Properties
The counter properties specify which operation to perform and how to perform it. Properties allow you to specify parameters such as the type of operation, buffer setting, buffer size, pulse duration, and the source and gate signal source. The type of operation determines which properties to set.
Property pages allow easy navigation through the counter properties of each counter operation. Depending on the counter operation selected, the property pages automatically enable and disable properties. This feature is useful when setting counter options in your code because it allows you to see the relation between different properties. The Help button on the property pages opens the reference for the control and provides detailed explanations of control properties.
Set the counter properties through the property pages of the ActiveX control or within your code. To access the property pages, right-click the CWCounter or CWPulse icon on the form, and select Properties. The following figure shows the property pages for the CWCounter control. Refer to the Line Selection section of this tutorial for more information.
The red area shows the Clock, Gate, and AuxLine property pages. These pages allow you to select the signals the counter uses for each of these counter terminals. The blue area shows the Measurement Type. This drop-down list allows you to select the counter operation. The green area shows the buffer settings.
The following figure shows the property pages for the CWPulse control. Refer to the Line Selectionsection of this tutorial for more information.
The red area shows the Pulse Settings and Gate property pages. These pages allow you to specify the signal the counter uses for the counter operation.The blue area shows the Pulse Type. This drop-down list allows you to select the counter operation.
Property pages allow you to set properties at development time that remain fixed when your application runs. All properties available on the property pages are available in the code. Properties set in the code overwrite values set on the property pages. To set properties at run time, you must explicitly set them in your code. The following figure shows how to access the properties in your code.
In Visual Basic, typing the control name followed by a period pulls up a list containing the available control properties and methods. Properties have the icon next to them, and methods have the icon next to them. This list allows you to quickly select the desired property or method without having to contend with syntax.
Configuring the Counter
When the properties are set, you must configure the counter by calling the CWCounter1.Configure or the CWPulse1.Configure method. Calling this method programs the counter with the properties specified in the previous step but does not start counter operation.
The CWPulse control offers an additional Reconfigure method that allows you to modify only the pulse specifications in a counter generation operation. You can use this method to modify the frequency or duty cycle of a pulse train on the fly.
Starting (Arming) the Counter
When the counter is configured, start the operation by calling the CWCounter1.Start or the CWPulse1.Start method. Calling this method begins the measurements or the generation.
On finite operations, such as single-pulse generation, the Start method can be called to restart the operation. The counter remains configured until the Reset method is called or the configuration is changed using the Configure method.
The counter remains armed until it finishes the operation. After the operation is performed, the counter can be rearmed or restarted using the same configuration. In retriggerable pulse generation, the counter rearms automatically after the pulse is generated.
Note: This section applies to measurement operations only.
When the counter starts, you can get the counter measurement either by querying the counter or through an event, depending on the counter operation. By knowing how events are generated, you can determine which of these your program uses to get the counter measurements.
When the counter is ready to transfer data, the DAQ hardware generates a signal that informs the system that data needs to be transferred. When the data is transferred to the Traditional NI-DAQ (Legacy) driver, the driver uses an event to inform the application that data is ready. The application then handles the event and retrieves the data. An event is generated anytime the counter determines that the operation is complete.
For other operations, such as non-buffered event counting, time measurements, and position measurements, the program must query the counter value. When performing these operations, the counter continuously changes the count register, depending on the signals it receives. The DAQ hardware cannot detect when the operation is complete, so the application must query the counter for the measurement.
The two CWCounter methods that query the counter for the value of the count register are CWCounter1.ReadMeasurement and CWCounter1.ReadCounter. The ReadMeasurement method returns the counter measurement in the units of the measurement. The ReadCounter method returns the raw value of the counter’s count register. ReadMeasurement is the more commonly used method.
For period, semi-period, two-signal edge-separation, and frequency measurements, the counter takes a single reading and stops. In these cases, the DAQ hardware can determine that the operation is complete and then fire an event to inform the application the measurement has been taken.
In all buffered operations, the measurements are taken when an active edge occurs at the counter gate. The counter is programmed to take a predetermined number of measurements. When these measurements are taken, the counter can transfer the data to the system, and an event is fired. In buffered operations, the application uses the Callback function to handle the event. When the measurements are ready, the system automatically calls this function. The Callback function, shown below, automatically generates in your code by double-clicking the CWCounter icon on the form.
Private Sub CWCounter1_AcquiredData(Measurement As Variant, Overflow As Boolean)
Text1.Text = Measurement
The Callback function has two parameters that the Traditional NI-DAQ (Legacy) driver provides: the Measurement parameter and the Overflow parameter. The Measurement parameter contains the data returned from the counter. It can be a single value or an array of values, depending on the requested operation. The above code returns a single value, which appears in a text box. The Overflow parameter contains a TRUE or FALSE value, which determines if there is an overflow in the count register. If the counter overflows, the measurement is invalid, and a slower frequency timebase signal should be used for this measurement.
Note: Overflow detection is not supported with buffered operations.
Stopping and Resetting the Counter
In finite operations, the counter stops when the operation finishes. In continuous operations, the counter must be explicitly stopped. For example, in a continuous pulse train generation operation, if the application configures and starts the counter but does not explicitly stop the counter, the application will finish execution and the counter will continue to generate a pulse train.
Calling the CWCounter1.Stop method stops a counter operation. When the counter is stopped, it retains its current configuration. Calling the Start method restarts the operation.
Calling the CWCounter1.Reset method resets the counter, which means that the Configure method must be called before starting a new counter operation. This means that the configure method must be called before starting a new counter operation.
This general application structure applies to all counter operations in Visual Basic using Measurement Studio ActiveX control. The next section provides more detailed explanations of other configurations and features available with the Measurement Studio ActiveX controls.