|
Click for Printer Friendly Version
Jump to sample code download for this page
With the new Microsoft release of Visual Studio.NET we are realizing that many people are migrating to this exciting new development platform. With new technology comes change, and this can and often means " we might .NOT be compatible with .NET". But before we begin, we should say that we have had good results in our OPC Data Control ActiveX testing from within .NET.
This document will explain the following:
- If and how the OPC Data Control ActiveX works within Visual Studio.NET
- The differences in using the OPC Data Control ActiveX in .NET vs. VB5 or
VB6.
- Software Toolbox Technical Support
Intended Audience & Assumptions Made
Users who already own, or plan to use the OPC Data Control ActiveX within the
Visual Studio.NET environment must read this document.
It is the assumption of Software Toolbox that the reader of this document
understand the following:
- The OPC Data Control ActiveX is designed for VB5 and VB6
- There are significant differences between how VB5/6 and VB.NET work
- The user has a good understanding of ActiveX terminology and/or experience
with ActiveX development
- The user has a basic understanding of VB.NET
What This Document Will Cover
This document will cover the following:
- How Connecting Tags to Objects via the Property Screen has changed
- The differences in parameters that apply to many of the ActiveX
methods/events etc.
- How to use ReadVariable, WriteVariable, ReadMultiVariables and
WriteMultiVariables methods
- How to use ConnectName and ConnectObject methods
- How to use the ConnectionError and ValueChanged Events
This document will not cover (for Technical Support reasons):
- Dynamic instantiation
- Wrapping the control
- Developing web-based systems
- Developing multi-threaded applications
- Developing Server or Service-based applications
- Developing Console based applications
Important Information
COM based ActiveX components such as the OPC Data Control ActiveX are run
within a ".NET COM-wrapper" that enables these controls to operate
within .NET. There are currently known issues with how this architectural
component handles ActiveX controls which can lead to instability
issues. Please refer to Microsoft (msdn.microsoft.com)
for more up to date information and patches/service packs.
In VB5/VB6 you could quickly and easily connect OPC Tags to Visual Basic
objects like Textboxes, Labels, Forms or other COM based components by using the
Property screen of the ActiveX control. Please refer to our
Simple Visual Basic Read Tutorial - No Code Required guide for more
information about using the ActiveX control in this way.
In Visual Basic, the Form controls (as shown below) were COM based
components.
|

Visual Basic Form Controls available to the applications developer
- all are COM based components
|
In Visual Studio.NET, these Form controls are replaced with the
.NET framework components instead:
|

Visual Studio.NET Form Controls - all are .NET framework components
|
As
previously stated, the OPC Data Control ActiveX was developed for Visual Basic
5/6.
When using the Property screen of the OPC Data Control ActiveX to connect
Tags to Objects, in VB you would see the Form labels, textboxes and other
objects as seen on your form. The image to the right shows this...
From this screen, you would typically highlight the Property of the Control
(in the left-side of the window) and then Browse a Tag (in the right-side of the
window) to connect the Tag.
Upon completion, you would click OK and then your Object would be subscribed
to the configured OPC Tag.
This
is different now in Visual Studio.NET because of the .NET framework components
replace those older COM-based components.
Therefore, even if you created the same looking form within Visual Studio.NET
(using the .NET framework components instead) then upon opening the Properties
screen of the Data Control, you would have a list of "nothing" to
choose from. The screen to the right shows this:
+Why is this ?+
This is because the OPC Data Control ActiveX is looking for COM-based components. In Visual Studio.NET the same form creation components (label, textbox etc.) are now .NET components instead. The OPC Data Control ActiveX is unaware of these different types of Object and therefore cannot see them.
You can add COM, or Custom COM-based objects to your form and connect/subscribe Tags to them...
here's how:
- from within the Visual Studio Developer Environment, right-click in the
toolbox and choose "Customize Toolbox"

- from within this window, scroll down to the "Microsoft Forms
2.0..." section of objects. Simply select those items you wish to use,
in the example window above, we are using the label and textbox objects.
Click the OK button to select those items and to place them within your
toolbox in the Developer Environment.

- Now place an instance of these controls on your form, and you can now use
the property screen of the OPC Data Control ActiveX to map OPC Tags to these
objects and/or use the "connectObject()" method to accomplish the
same thing.
but the point of this note is to specifically mention that the new form controls use the .NET framework and can't be used by the Data Control ActiveX.
Parameter Changes with Various OPC Data ActiveX Methods
With the new .NET framework has come some changes that affect the way data is
passed into/out of various Methods of the OPC Data Control ActiveX. This change
is "transparent" in the same way the old Windows 3 window icons were
transparently replaced on all Windows applications when people upgraded to
Windows 95.
Three important changes that effectively summarize the majority of changes to
the way you use the OPC Data Control Methods are:
- Variant data types are now Object data types
- Events now have a Sender parameter
- Event parameters are now encased within an E object
Variant Data Type
The Visual Basic Variant data-type still exists within Visual Studio.NET;
however it works +differently+.
Because everything in .NET is object oriented, the Variant data-type has been replaced by VariantType and is now an object. As an object, it has properties, methods and other interfaces that can be and are attached to it.
Some methods of the OPC Data Control are designed to use the
"Variant" data type. Within those methods (internally to the
ActiveX control) the data type is changed to match that of the data, i.e.
changed to Bit, Integer or Float etc. When passing in an Object you have to
make sure that the object is defined but not created. If you use an existing
object, then you should destroy it prior to calling the method. This is
because the method will try to create the Object for you, which it can't do
because it has already been created... this will generate an error.
There will be times when the OPC Data Control documentation specifies a
"Variant" data-type to be used. In these situations you will use a
data type of "Object" instead. As with any object, you must remember
that it has to be created and destroyed.
More information and an example will be shown at the relevant places within
this document.
Events have a Sender Property
This is coming from the root of the .NET framework. For all intent and
purposes, you +can+ ignore
this property.
In Visual Basic 5/6 each object was already determined with the events it had, i.e. if you added 2 buttons on a form, both of them automatically get an "onClick" event. This is no longer the case in .NET. Now you can not only define your own events, but you can take one event and have many different objects use it!
In this situation where many objects are sharing a single event, then you would make use of this Sender property so you knew which object had "fired the event!".
The 'E' Object
The "E" object will now been seen in the Event parameters. This object,
like any other has properties and methods. You can see a list of the
properties/methods available by typing "e." from within the
procedure. You will then see a drop-down of the Properties/Methods available
to this object. This topic will be discussed further in this paper.
How to use ReadVariable, WriteVariable Methods
The best way to show the difference is to show a comparison between VB5/6
and VB.NET:
+ReadVariable
Method+
| Visual Basic 5/6 |
Visual Basic.NET |
Private Sub Command1_Click( )
Dim result, State As Long
Dim Value As Variant
result = OPCData1.ReadVariable("ITEMNAME",
Value, State, 0)
If result = 0 Then
msgbox "successfully executed"
else
msgbox "Failed to execute properly"
end if
if State = 192 Then
msgbox "Quality is good"
else
msgbox "Quality is NOT good"
end if
End Sub |
Private Sub cmdReadVariable_Click(ByVal
sender As System.Object, _
ByVal e As System.EventArgs) _
Handles cmdReadVariable.Click
Dim result, State
As Long
result =
axOPCData1.ReadVariable("ItemName", value,
State)
If result
= 0 Then
MsgBox("Successfully executed")
Else
MsgBox("Error connecting name to
object: " & result)
End If
If State
= 192 Then
MsgBox("Quality is Good")
Else MsgBox("Quality is Not Good:"
& State
End If
End Sub |
|
As you can see, there is very little difference in
coding between VB5/6 and VB.NET |
+WriteVariable Method+
| Visual Basic 5/6 |
Visual Basic.NET |
Private Sub Command1_Click( ) Dim result As Long Dim Value As Variant Set Value = 1 ‘ Write the value 1 to the item
result = OPCData1.WriteVariable("ITEMNAME",Value, 0)
If result = 0 Then msgbox "Successfully written" else msgbox "Failed, error: " & result end if End Sub
|
Private Sub cmdWriteVariable_Click(ByVal
sender
As System.Object, _
ByVal e As System.EventArgs)
_
Handles cmdReadVariable.Click
Dim result,
ValueAs
Long
Value = 10
result = axOPCData1.WriteVariable("ItemName",
value,
0)
If result
= 0 Then
MsgBox("Successfully written")
Else
MsgBox("Error writing to tag::
" & result)
End If
End Sub
|
|
As you can see, there is very little difference in
coding between VB5/6 and VB.NET |
How to use ReadMultiVariables and WriteMultiVariables Methods
Again, we will show by example how to use ReadMultiVariables and
WriteMultiVariables by comparison:
+ReadMultiVariables Method+
| Visual Basic 5/6 |
Visual Basic.NET |
Private Sub Command1_Click( )
Dim result As Long
Dim States, VarValues As
Variant
Dim Counter as Integer
Dim VarNames(2) As String
Dim errTrue as Boolean
errTrue = False
VarNames(0) = "Tag_0"
VarNames(1) = "Tag_1"
VarNames(2) = "Tag_2"
result =
OPCData1.ReadMultiVariables(VarNames, VarValues,
States)
If result = 0 Then
msgbox "Successfully executed"
Else
msgbox "Execution failed, error: "
& result
End If
For Counter = 0 to
UBound(States,,1) - 1
If States(intcounter)
<> 192 Then errTrue = True
Next
If errTrue = True
Then Msgbox "Tag(s) Read indicate quality issues"
End Sub
|
Private Sub cmdReadMultiVariable_Click_ (ByVal
sender
As System.Object, _
ByVal e As System.EventArgs) _
Handles cmdReadVariable.Click
Dim
varStates,
varValues
as
Object
Dim lngResult
as
Long
Dim
intCounter
as
Integer
Dim
strTags(2)
as
String
Dim
errTrue
as
Boolean
errTrue
= False
strTags(0)
= ".Tag_0"
strTags(1)
= ".Tag_1"
strTags(2)
= ".Tag_2"
result =
axOPCData1.ReadMultiVariable(strTags,
varValues,
varStates)
If result
= 0 Then
MsgBox("Successfully executed")
Else
MsgBox("Error:
" & result)
End If
For intCounter
= 0 to UBound(varStates,
1) - 1
If varStates(intCounter)
<> 192 Then errTrue
= True
Next
If errTrue
Then MsgBox("Tag(s) quality concern")
End Sub
|
|
Again, there is very little difference in the way you use the ActiveX
control. |
+WriteMultiVariables Method+
| Visual Basic 5/6 |
Visual Basic.NET |
Private Sub Command1_Click( ) Dim result As Long Dim
States
As Variant
Dim
VarValues(1) As Variant
Dim
VarNames(1) As String
VarNames(0) = "ITEM_0"
VarNames(1) = "ITEM_1"
VarValues(0) = 1
VarValues(1) = 2
result
= OPCData1.WriteMultiVariables(varNames,
VarValues,
States)
If result = 0 Then
msgbox "Successfully executed"
Else
msgbox "Execution failed, error: "
& result
End If
End Sub
|
Private
Sub cmdReadMultiVariables_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles cmdReadMultiVariable.Click
Dim lngResult As Long
Dim varStates As Object
Dim varValues() As Object = {100, 200,
300}
Dim varTags() As String =
{"Tag_1", "Tag_2", "Tag_3"}
lngResult =
axOpcData1.WriteMultiVariables(varTags, varValues,
varStates)
If lngResult
= 0 Then
MsgBox("Successfully Executed")
Else
MsgBox("Failed with error: "
& lngResult)
End If
End Sub |
|
Again, very little difference - you can see from this example how similar
VB.NET is to C++ when defining arrays |
How to use the ConnectName and ConnectObject Methods
The methods ConnectName and ConnectObject can be used in the same way as used
in VB6. There is NO DIFFERENCE in the syntax for using these methods, however
THERE IS A DIFFERENCE when choosing objects to bind Tags to. The reason is
because the OPC Data Control is compatible with COM-based objects, not .NET.
This was discussed in the first section First, Connecting Tags to Objects.
For the following example, I am going to connect a tag called "Tag"
to a textbox called "Textbox" and specifically to a property called
"Text" (textbox.text). In the .NET example, I am assuming that
I have already dropped the COM-based Textbox onto the form and have renamed the
control.
+ConnectObject Method+
|
Visual Basic 5/6
|
Visual Basic.NET
|
Private
Sub Command1_Click()
Dim
result
As Long
Dim
connectionTable(3) As String
connectionTable(0) = "Text" 'property
of object to bind Tag to
connectionTable(1) = "Tag" 'Tag
in OPC server to subscribe to
connectionTable(2) = "100" 'subscription
rate, in ms.
connectionTable(3) = "0.0" 'deadband
result
= OpcData1.ConnectObject(textbox,
connectionTable)
If
result
= 0 Then
msgbox "Successfully connected"
Else
msgbox "Failed to connect, error # " &
result
End If
End Sub |
Private
Sub Command1_Click(sender As Object, args As EventArgs) handles
Command1.Click
Dim
result
As Long
Dim
connectionTable(3) As String
connectionTable(0) = "Text" 'property
of object to bind Tag to
connectionTable(1) = "Tag" 'Tag
in OPC server to subscribe to
connectionTable(2) = "100" 'subscription
rate, in ms.
connectionTable(3) = "0.0" 'deadband
result
= OpcData1.ConnectObject(textbox,
connectionTable)
If
result
= 0 Then
msgbox
("Successfully connected")
Else
msgbox ("Failed to connect, error # " &
result)
End If
End Sub |
|
As you can see here, there is
no difference in the syntax when using the ConnectObject method. |
+ConnectName Method+
In the following example, I am going to bind my Tag to the "ValueChanged"
event only.
| Visual
Basic 5/6 |
Visual
Basic.NET |
Private Sub Command1_Click()
Dim
result
As Long
Dim
connectionTable(3) As String
connectionTable(0) = "myTag"
'internal name for this subscription
connectionTable(1) = "Tag" 'Tag
in OPC server to subscribe to
connectionTable(2) = "100" 'subscription
rate, in ms.
connectionTable(3) = "0.0" 'deadband
result
= OpcData1.ConnectName(Nothing,
connectionTable)
If
result
= 0 Then
msgbox "Successfully connected"
Else
msgbox "Failed to connect, error # " &
result
End If
End Sub |
Private
Sub Command1_Click(sender As Object, args As EventArgs) handles
Command1.Click
Dim result As Long
Dim connectionTable(3)
As String
connectionTable(0)
= "Text" 'property of object to bind Tag to
connectionTable(1)
= "Tag" 'Tag in OPC server to subscribe to
connectionTable(2)
= "100" 'subscription rate, in ms.
connectionTable(3)
= "0.0" 'deadband
result =
OpcData1.ConnectName(Nothing, connectionTable)
If result = 0
Then
msgbox("Successfully connected")
Else
msgbox ("Failed to connect, error # " & result)
End If
End Sub |
More information on the ValueChanged event is below, or click here to go there now.
As previously stated, the way Events are handled in VB.NET is different to
VB5/6. So once again, we will show by example the difference between VB5/6 and
VB.NET:
+ConnectionError Event+
| Visual Basic 5/6 |
Visual Basic.NET |
Private Sub OPCData1_ConnectionError _
(State As Long, ConnectedObject As Object,
_
Property As String, Variable As String)
msbox "Error with " & connectObject
_
& "." & property
_
& connected to: " & variable
_
& " --- " & state
End Sub |
Private Sub
OPCData1_ConnectionError _
(ByVal sender As Object, _
ByVal e As AxOPCDATALib._DS7DataEvents_ConnectionErrorEvent) _
Handles OPCData1.ConnectionError
MsgBox("Error: " & e.property _
& " quality=" & e.state _
& " tag=" & e.varName)
End Sub |
|
This time, instead of accessing the parameters directly as in VB5/6 - in
.NET you access them via the E.object |
+ValueChanged Event+
| Visual Basic 5/6 |
Visual Basic.NET |
Private Sub OPCData1_ValueChanged(Property As String,
_
Variable As String, Value as Variant,
Quality as Integer)
msgbox "Tag: " & variable
_
& "=" _
& value _
&
"(" & quality & ")"
End Sub |
Private Sub OPCData1_ValueChanged(ByVal
senderAs Object, _ ByVal
eAs AxOPCDATALib._DS7DataEvents_ValueChangedEvent) _ Handles OPCData1.ValueChanged
MsgBox("Tag: " & e.property_ & " = " & e.value _ & " quality(" & e.quality & ")" _ & " on object: " & e.varName) End Sub |
|
This time, instead of accessing the parameters directly as in VB5/6 - in
.NET you access them via the E.object |
Demo VB.NET Solution To Download
We have created an example application using the OPC Data Control, written
entirely within VB.NET that demonstrates:
- ReadVariable, ReadMultiVariable, WriteVariable and WriteMultiVariable
Methods
- ConnectName and ConnectObject Methods
- ConnectionError and ValueChanged Events
To download this complete VB Solution, click here (OPC_Data_Control_VBDotNetSample.zip - 530 Kb).
Final Comments
You have now been introduced to the changes that .NET imposes upon the OPC
Data Control ActiveX.
For your own advanced understanding of the OPC Data Control ActiveX, be sure
to read the following documents:
Obviously the .NET development environment offers a vast array of Systems
Development possibilities utilizing a wide-spectrum of available technologies.
While Software Toolbox encourages the use of our software within these
applications, we have to draw the line as to what we will support within .NET.
First of all, Software Toolbox supports "Software Toolbox Products used within Visual Studio.NET". This does not mean that we support Visual
Studio.NET. Questions pertaining to the use of, problems with or general advice
for Visual Studio.NET itself should be directed towards Microsoft.
We fully support the OPC Data Control ActiveX when used in the manner as
documented with the control. Applications that place an instance of the OPC Data
Control ActiveX on a form as seen in the Source Code Examples of our download
section, will be supported. Technical Support issues outside of this criteria
will be dealt with by a professional developer whose time will be billed as
defined in our Application
Consulting Services document..
|