Sunteți pe pagina 1din 7

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.




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.



Read/Write of OPC Data with OPC Data Control using VB Code
Writing the Code
Now it's time to write the Visual Basic code for this example. Double click the Exit button on your form. This
will bring up the code section for our form. Important: In the code shown below, you only need to insert the
code NOT shown in blue text. Code comments are shown in green. Note that this code is heavily
commented to try and be self explanatory - dont' take the length of this document as any indication of how
much code there really is! - -If you throw out our comments this code collapses down significantly.
Feel free to "cut and paste" the code from here. You may also download our code exactly as it's written for
this document and modify it to suit your needs.
Insert the following code for Exit button click event.



Private Sub cmdExit_Click()
Dim result As Long
'disconnect from OPC Server before exiting
result =OPCData1.Disconnect
'end program
End
End Sub
Insert the following code for Multiple Write button click event.
Private Sub cmdMultipleWrite_Click()
Dim result As Long
Dim States As Variant
'Array of 2 variants (0 through 1)
Dim VarValues(0 To 1) As Variant
'clear write result labels
Label1 =""
Label2 =""
'Array of 2 strings (0 through 1)
Dim VarNames(0 To 1) As String
' VERY IMPORTANT the array element values must exactly match the
' items in the OPC server to be written to
VarNames(0) ="Device1.Group1.Tag1"
VarNames(1) ="Device1.Group1.Tag2"
'get the values to write from the user interface form
VarValues(0) =txtMultipleWriteValue1.Text
VarValues(1) =txtMultipleWriteValue2.Text
'The WriteMultiVariables method writes new values for several OPC
'items in the OPC Server.
'WriteMutipleVariables method syntax is as follows
'result =object.WriteMultiVariables(VarNames, VarValues, States)
'The WriteMultiVariables method has these parts:
'object=identifier for the specific OPC Data control
'VarNames=Variant that specifies the array of OPC items
'in the OPC Server.
'VarValues=Variant that contains an array of the corresponding
'values to be written to the specified OPC items.
'States=Variant that contains an array of the OPC quality code
'(Long) for each of the OPC items.



'result=Long value that indicates whether an error has occurred.
'result is zero if no error occurs.
result =OPCData1.WriteMultiVariables(VarNames, VarValues, States)
'States(i) contains the state of the item with name VarNames(i)
'display write results
If result =0 Then
Label1 ="Multiple Write Success"
Else
Label1 ="Multiple Write Failure"
End If
End Sub
Type the following code for Multiple Read button click event.
Private Sub cmdMultipleRead_Click()
Dim result As Long
Dim States As Variant
Dim VarValues As Variant
'Array of 2 strings (0 through 1)
Dim VarNames(0 To 1) As String
'clear MultipleRead result
Label2 =""
' VERY IMPORTANT the array element value must exactly match the item names in the OPC server be
read

VarNames(0) ="Device1.Group1.Tag1"
VarNames(1) ="Device1.Group1.Tag2"
'The ReadMultipleVariables method reads the values of several OPC Items in the OPC Server.
'ReadMultipleVariables method syntax is as follows
'result =object.ReadMultiVariables (VarNames, VarValues, States)
'The ReadMultipleVariables method has these parts:
'object identifier for the specific OPC Data control
'VarNames Variant that specifies the array of OPC items
' to be read from the control engine.
'VarValues Variant that contains an array of the corresponding values
' of the specified OPC items in the OPC Server.
'States Variant that contains an array of the quality code (Long)
' for each of the OPC items.
'result Long value that indicates whether an error has occurred.
' result is zero if no error occurs.
result =OPCData1.ReadMultiVariables(VarNames, VarValues, States)



'VarValues(i) and States(i) contain value/state of the item with name VarNames(i)
'display read result
If result =0 Then
Label2 ="Multiple Read Success Value 1 =" & VarValues(0) & " Value 2 =" &
VarValues(1)
Else
Label2 ="Multiple Read Failure"
End If
End Sub
Type the following code for Single Read button click event.
Private Sub cmdSingleRead_Click()
Dim result As Long
Dim State As Long
Dim Value As Variant
'clear read results
Label4 =""
'The ReadVariable method reads the status of one specific OPC item in the OPC server.
'ReadVariable Method syntax is as follows
'result =object.ReadVariable (VariableName, Value, State, TimeOut)
'The ReadVariable method has these parts:
'object identifier for the specific OPC Data control
'VariableName String expression that specifies the OPC item
' in the OPC server to be read.
'Value Variant value containing the content of the specified
' OPC item in the OPC server.
'State Long value that provides the quality code for the OPC Item.
'TimeOut Long value that determines the length of time(in ms)
' before a time-out error.
' If TimeOut value is 0 ReadVariable will not time out
'result Long value that indicates whether an error has occurred.
' result is zero if no error occurs.
' VERY IMPORTANT the 1st parameter in the ReadVariable methodmust exactly match the item
'name in the OPC server be read - modify to match your OPC server's needs
result =OPCData1.ReadVariable("Device1.Group1.Tag1", Value, State, 3000)
'display read result
If result =0 Then
Label4 ="Single Read Success Value =" & Value
Else
Label4 ="Single Read Failure"
End If
End Sub
Type the following code for Single Write button click event.



Private Sub cmdSingleWrite_Click()
Dim result As Long
Dim Value As Variant
'clear read and write results
Label3 =""
Label4 =""
'Write the value entered by user to the item
Value =txtSingleWriteValue
'The WriteVariable method writes a new value to a specific OPC item in the OPC Server.
'The WriteVariable syntax is as follows:
'result =object.WriteVariable(VariableName, Value, TimeOut)
'The WriteVariable method has these parts:
'object identifier for the specific OPC Data control
'VariableName String expression that specifies the OPC item in the OPC server.
'Value Variant value containing the content to be written
' to the specified OPC item in the OPC Server.
'TimeOut Long value that determines the length of time(in ms)
' before a time-out error.
' If TimeOut value is 0 WriteVariable will not time out
'result Long value that indicates whether an error has occurred.
' result is zero if no error occurs.
' VERY IMPORTANT the 1st parameter (VariableName) in the WriteValue method must exactly match the
'item name in the OPC server to be written to
result =OPCData1.WriteVariable("Device1.Group1.Tag1", Value, 3000)
'display write result
If result =0 Then
Label3 ="Single Write Success"
Else
Label3 ="Single Write Failure"
End If
End Sub
You should now be able to run your project and access data from your plc

S-ar putea să vă placă și