📄 frmmain.frm
字号:
VERSION 5.00
Begin VB.Form Main
Caption = "Form1"
ClientHeight = 4590
ClientLeft = 60
ClientTop = 435
ClientWidth = 8355
LinkTopic = "Form1"
ScaleHeight = 4590
ScaleWidth = 8355
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox txtGroupCount
Height = 375
Left = 6960
TabIndex = 27
Top = 600
Width = 1095
End
Begin VB.CommandButton Command2
Caption = "Command2"
Height = 375
Left = 6480
TabIndex = 26
Top = 120
Width = 1575
End
Begin VB.TextBox Text1
Height = 495
Left = 6240
TabIndex = 25
Text = "Text1"
Top = 3840
Width = 1455
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Left = 6240
TabIndex = 24
Top = 1320
Width = 1455
End
Begin VB.TextBox txtVarName
Height = 375
Index = 4
Left = 480
TabIndex = 23
Top = 3360
Width = 1455
End
Begin VB.TextBox txtVarValue
Height = 375
Index = 4
Left = 2400
TabIndex = 22
Top = 3360
Width = 1455
End
Begin VB.TextBox txtVarNew
Height = 375
Index = 4
Left = 4440
TabIndex = 21
Top = 3360
Width = 1455
End
Begin VB.TextBox txtVarNew
Height = 375
Index = 3
Left = 4440
TabIndex = 20
Top = 2640
Width = 1455
End
Begin VB.TextBox txtVarNew
Height = 375
Index = 2
Left = 4440
TabIndex = 19
Top = 2040
Width = 1455
End
Begin VB.TextBox txtVarNew
Height = 375
Index = 1
Left = 4440
TabIndex = 18
Top = 1440
Width = 1455
End
Begin VB.TextBox txtVarValue
Height = 375
Index = 3
Left = 2400
TabIndex = 17
Top = 2640
Width = 1455
End
Begin VB.TextBox txtVarValue
Height = 375
Index = 2
Left = 2400
TabIndex = 16
Top = 2040
Width = 1455
End
Begin VB.TextBox txtVarValue
Height = 375
Index = 1
Left = 2400
TabIndex = 15
Top = 1440
Width = 1455
End
Begin VB.TextBox txtVarName
Height = 375
Index = 3
Left = 480
TabIndex = 14
Top = 2760
Width = 1455
End
Begin VB.TextBox txtVarName
Height = 375
Index = 2
Left = 480
TabIndex = 13
Top = 2160
Width = 1455
End
Begin VB.TextBox txtVarName
Height = 375
Index = 1
Left = 480
TabIndex = 12
Top = 1440
Width = 1455
End
Begin VB.CommandButton cmdClose
Caption = "关闭"
Height = 375
Left = 4440
TabIndex = 11
Top = 4080
Width = 1455
End
Begin VB.CommandButton cmdShut
Caption = "断开"
Height = 375
Left = 2400
TabIndex = 10
Top = 4080
Width = 1455
End
Begin VB.CommandButton cmdLink
Caption = "连接"
Height = 375
Left = 480
TabIndex = 9
Top = 4080
Width = 1455
End
Begin VB.TextBox txtVarNew
Height = 375
Index = 0
Left = 6240
TabIndex = 7
Top = 3360
Width = 1455
End
Begin VB.TextBox txtVarValue
Height = 375
Index = 0
Left = 6240
TabIndex = 5
Top = 2640
Width = 1455
End
Begin VB.TextBox txtVarName
Height = 375
Index = 0
Left = 6240
TabIndex = 3
Top = 1920
Width = 1455
End
Begin VB.CheckBox chkLink
Caption = "进行连接"
Height = 495
Left = 3840
TabIndex = 2
Top = 120
Width = 1935
End
Begin VB.TextBox txtCName
Height = 375
Left = 1440
TabIndex = 0
Top = 120
Width = 1695
End
Begin VB.Label Label4
Caption = "变量组数"
Height = 375
Left = 5880
TabIndex = 28
Top = 600
Width = 735
End
Begin VB.Label Label3
Caption = "WINCC变量改变"
Height = 375
Left = 4440
TabIndex = 8
Top = 960
Width = 1575
End
Begin VB.Label Label2
Caption = "WINCC变量值"
Height = 375
Left = 2400
TabIndex = 6
Top = 960
Width = 1575
End
Begin VB.Label lblWinccVarName
Caption = "WINCC变量名称"
Height = 375
Left = 480
TabIndex = 4
Top = 960
Width = 1575
End
Begin VB.Label Label1
Caption = "计算机名:"
Height = 495
Left = 240
TabIndex = 1
Top = 240
Width = 1095
End
End
Attribute VB_Name = "Main"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Option Base 1 '这数组必须由1开始,不能由0开始
Const ServerName = "OPCServer.WinCC" 'OPC的类型
Dim NodeName As String '结点名,即计算机名
Dim WithEvents MyOPCServer As OPCServer 'OPC服务
Attribute MyOPCServer.VB_VarHelpID = -1
Dim WithEvents MyOPCGroup As OPCGroup 'OPC组
Attribute MyOPCGroup.VB_VarHelpID = -1
Dim MyOPCGroupColl As OPCGroups
Dim MyOPCItemColl As OPCItems 'OPC标签组
Dim MyOPCItems As OPCItems
Dim MyOPCItem As OPCItem
Dim ClientHandles(1) As Long '句柄
Dim ServerHandles() As Long
Dim Errors() As Long
Dim ItemIDs(4) As String '记录OPC的标签
Dim ItemIDsValue(4) As Variant '存放OPC的值
Dim GroupName As Variant
Private Declare Function Min Lib "C:\MaxMin.DLL" (ByVal x As Long, ByVal y As Long) As Long
Private Sub cmdClose_Click()
End
End Sub
' 在定义所有变量后,我们就要进行OPC连接了,要进行OPC连接之前,先要配置要访问的OPC标签名(即WinCC内部变量名),我们ItemIDs中加入相应的标签名,注意:这数组必须由1开始,不能由0开始。
' 配置好标签后就要进行OPC连接了。如下面子程序:① ClientHandles1先配置名柄索引,这将在读取OPC标签的值时可要用到。②生成OPC对象,③ 进行OPC标签连接此,OPC连接就成功了,我们可以对OPC进行读与写的操作了。
Private Sub cmdLink_Click()
Dim ii As Integer
Dim ClientHandles1(4) As Long
For ii = 1 To 4
ClientHandles1(ii) = ii
ItemIDs(ii) = txtVarName(ii).Text '指明WinCC内部变量名称
Next ii
On Error GoTo ErrorHandler
GroupName = "bot"
NodeName = txtCName.Text '结点名,即计算机名
Set MyOPCServer = New OPCServer
MyOPCServer.Connect ServerName, NodeName
Set MyOPCGroupColl = MyOPCServer.OPCGroups
MyOPCGroupColl.DefaultGroupIsActive = True
Set MyOPCGroup = MyOPCGroupColl.Add(GroupName)
Set MyOPCItemColl = MyOPCGroup.OPCItems
For ii = 1 To 4
ClientHandles1(ii) = ii
ItemIDs(ii) = txtVarName(ii).Text
MyOPCItemColl.AddItems 4, ItemIDs, ClientHandles1, ServerHandles, Errors '初始化OCP连接
Next ii
'MyOPCItemColl.AddItems 4, ItemIDs, ClientHandles1, ServerHandles, Errors '初始化OCP连接
MyOPCGroup.IsSubscribed = True
chkLink.Value = 1 '连接成功标志
txtGroupCount.Text = MyOPCServer.OPCGroups.Count
Exit Sub
ErrorHandler:
MsgBox "Error: " & Err.Description, vbCritical, "ERROR" '连接发生错误
End Sub
Private Sub Command1_Click()
Text1.Text = Str(Min(Val(txtVarName(0).Text), Val(txtVarValue(0).Text)))
End Sub
Private Sub Command2_Click()
About.Show
End Sub
Private Sub Form_Load()
txtCName.Text = "IBMT42"
txtVarName(1).Text = "botnum2"
txtVarName(2).Text = "botnum3"
txtVarName(3).Text = "botnum4"
txtVarName(4).Text = "botnum5"
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)
Dim ii As Long
For ii = 1 To 4
ItemIDsValue(ClientHandles(ii)) = ItemValues(ii) '对改变的值读入本数组
txtVarValue(ClientHandles(ii)).Text = ItemIDsValue(ClientHandles(ii))
Next ii
End Sub
'写入
Private Sub txtVarNew_Change(Index As Integer)
Dim valuess(4) As Variant
Dim ii As Long
For ii = 1 To 4
valuess(ii) = txtVarNew(ii).Text
MyOPCGroup.SyncWrite 4, ServerHandles, valuess, Errors
Next
End Sub
' 对OPC的写可以有同步与异步之分,对于大量的数据传输,异步是更佳的选择,但对少量的数据传输,同步表现得更好。
'4 ?OPC连接断开?
' OPC客户端连接后要占用服务器资源,所以如果不需要使用OPC时,必须进行OPC连接断开。断开的程序相当简单,释放资源即可。如下:
Private Sub cmdShut_Click()
On Error Resume Next
MyOPCGroupColl.RemoveAll -----------释放组和服务器对象
MyOPCServer.Disconnect
'----------- 与服务器断开连接并且清除
Set MyOPCItemColl = Nothing
Set MyOPCGroup = Nothing
Set MyOPCGroupColl = Nothing
Set MyOPCServer = Nothing
chkLink.Value = 0 '连接成功标志
End Sub
'至此,用VB 6.0开发WinCC的OPC客户机开发完毕。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -