|
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.
|