📄 main.frm
字号:
VERSION 5.00
Begin VB.Form main
Caption = "OPC-Client"
ClientHeight = 4965
ClientLeft = 60
ClientTop = 345
ClientWidth = 7200
LinkTopic = "Form1"
ScaleHeight = 4965
ScaleWidth = 7200
StartUpPosition = 3 'Windows-Standard
Begin VB.CommandButton Stop
Caption = "Stop"
Height = 375
Left = 1800
TabIndex = 27
Top = 3960
Width = 975
End
Begin VB.CommandButton Start
Caption = "Start"
Height = 375
Left = 480
TabIndex = 26
Top = 3960
Width = 975
End
Begin VB.TextBox State
Height = 375
Left = 5280
TabIndex = 24
Text = "0"
Top = 3480
Width = 1455
End
Begin VB.TextBox Status
Height = 375
Left = 5280
TabIndex = 22
Text = "off"
Top = 2040
Width = 1455
End
Begin VB.TextBox ICount
Height = 375
Left = 5280
TabIndex = 20
Top = 3000
Width = 1455
End
Begin VB.TextBox UTime
Height = 375
Left = 5280
TabIndex = 18
Top = 2520
Width = 1455
End
Begin VB.TextBox Server
Height = 375
Left = 5280
TabIndex = 16
Text = "OPC.SimaticHMI.PTPro"
Top = 360
Width = 1455
End
Begin VB.TextBox Group
Height = 375
Left = 5280
TabIndex = 14
Text = "MyGroup"
Top = 1320
Width = 1455
End
Begin VB.TextBox Node
Height = 375
Left = 5280
TabIndex = 12
Text = "192.168.0.80"
Top = 840
Width = 1455
End
Begin VB.TextBox var6
Height = 375
Left = 1080
TabIndex = 5
Top = 2760
Width = 1215
End
Begin VB.TextBox var5
Height = 375
Left = 1080
TabIndex = 4
Top = 2280
Width = 1215
End
Begin VB.TextBox var4
BeginProperty DataFormat
Type = 1
Format = "0"
HaveTrueFalseNull= 0
FirstDayOfWeek = 0
FirstWeekOfYear = 0
LCID = 1031
SubFormatType = 1
EndProperty
Height = 375
Left = 1080
TabIndex = 3
Text = "0"
Top = 1800
Width = 1215
End
Begin VB.TextBox var3
BeginProperty DataFormat
Type = 1
Format = "0"
HaveTrueFalseNull= 0
FirstDayOfWeek = 0
FirstWeekOfYear = 0
LCID = 1031
SubFormatType = 1
EndProperty
Height = 375
Left = 1080
TabIndex = 2
Text = "0"
Top = 1320
Width = 1215
End
Begin VB.TextBox var2
BeginProperty DataFormat
Type = 1
Format = "0"
HaveTrueFalseNull= 0
FirstDayOfWeek = 0
FirstWeekOfYear = 0
LCID = 1031
SubFormatType = 1
EndProperty
Height = 375
Left = 1080
TabIndex = 1
Text = "0"
Top = 840
Width = 1215
End
Begin VB.TextBox var1
BeginProperty DataFormat
Type = 1
Format = "0"
HaveTrueFalseNull= 0
FirstDayOfWeek = 0
FirstWeekOfYear = 0
LCID = 1031
SubFormatType = 1
EndProperty
Height = 375
Left = 1080
TabIndex = 0
Text = "0"
Top = 360
Width = 1215
End
Begin VB.Label lblState
Caption = "Server State"
Height = 255
Left = 4200
TabIndex = 25
Top = 3600
Width = 975
End
Begin VB.Label lblStatus
Caption = "Status"
Height = 255
Left = 4200
TabIndex = 23
Top = 2160
Width = 975
End
Begin VB.Label lblCount
Caption = "Item Count"
Height = 255
Left = 4200
TabIndex = 21
Top = 3120
Width = 975
End
Begin VB.Label lblTime
Caption = "Update Time"
Height = 255
Left = 4200
TabIndex = 19
Top = 2640
Width = 975
End
Begin VB.Label lblServer
Caption = "Server Name"
Height = 255
Left = 4200
TabIndex = 17
Top = 480
Width = 975
End
Begin VB.Label lblGroup
Caption = "Group Name"
Height = 255
Left = 4200
TabIndex = 15
Top = 1440
Width = 975
End
Begin VB.Label lblNode
Caption = "Node Name"
Height = 255
Left = 4200
TabIndex = 13
Top = 960
Width = 975
End
Begin VB.Label lbl_6
Caption = "VAR 6"
Height = 255
Left = 120
TabIndex = 11
Top = 2880
Width = 735
End
Begin VB.Label lbl_5
Caption = "VAR 5"
Height = 255
Left = 120
TabIndex = 10
Top = 2400
Width = 735
End
Begin VB.Label lbl_4
Caption = "VAR 4"
Height = 255
Left = 120
TabIndex = 9
Top = 1920
Width = 735
End
Begin VB.Label lbl_3
Caption = "VAR 3"
Height = 255
Left = 120
TabIndex = 8
Top = 1440
Width = 735
End
Begin VB.Label lbl_2
Caption = "VAR 2"
Height = 255
Left = 120
TabIndex = 7
Top = 960
Width = 735
End
Begin VB.Label lbl_1
Caption = "VAR 1"
Height = 255
Left = 120
TabIndex = 6
Top = 480
Width = 735
End
End
Attribute VB_Name = "main"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit 'Variablennamen m黶sen eindeutig sein
Option Base 1 'Indizes beginnen bei 1 (nicht 0)
Dim WithEvents MyOPCServer As OPCServer 'OPCServer-Objekt wird definiert
Attribute MyOPCServer.VB_VarHelpID = -1
Dim MyOPCGroupColl As OPCGroups 'OPCGroups-Objekt wird definiert
Dim WithEvents MyOPCGroup As OPCGroup 'OPCGroup-Objekt wird definiert
Attribute MyOPCGroup.VB_VarHelpID = -1
Dim MyOPCItemColl As OPCItems 'OPCItems-Objekt wird definiert
Dim MyOPCItem As OPCItem 'OPCItem-Objekt wird definiert
Dim ClientHandles(1 To 6) As Long
Dim ServerHandles() As Long
Dim Values(1 To 6) As Variant
Dim Errors() As Long
Dim ItemIDs(1 To 6) As String
Dim ServerName As String
Dim NodeName As String
Dim GroupName As String
Private Sub StartClient()
'Connect to Server, create Groups, items etc.
On Error GoTo ErrorHandler
ClientHandles(1) = 1
ClientHandles(2) = 2
ClientHandles(3) = 3
ClientHandles(4) = 4
ClientHandles(5) = 5
ClientHandles(6) = 6
ServerName = Server.Text
GroupName = Group.Text
NodeName = Node.Text
ItemIDs(1) = "VAR_1"
ItemIDs(2) = "VAR_2"
ItemIDs(3) = "VAR_3"
ItemIDs(4) = "VAR_4"
ItemIDs(5) = "VAR_5"
ItemIDs(6) = "VAR_6"
'----------- Get an instance of the OPC server
Set MyOPCServer = New OPCServer
MyOPCServer.Connect ServerName, NodeName
Set MyOPCGroupColl = MyOPCServer.OPCGroups
'----------- Set the default active state for adding groups
MyOPCGroupColl.DefaultGroupIsActive = True
'----------- Add our group to the Collection
Set MyOPCGroup = MyOPCGroupColl.Add(GroupName)
Set MyOPCItemColl = MyOPCGroup.OPCItems
'----------- Add one item, ServerHandles are returned
MyOPCItemColl.AddItems 6, ItemIDs, ClientHandles, ServerHandles, Errors
'----------- A group that is subscribed receives asynchronous notifications
MyOPCGroup.IsSubscribed = True
Status.Text = "Client on"
UTime.Text = CStr(MyOPCServer.LastUpdateTime)
ICount.Text = MyOPCItemColl.Count
State.Text = MyOPCServer.ServerState
Exit Sub
ErrorHandler:
MsgBox "Error: " & Err.Description, vbCritical, "ERROR"
End Sub
Private Sub Form_Unload(Cancel As Integer)
StopClient
End Sub
Private Sub Start_Click()
StartClient
End Sub
Private Sub Stop_Click()
StopClient
End Sub
Private Sub StopClient()
'----------- Release the Group and Server objects
If (State.Text <> "1") Then Exit Sub
MyOPCGroupColl.RemoveAll
'----------- Disconnect from the server and clean up
MyOPCServer.Disconnect
Set MyOPCItemColl = Nothing
Set MyOPCGroup = Nothing
Set MyOPCGroupColl = Nothing
Set MyOPCServer = Nothing
Status.Text = "Client off"
State.Text = 0
End Sub
Private Sub var1_Change()
If Not IsNumeric(var1.Text) Then var1.Text = 0
VarChange
End Sub
Private Sub var2_Change()
If Not IsNumeric(var2.Text) Then var2.Text = 0
VarChange
End Sub
Private Sub var3_Change()
If Not IsNumeric(var3.Text) Then var3.Text = 0
VarChange
End Sub
Private Sub var4_Change()
If Not IsNumeric(var4.Text) Then var4.Text = 0
VarChange
End Sub
Private Sub var5_Change()
VarChange
End Sub
Private Sub var6_Change()
VarChange
End Sub
Private Sub VarChange()
'------------ Ereignis wird ausgeloest, wenn sich Daten in Arbeitsblatt 鋘dern
If State.Text = "0" Then Exit Sub 'Abbruch falls nicht verbunden
Values(1) = var1.Text
Values(2) = var2.Text
Values(3) = var3.Text
Values(4) = var4.Text
Values(5) = var5.Text
Values(6) = var6.Text
MyOPCGroup.SyncWrite 6, ServerHandles, Values, Errors 'Werte im Synchron-Modus auf Server schreiben
End Sub
Private Sub MyOPCGroup_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)
'------------ Ereignis wird ausgeloest, wenn sich Daten am Server 鋘dern
'es wird jeweils nur ein Item abgefragt. es wird angenommen, da
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -