📄 kepserverex_simple_vb_opc.frm
字号:
' Disable the item controls now that a group has been removed.
' Items can't be added without a group so prevent the user from editing them.
OPCAddItems.Enabled = False
Dim i As Integer
For i = 0 To 9
OPCItemName(i).Enabled = False
Next i
' Enable the Disconnect Server button since we have removed the group and can disconnect from the server properly
DisconnectFromServer.Enabled = True
End If
GoTo SkipRemoveGroupError
ShowOPCGroupRemoveError:
Call DisplayOPC_COM_ErrorValue("Remove Group", Err.Number)
SkipRemoveGroupError:
End Sub
' This sub allows the group's active state to be changed on the fly. The
' OPCGroup object provides a number of properties that can be used to control
' a group's operation. The '.IsActive' property allows you to turn all of the
' OPC items in the group On(active) and Off(inactive). To see the effect that
' the group's '.InActive' property has on an OPC Server run this demo and connect
' with Server, add the default group, add the default items. Once you see
' changing data click on the CheckBox in the Group frame. If you watch
' the Server OPC Server you will see it's active tag count go from 10 when
' updating to 'No Active Items' when the group is made inactive.
' Changing the actvie state of a group can be useful in controlling how your
' application makes use of an OPC Servers communication bandwidth. If you don't
' need any of the data in a given group simply set it inactive, this will allow an
' OPC Server to gather only the data current required by your application.
Private Sub GroupActiveState_Click()
'Set error handling for OPC Function
On Error GoTo ShowOPCGroupActiveError
' If the group has been added and exist then change its active state
If Not ConnectedGroup Is Nothing Then
ConnectedGroup.IsActive = GroupActiveState.Value
End If
GoTo SkipGroupActiveError
ShowOPCGroupActiveError:
Call DisplayOPC_COM_ErrorValue("Group Active State", Err.Number)
SkipGroupActiveError:
End Sub
' This sub allows the group's deadband to be changed on the fly. Like the
' '.IsActive' property, the '.DeadBand' property can be changed at any time.
' The Deadband property allows you to control how much change must occur in
' an OPC item in this group before the value will be reported in the 'DataChange'
' event. The value entered for '.DeadBand' is 0 to 100 as a percentage of full
' scale for each OPC item data type within this group. If your OPC item is a
' Short(VT_I2) then your full scale is -32768 to 32767 or 65535. If you
' enter a Deadband value of 1% then all OPC Items in this goup would need
' to change by a value of 655 before the change would be returned in the
' 'DataChange' event. The '.DeadBand' property is a floating point number
' allowing very small ranges of change to be filtered.
Private Sub GroupDeadBand_Change()
'Set error handling for OPC Function
On Error GoTo ShowOPCGroupDeadBandError
' If the group has been added and exist then change its dead band
If Not ConnectedGroup Is Nothing Then
ConnectedGroup.DeadBand = Val(GroupDeadBand.Text)
End If
GoTo SkipGroupDeadBandError
ShowOPCGroupDeadBandError:
Call DisplayOPC_COM_ErrorValue("Group Dead Band", Err.Number)
SkipGroupDeadBandError:
End Sub
' This sub allows the group's update rate to be changed on the fly. The
' '.UpdateRate' property allows you to control how often data from this
' group will be returned to your application in the 'DataChange' event.
' The '.UpdateRate' property can be used to control and improve the overall
' performance of you application. In this example you can see that the update
' rate is set for maximum update speed. In a demo that's OK. In your real
' world application, forcing the OPC Server to gather all of the OPC items in
' a group at their fastest rate may not be ideal. In applications where you
' have data that needs to be acquired at different rates you can create
' multiple groups each with its own update rate. Using multiple groups would
' allow you to gather time critical data in GroupA with an update rate
' of 200 millliseconds, and gather low priority data from GroupB with an
' update rate of 7000 milliseconds. The lowest value for the '.UpdateRate'
' is 0 which tells the OPC Server go as fast as possible. The maximium is
' 2147483647 milliseconds which is about 596 hours.
Private Sub GroupUpdateRate_Change()
'Set error handling for OPC Function
On Error GoTo ShowOPCGroupUdateRateError
' If the group has been added and exist then change its update rate
If Not ConnectedGroup Is Nothing Then
ConnectedGroup.UpdateRate = Val(GroupUpdateRate.Text)
End If
GoTo SkipGroupUdateRateError
ShowOPCGroupUdateRateError:
Call DisplayOPC_COM_ErrorValue("Group Update Rate", Err.Number)
SkipGroupUdateRateError:
End Sub
' This sub handles adding an OPC item to a group. The group must be established first before
' any items can be added. Once you have a group added to the OPC Server you
' need to add item to the group. The OPCItems object provides the methods and
' properties need to add item to an estabished OPC group.
Private Sub OPCAddItems_Click()
'Set error handling for OPC Function
On Error GoTo ShowOPCItemAddError
' In this example we have at most 10 items that we will attempt to add
' to the group.
ItemCount = 10
'
' Load the request OPC Item names and build the ClientHandles list
Dim i As Integer
For i = 0 To 9
' Load the name of then item to be added to this group. You can add
' as many items as you want to the group in a single call by building these
' arrays as needed.
OPCItemIDs(i + 1) = OPCItemName(i).Text
' The client handles are given to the OPC Server for each item you intend
' to add to the group. The OPC Server will uses these client handles
' by returning them to you in the 'DataChange' event. You can use the
' client handles as a key to linking each valued returned from the Server
' back to some element in your application. In this example we are simply
' placing the Index number of each control that will be used to display
' data for the item. In your application the ClientHandle value you use
' can by whatever you need to best fit your program. You will see how
' these client handles are used in the 'DataChange' event handler.
ClientHandles(i + 1) = i
' Make the Items active start control Active, for the demo I want all itme to start active
' Your application may need to start the items as inactive.
OPCItemActiveState(i).Value = 1
Next i
' Establish a connection to the OPC item interface of the connected group
Set OPCItemCollection = ConnectedGroup.OPCItems
' Setting the '.DefaultIsActive' property forces all items we are about to
' add to the group to be added in an active state. If you want to add them
' all as inactive simply set this property false, you can always make the
' items active later as needed using each item's own active state property.
' One key distinction to note, the active state of an item is independent
' from the group active state. If a group is active but the item is
' inactive no data will be received for the item. Also changing the
' state of the group will not change the state of an item.
OPCItemCollection.DefaultIsActive = True
' Atempt to add the items, some may fail so the ItemServerErrors will need
' to be check on completion of the call. We are adding all item using the
' default data type of VT_EMPTY and letting the server pick the appropriate
' data type. The ItemServerHandles is an array that the OPC Server will
' return to your application. This array like your own ClientHandles array
' is used by the server to allow you to reference individual items in an OPC
' group. When you need to perform an action on a single OPC item you will
' need to use the ItemServerHandles for that item. With this said you need to
' maintain the ItemServerHandles array for use throughout your application.
' Use of the ItemServerHandles will be demonstrated in other subroutines in
' this example program.
OPCItemCollection.AddItems ItemCount, OPCItemIDs, ClientHandles, ItemServerHandles, ItemServerErrors
' This next step checks the error return on each item we attempted to
' register. If an item is in error it's associated controls will be
' disabled. If all items are in error then the Add Item button will
' remain active.
Dim AnItemIsGood As Boolean
AnItemIsGood = False
For i = 0 To 9
If ItemServerErrors(i + 1) = 0 Then ' If the item was added successfully then allow it to be used.
OPCItemValueToWrite(i).Enabled = True
OPCItemWriteButton(i).Enabled = True
OPCItemActiveState(i).Enabled = True
OPCItemSyncReadButton(i).Enabled = True
AnItemIsGood = True
Else
ItemServerHandles(i + 1) = 0 ' If the handle was bad mark it as empty
OPCItemValueToWrite(i).Enabled = False
OPCItemWriteButton(i).Enabled = False
OPCItemActiveState(i).Enabled = False
OPCItemSyncReadButton(i).Enabled = False
End If
Next i
' Disable the Add OPC item button if any item in the list was good
If AnItemIsGood Then
OPCAddItems.Enabled = False
For i = 0 To 9
OPCItemName(i).Enabled = False ' Disable the Item Name cotnrols while now that they have been added to the group.
Next i
RemoveOPCGroup.Enabled = False ' If an item has been don't allow the group to be removed until the item is removed
OPCRemoveItems.Enabled = True
Else
' The OPC Server did not accept any of the items we attempted to enter, let the user know to try again.
Dim Response
Response = MsgBox("The OPC Server has not accepted any of the item you have enter, check your item names and try again.", vbOKOnly, "OPC Add Item")
End If
GoTo SkipOPCItemAddError
ShowOPCItemAddError:
Call DisplayOPC_COM_ErrorValue("OPC Add Items", Err.Number)
SkipOPCItemAddError:
End Sub
' This sub handles removing OPC items from a group. Like the 'AddItems' method
' of the OPCItems object, the 'Remove' method allow us to remove item from
' an OPC group. In this example we are removing all item from the group.
' In your application you may find it necessary to remove some items from
' a group while ading others. Normally the best practice however it to add
' all the item you wish to use to the group and then control their active
' states indivudually. You can control the active state of individual items
' in a group as shown in the 'OPCItemActiveState_Click' subroutine of this
' module. With that said if you intend to remove the group you
' should first remove all its items. The 'Remove' method uses the
' ItemServerHandles we received from the 'AddItems' method to properly remove
' only the items you wish. This is an example of how ItemServerHandles are
' used by your application and the OPC Server. As stated above, you can
' design your application to add and remove items as needed but that's not
' necessarily the most effiecent operation for the OPC Server.
Private Sub OPCRemoveItems_Click()
If Not OPCItemCollection Is Nothing Then
'Set error handling for OPC Function
On Error GoTo ShowOPCRemoveItemError
ItemCount = 1
' Provide an array to contain the ItemServerHandles of the item
' we intend to remove
Dim RemoveItemServerHandles(10) As Long
' Array for potential error returns. This example doesn't
' check them but yours should ultimately.
Dim RemoveItemServerErrors() As Long
' Get the Servers handle for the desired items. The server handles
' were returned in add item subroutine. In this case we need to get
' only the handles for item that are valid.
Dim i As Integer
For i = 1 To 10
' In this example if the ItemServerHandle is non zero it is valid
If ItemServerHandles(i) <> 0 Then
RemoveItemServerHandles(ItemCount) = ItemServerHandles(i)
ItemCount = ItemCount + 1
End If
Next i
' Item count is 1 greater than it needs to be at this point
ItemCount = ItemCount - 1
' Invoke the Remove Item operation. Remember this call will
' wait until completion
OPCItemCollection.Remove ItemCount, RemoveItemServerHandles, RemoveItemServerErrors
' Clear the ItemServerHandles and turn off the controls for interacting
' with the OPC items on the form.
For i = 0 To 9
ItemServerHandles(i + 1) = 0 'Mark the handle as empty
OPCItemValueToWrite(i).Enabled = False
OPCItemWriteButton(i).Enabled = False
OPCItemActiveState(i).Enabled = False
OPCItemSyncReadButton(i).Enabled = False
Next i
' Enable the Add OPC item button and Remove Group button now that the
' items are released
OPCAddItems.Enabled = True
RemoveOPCGroup.Enabled = True
OPCRemoveItems.Enabled = False
' Enable the OPC Item name controls to allow a new set of items
' to be entered
For i = 0 To 9
OPCItemName(i).Enabled = True
Next i
' Release the resources by the item collection interface
Set OPCItemCollection = Nothing
End If
GoTo SkipOPCRemoveItemError
ShowOPCRemoveItemError:
Call DisplayOPC_COM_ErrorValue("OPC Remove Items", Err.Number)
SkipOPCRemoveItemError:
End Sub
' This sub handles writing a single value to the server using the
' 'SyncWrite' write method. The 'SyncWrite' method provides a
' quick(programming wise) means to send a value to an OPC Server. The item
' you intend to write must already be part of an OPC group you have added
' and you must have the ItemServerHandle for the item. This is another example
' of how the ItemServerHandle is used and why it is important to properly
' store and track these handles. The 'SyncWrite' method while quick and easy
' will wait for the OPC Server to complete the operation. Once you invoke
' the 'SyncWrite' method it could take a moment for the OPC Server to return
' control to your application. For this example that's OK. If your application
' can't tolerate a pause you can use the 'AsyncWrite' and its associated
' 'AsyncWriteComplete' call back event instead. In this sub we are only
' writing one value at a time. The 'SyncWrite' mehtod can take a list of
' writes to be performed allow you to write entire recipes to the server
' in one shot. If you are going to write more than one item, the
' ItemServerHandles for each item must be from the same OPC Group.
Private Sub OPCItemWriteButton_Click(Index As Integer)
'Set error handling for OPC Function
On Error GoTo ShowOPCSyncWriteError
' Write only 1 item
ItemCount = 1
' Create some local scope variables to hold the value to be sent.
' These arrays could just as easily contain all of the item we have added.
Dim SyncItemValues(1) As Variant
Dim SyncItemServerHandles(1) As Long
Dim SyncItemServerErrors() As Long
' Get the Servers handle for the desired item. The server handles
' were returned in add item subroutine.
SyncItemServerHandles(1) = ItemServerHandles(Index + 1)
' Load the value to be written
SyncItemValues(1) = Val(OPCItemValueToWrite(Index).Text)
' Invoke the SyncWrite operation. Remember this call will wait until completion
ConnectedGroup.SyncWrite ItemCount, SyncItemServerHandles, S
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -