Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Tutorial
NI Supported: Yes
Publish Date: Nov 27, 2006

Using CW Controls in the .NET Environment and Creating a Simple Project

2 ratings | 4.00 out of 5
Print

Overview

This tutorial demonstrates how to use the Measurement Studio 6.0 CW controls in the .NET environment. Specifically, this tutorial uses the CWDIO control in a C# project.

Creating a C# Project

1. Launch Visual Studio .NET.

2. Create a new project by either going to File>>New>>Project or by clicking "New Project" on the Start Page.

3. Under Project Types, open Visual C# Projects and select Windows Application.

4. Name your project, specify the location you want your new project to be created in, and click OK.


[+] Enlarge Image
Figure 1.

Adding CW Controls in a .NET Project



1. If the Toolbox is not already open, do so by going to View>>Toolbox.

2. Right-click anywhere in the Toolbox and choose Customize Toolbox. This window shows you all the ActiveX controls registered on your system.

3. Choose "CWDIO Control" and click OK.


[+] Enlarge Image
Figure 2.

4. The CWDIO control is now placed in the Toolbox so that you can drop a CWDIO control onto your form.

Creating the User Interface and Writing Code


Create a program that reads a digital port when the "Read" button is pressed and then displays the results in the TextBox control.

1. Drop a CWDIO control onto your form. Notice that the control name defaults to "axCWDIO1."

2. Drop a button control onto your form. Rename it "readButton" and rename its caption "Read" in the property page. If the property page is not already open, you can open it by right-clicking the control and selecting Property.

3. Drop a TextBox control onto your form, rename it "dataTextBox," and remove any text inside the control. Also add a label above the TextBox control with the text, "Data Read:"


Figure 3.

4. Right-click the CWDIO control and open the property page. Configure Device and change Port Assignment to be Input. Note that the configuration can also be done programmatically.

5. Double-click readButton so that it creates a skeleton for the callback function in the source file.

6. CWDIO.SingleRead takes an "object" parameter by reference, not a Variant pointer. So, declare an "object" variable for data, as:

object data;

Make sure to initialize data. You can initialize it to null because the C# compiler forces you to initialize a variable before you actually use it.

7. Now you can pass data to SingleRead function. In this particular example, we will read from port 0 only.

axCWDIO1.Ports.Item(0).SingleRead(ref data);

8. Now add the code to display the data in dataTextBox:

dataTextBox.Text = data.ToString();

Note that you have to explicitly convert an object type (or any type that is not String) to String. You can do that by calling the ToString function on the object.

Complete Example Code


The callback function for readButton has only three lines of code, as shown below:
private void readButton_Click(object sender, System.EventArgs e)
{
object data = null;
axCWDIO1.Ports.Item(0).SingleRead(ref data);
dataTextBox.Text = data.ToString();
}

The following is a link to the EPD that contains the entire program:
See Also:
Using CWDIO Controls in .NET Environment, and Creating a Simple Project
CWDIO Measurement Studio 6.0 ActiveX Control, Used in Visual C# .NET

Miscellaneous


Note that the object variable--in this example, object data--must always be set to null before passed by reference to SingleRead (or any other CW functions that take an object parameter reference). Otherwise, you get a type mismatch exception at run time. This exception occurs because after the function call, object data becomes an int pointer and not a Variant pointer anymore. Therefore, if you try to pass the object variable again to SingleRead function, which is expecting a Variant pointer under the wrapper class that .NET created for you, a type mismatch error is returned.
Related Links:
Using Measurement Studio ActiveX Controls in Visual Studio .NET
Visual Studio .NET -- The Latest Microsoft .NET Component
2 ratings | 4.00 out of 5
Print

Reader Comments | Submit a comment »

What if CWDIO is not installed
I don't have CWDIO in my list of COM components. I have Measurement Studio 7.0 enterprise installed. This document does not tell me what features I have to install in order to be able to use the CW Controls.
- May 26, 2004

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).