📄 mainfrm.frm
字号:
Top = 480
Width = 1335
End
Begin VB.Label Label4
Caption = "Enter OPC Item Names"
Height = 255
Left = 120
TabIndex = 29
Top = 480
Width = 1815
End
End
End
Attribute VB_Name = "OPCInterface"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' This example attempts to demonstrate the steps required to connect
' with and access data from and OPC server. Using the Data Access 2.0
' automation interface installed when you installed KEPServer, this
' example will give you a general understanding of this powerful interface
' and the steps required to use it. If you have used KEPServer in your
' Visual Basic applications as a DDE server you will be impressed with the
' performance gains that OPC connectivity provides. The steps required to
' use an OPC server in your VB application is a little more involved than
' simple TextBox DDE links but the benefits far outweigh the extra effort.
' Hopefully this example will make even the extra effort seem trivial.
'
' In this example and its operation you will see that the steps of connecting
' with, adding a group, and adding items have been purposely imposed by the
' enabling and disabling of the controls on this form. The goal of this hand
' holding is to insure that your application connects with and uses OPC data in
' the proper sequence. That sequence being 1) Connect with the Server.
' 2) Add group(s) 3) Add items to group(s). The same steps should be done in
' reverse order when disconnecting from the server. As always VB gives you a
' great deal of freedom, your resulting application may need to take many steps
' to achieve its goal but you should attempt to follow these steps if possible.
'
' This exmaple project has already been configured to reference the
' 'OPC Automation 2.0' object. When creating your own OPC applications from
' scratch you must first add the 'OPC Automation 2.0' object to the reference
' object list. This can be done from the VB menu Project|References. Once
' the References Dialog is displayed, scroll down to the 'OPC Automation 2.0'
' object and select it, then click the OK button. You'll know that the
' 'OPC Automation 2.0' object is being referenced when VB displays smart
' object properties for the OPC related objects as you use them.
Option Explicit
Option Base 1
' Server and group related data
' The OPCServer objects must be declared here due to the use of WithEvents
Dim WithEvents AnOPCServer As OPCServer
Attribute AnOPCServer.VB_VarHelpID = -1
Dim WithEvents ConnectedOPCServer As OPCServer
Attribute ConnectedOPCServer.VB_VarHelpID = -1
Dim ConnectedServerGroup As OPCGroups
Dim WithEvents ConnectedGroup As OPCGroup
Attribute ConnectedGroup.VB_VarHelpID = -1
' OPC Item related data
Dim OPCItemCollection As OPCItems
Dim OneOPCItem As OPCItem
Dim ItemCount As Long
Dim OPCItemIDs(10) As String
Dim ItemServerHandles() As Long
Dim ItemServerErrors() As Long
Dim ClientHandles(10) As Long
' General startup initialization
Private Sub Form_Load()
AvailableOPCServerList.AddItem "Click on 'List OPC Servers' to start"
End Sub
' Make sure things get shut down properly upon closing application
Private Sub Form_Terminate()
Call ExitExample_Click
End Sub
' This sub handles gathering a list of available OPC Servers and displays them
' The OPCServer Object provides a method called 'GetOPCServers' that will allow
' you to get a list of the OPC Servers that are installed on your machine. The
' list is retured as a string array.
Private Sub ListOPCServers_Click()
Dim AllOPCServers As Variant
Dim i As Integer
'Set error handling for OPC Function
On Error GoTo ShowOPCGetServersError
' Create a temporary OPCServer object and use it to get the list of
' available OPC Servers
Set AnOPCServer = New OPCServer
' Clear the list control used to display them
AvailableOPCServerList.Clear
AllOPCServers = AnOPCServer.GetOPCServers
' Load the list returned into the List box for user selection
For i = LBound(AllOPCServers) To UBound(AllOPCServers)
AvailableOPCServerList.AddItem AllOPCServers(i)
Next i
GoTo SkipOPCGetServersError
ShowOPCGetServersError:
Call DisplayOPC_COM_ErrorValue("Get OPC Server List", Err.Number)
SkipOPCGetServersError:
' Release the temporary OPCServer object now that we're done with it
Set AnOPCServer = Nothing
End Sub
' This sub loads the OPC Server name when selected from the list
' and places it in the OPCServerName object
Private Sub AvailableOPCServerList_Click()
' When a user selects a server from the list box its name is placed
' in the OPCServerName
OPCServerName = AvailableOPCServerList.List(AvailableOPCServerList.ListIndex)
End Sub
' This sub handles connecting with the selected OPC Server
' The OPCServer Object provides a method called 'Connect' that allows you
' to 'connect' with an OPC server. The 'Connect' method can take two arguments,
' a server name and a Node name. The Node name is optional and does not have to
' be used to connect to a local server. When the 'Connect' method is called you
' should see the OPC Server application start if it is not aleady running.
Private Sub OPCServerConnect_Click()
Dim ConnectedServerName As String
Dim ConnectedNodeName As Variant
' Test to see if the User has entered or selected an OPC server name yet if not post a message
If InStr(OPCServerName.Text, "Click") = 0 Then
'Set error handling for OPC Function
On Error GoTo ShowOPCConnectError
'
'Create a new OPC Server object
Set ConnectedOPCServer = New OPCServer
'Load the selected server name to start the interface
ConnectedServerName = OPCServerName.Text
'Load the node name of the connected server. The node name should be entered
'without the use of forward slashes \\.
ConnectedNodeName = OPCNodeName.Text
'Attempt to connect with the server
ConnectedOPCServer.Connect ConnectedServerName, ConnectedNodeName
' Throughout this example you will see a lot of code that simply enables
' and disables the various controls on the form. The purpose of these
' actions is to demonstrate and insure the proper sequence of events when
' making an OPC connection.
' If we successfully connect to a server allow the user to disconnect
DisconnectFromServer.Enabled = True
' Don't allow a reconnect until the user disconnects
OPCServerConnect.Enabled = False
AvailableOPCServerList.Enabled = False
OPCServerName.Enabled = False
' Enable the group controls now that we have a server connection
OPCGroupName.Enabled = True
GroupUpdateRate.Enabled = True
GroupDeadBand.Enabled = True
GroupActiveState.Enabled = True
AddOPCGroup.Enabled = True ' Remove group isn't enable until a group has been added
GoTo SkipOPCConnectError
ShowOPCConnectError:
DisconnectFromServer.Enabled = False
Set ConnectedOPCServer = Nothing
Call DisplayOPC_COM_ErrorValue("Connect", Err.Number)
SkipOPCConnectError:
Else
' A server name has not been selected yet post an error to the user
Dim Response
Response = MsgBox("You must first select an OPC Server, Click on the 'List OPC Servers' button and select a server", vbOKOnly, "OPC Server Connect")
End If
End Sub
' This sub handles disconnecting from the OPC Server. The OPCServer Object
' provides the method 'Disconnect'. Calling this on an active OPCSerer
' object will release the OPC Server interface with your application. When
' this occurs you should see the OPC server application shut down if it started
' automatically on the OPC connect. This step should not occur until the group
' and items have been removed
Private Sub DisconnectFromServer_Click()
' Test to see if the OPC Server connection is currently available
If Not ConnectedOPCServer Is Nothing Then
'Set error handling for OPC Function
On Error GoTo ShowOPCDisconnectError
'Disconnect from the server, This should only occur after the items and group
' have been removed
ConnectedOPCServer.Disconnect
' Release the old instance of the OPC Server object and allow the resources
' to be freed
Set ConnectedOPCServer = Nothing
' Allow a reconnect once the disconnect completes
OPCServerConnect.Enabled = True
AvailableOPCServerList.Enabled = True
OPCServerName.Enabled = True
' Don't alllow the Disconnect to be issued now that the connection is closed
DisconnectFromServer.Enabled = False
' Disable the group controls now that we no longer have a server connection
OPCGroupName.Enabled = False
GroupUpdateRate.Enabled = False
GroupDeadBand.Enabled = False
GroupActiveState.Enabled = False
AddOPCGroup.Enabled = False
End If
GoTo SkipDisconnectError
ShowOPCDisconnectError:
Call DisplayOPC_COM_ErrorValue("Disconnect", Err.Number)
SkipDisconnectError:
End Sub
' This sub handles adding the group to the OPC server and establishing the
' group interface. When adding a group you can preset some of the group
' parameters using the properties '.DefaultGroupIsActive'
' and '.DefaultGroupDeadband'. Set these before adding the group. Once the
' group has been successfully added you can change these same settings
' along with the group update rate on the fly using the properties on the
' resulting OPCGroup object.
Private Sub AddOPCGroup_Click()
'Set error handling for OPC Function
On Error GoTo ShowOPCGroupAddError
'Prepare to add a group to the current OPC Server
' Get the group interface from the server object
Set ConnectedServerGroup = ConnectedOPCServer.OPCGroups
' Set the desire active state for the group
ConnectedServerGroup.DefaultGroupIsActive = GroupActiveState.Value
'Set the desired percent deadband
ConnectedServerGroup.DefaultGroupDeadband = Val(GroupDeadBand.Text)
' Add the group and set its update rate
Set ConnectedGroup = ConnectedServerGroup.Add(OPCGroupName.Text)
' Set the update rate for the group
ConnectedGroup.UpdateRate = Val(GroupUpdateRate.Text)
' ****************************************************************
' Mark this group to receive asynchronous updates via the DataChange event.
' This setting is IMPORTANT. Without setting '.IsSubcribed' to True your
' VB application will not receive DataChange notifications. This will
' make it appear that you have not properly connected to the server.
ConnectedGroup.IsSubscribed = True
'*****************************************************************
' Now that a group has been added disable the Add group Button and enable
' the Remove group Button. This demo application adds only a single group
AddOPCGroup.Enabled = False
OPCGroupName.Enabled = False
RemoveOPCGroup.Enabled = True
' Enable the OPC item controls now that a group has been added
OPCAddItems.Enabled = True
Dim i As Integer
For i = 0 To 9
OPCItemName(i).Enabled = True
Next i
' Disable the Disconnect Server button since we now have a group that must be removed first
DisconnectFromServer.Enabled = False
GoTo SkipAddGroupError
ShowOPCGroupAddError:
Call DisplayOPC_COM_ErrorValue("Add Group", Err.Number)
SkipAddGroupError:
End Sub
' This sub handles removing a group from the OPC server, this must be done after
' items have been removed. The 'Remove' method allows a group to be removed
' by name from the OPC Server. If your application will maintains more than
' one group you will need to keep a list of the group names for use in the
' 'Remove' method. In this demo there is only one group. The name is maintained
' in the OPCGroupName TextBox but it can not be changed once the group is added.
Private Sub RemoveOPCGroup_Click()
' Test to see if the OPC Group object is currently available
If Not ConnectedServerGroup Is Nothing Then
'Set error handling for OPC Function
On Error GoTo ShowOPCGroupRemoveError
' Remove the group from the server
ConnectedServerGroup.Remove (OPCGroupName.Text)
' Release the group interface and allow the server to cleanup the resources used
Set ConnectedServerGroup = Nothing
Set ConnectedGroup = Nothing
' Enable the Add group Button and disable the Remove group Button
AddOPCGroup.Enabled = True
OPCGroupName.Enabled = True
RemoveOPCGroup.Enabled = False
' 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -