⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmabout.frm

📁 WINCC OPC VB开发通信方式 ,读取其中的变量和改变变量内容,根据变量名和分组名,服务器名字
💻 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 + -