?? frmabout.frm
字號:
VERSION 5.00
Begin VB.Form About
Caption = "Form2"
ClientHeight = 3960
ClientLeft = 60
ClientTop = 435
ClientWidth = 6150
LinkTopic = "Form2"
ScaleHeight = 3960
ScaleWidth = 6150
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox txtNew
Height = 735
Left = 5040
TabIndex = 4
Top = 480
Width = 975
End
Begin VB.CommandButton cmdOK
Caption = "Command1"
Height = 615
Left = 2880
TabIndex = 3
Top = 1920
Width = 2055
End
Begin VB.TextBox txtComputer
Height = 615
Left = 600
TabIndex = 2
Top = 1920
Width = 1815
End
Begin VB.TextBox txtValue
Height = 615
Left = 2880
TabIndex = 1
Top = 480
Width = 1935
End
Begin VB.TextBox txtName
Height = 495
Left = 600
TabIndex = 0
Top = 480
Width = 1935
End
End
Attribute VB_Name = "About"
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(1) As String '記錄OPC的標簽
Dim ItemIDsValue(1) 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
' 在定義所有變量后,我們就要進行OPC連接了,要進行OPC連接之前,先要配置要訪問的OPC標簽名(即WinCC內部變量名),我們ItemIDs中加入相應的標簽名,注意:這數組必須由1開始,不能由0開始。
' 配置好標簽后就要進行OPC連接了。如下面子程序:① ClientHandles1先配置名柄索引,這將在讀取OPC標簽的值時可要用到。②生成OPC對象,③ 進行OPC標簽連接此,OPC連接就成功了,我們可以對OPC進行讀與寫的操作了。
Private Sub cmdOK_Click()
Dim ii As Integer
Dim ClientHandles1(1) As Long
For ii = 1 To 1
ClientHandles1(ii) = ii
ItemIDs(ii) = txtName.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 1
ClientHandles1(ii) = ii
ItemIDs(ii) = txtName.Text
MyOPCItemColl.AddItems 1, ItemIDs, ClientHandles1, ServerHandles, Errors '初始化OCP連接
Next ii
'MyOPCItemColl.AddItems 4, ItemIDs, ClientHandles1, ServerHandles, Errors '初始化OCP連接
MyOPCGroup.IsSubscribed = True
'chkLink.Value = 1 '連接成功標志
Exit Sub
ErrorHandler:
MsgBox "Error: " & Err.Description, vbCritical, "ERROR" '連接發生錯誤
End Sub
Private Sub Form_Load()
txtComputer.Text = "IBMT42"
txtName.Text = "botnum2"
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 1
ItemIDsValue(ClientHandles(ii)) = ItemValues(ii) '對改變的值讀入本數組
txtValue.Text = ItemIDsValue(ClientHandles(ii))
Next ii
End Sub
'寫入
Private Sub txtNew_Change()
Dim valuess(1) As Variant
Dim ii As Long
For ii = 1 To 1
valuess(ii) = txtNew.Text
MyOPCGroup.SyncWrite 1, 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 + -