Home Features
& Benefits What Is OPC Free Demo Purchasing Sample Code 
 & Support

Read/Write of OPC Data with OPC Data Control using VB Code

You may want to have the flexibility of read/writing multiple items of data using VB code to give you a little more control. The OPC Data Control provides some built in methods to handle reads and writes of single items or arrays of items from the OPC server. These methods also perform Synchronous Reads/Writes per the OPC Specification. If your application requirements are to perform synchronous reads/writes as opposed to asynchronous operations, this application note will help you.

This example illustrates how to use the ReadVariable, WriteVariable, ReadMultiVariable and WriteMultiVariables methods in the ActiveX control.  You will need to have downloaded and installed the OPC Data Control demo into your application if you plan to implement this on your computer. You may also download the sample Visual Basic Code we used and modify it to meet your requirements for your OPC server..

This example was written for the moderately-experienced Visual Basic (VB) programmer and makes some assumptions that you are comfortable moving around in the Visual Basic development environment. If you have questions regarding the properties, methods or events in the OPC DataControl there is on-line help available with the control - please take the time to read it.

To access the online help for the OPC Data Control from Visual Basic place the OPC Data Control on your form and right click on the OPC Data Control. Click on Properties and select the Help button on the screen that appears. Also the Help file is accessible by clicking Start->Programs->OPC Data->OPC Data Control Help. Last, if you don't want to install the OPC Data Control you can download our help file.

For this sample project we have built the form shown below. As you can see we have command buttons for Single Write, Single Read, Multiple Write and Multiple Read. We also have text boxes to allow the user to enter the values to write and labels to display the read and write results. We have also inserted the OPC Data Control (shows up as "OPC Data Control" in the VB components list) onto the form.(help on inserting OPC data control)) Right click on the OPC Data Control and then click on Properties as shown below to display the OPC Data Control property pages.

Form Layout in VB

Click on the General tab on the OPC Data Control property page and click the AutoConnect check box as shown here.

This will disable AutoConnect for the OPC Data control. The AutoConnect property allows us to specify whether or not the configured connections to OPC servers are automatically started up and established at runtime.

For this project we want to establish connections when the user clicks one of the Read or Write command buttons. You don't necessarily have to do it this way, we just chose to wait to connect to the OPC server until the user is ready to read or write. In our code we'll disconnect from the OPC server when the user exits the program. The way this is implemented is that the OPC Data ActiveX will automatically connect to the specified OPC server when the user executes a ReadVariable, WriteVariable, ReadMultiVariables, or WriteMultiVariables method call - there is a connect method available that you can also call on your own, but it is not necessary to do so.

Now click the OPC Server tab on the properties page and click the Browse button as shown below to find the OPC Server that you want to use, either local or remote. Once you have selected the OPC server you wish to connect to, click Apply and OK to dismiss the OPC Data Control's property pages.

Once you have completed this setup it's time add the code. 

You can view code with comments or without.

Property Pages- General

Copyright Software Toolbox, Inc., 1996-2002, All Rights Reserved Worldwide.
148A East Charles Street, Matthews, North Carolina, USA 28105
Phone: 704-849-2773 or 1-888-665-3678 (US), Fax: 704-849-6388
Business hours - Monday to Friday, 8 AM to 5 PM EST (GMT-5)