📄 kepserverex_simple_vb_opc.frm
字号:
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 8520
TabIndex = 100
Top = 2280
Width = 1695
End
Begin VB.Label Label10
Caption = "ATCE Department."
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 0
TabIndex = 99
Top = 0
Width = 1695
End
Begin VB.Label Label3
Caption = "ATCE Dep."
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 8520
TabIndex = 96
Top = 2040
Width = 1455
End
Begin VB.Label Label2
Caption = "ABB Shaghai Co.,Ltd"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Index = 1
Left = 0
TabIndex = 95
Top = 0
Width = 1935
End
End
Attribute VB_Name = "SimpleOPCInterface"
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.
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.
'
'Special Note: When connect remotely to another PC running the serverEx make
'sure that you have properly configured DCOM on both PC's. You will find documentation
'explaining exactly how to do this on your installation CD or at the ware web site.
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -