发表于:2003-05-19 10:58:00
4楼
我一般很少用VB写程序,大部分用C++写,若方便你可以工程发给我,我帮你看看。下面我将给出利用自动化包裹器来实现OPC Client 的步骤,研究一下,希望有所帮助。
OPC客户端的开发
OPC采用的是OLE/COM/DCOM技术,因此应用软件可以使用Visual C++、Visual Basic、Delphi、PowerBuilder等市场上流行的语言进行开发。下面以VB为例说明编写客户机程序的步骤:
1.安装动态连接库OPCDAAuto.dll,通过“引用”选择OPC Automation 2.0
2.在公共窗口中声明相关变量
Option Explicit
Option Base 1
Dim WithEvents myserver As OPCServer
Dim WithEvents mygroups As OPCGroups
Dim WithEvents mygroup As OPCGroup
Dim myitems As OPCItems
Dim myitem As OPCItem
Dim ItemNum As Long
Dim ItemError() As Long
Dim ClientHandle(17) As Long
Dim ServerHandle() As Long
Dim ItemName(17) As String
Dim Itemvalue(17) As Variant
3.将VB工程与服务器连接
Dim i As Long Dim serversname As String
Dim servers As Variant
ItemNum = 17
serversname=“Insight.OPCServerDA.1” ’SIEMENS服务器名称 Set myserver = New OPCServer servers = myserver.GetOPCServers
For i = LBound(servers) To UBound(servers)
Debug.Print server(i) ’给出已安装的服务器名称
Next I
myserver.Connect (serversname) ’与服务器连接
4.加入OPCGroup 对象
Set mygroups = myserver.OPCGroups mygroups.DefaultGroupDeadband = 0
Set mygroup = mygroups.Add(“DataGroup1”) ’加入OPC Group 对象“DataGroup1”
mygroup.UpdateRate = 1000
mygroup.IsActive = True
mygroup.IsSubscribed = True
5.加入OPCItem 对象并检测服务器返回的ServerHandle数值
Set myitems = mygroup.OPCItems
For i = 0 To 16 ClientHandle(i + 1) = i
ItemName(i + 1) = “kelling” & (i + 1)
Debug.Print ItemName(i + 1)
Next i myitems.DefaultIsActive = True
myitems.AddItems ItemNum, ItemName, ClientHandle, ServerHandle, ItemError ’创建新的OPCItem并将其加入PCGroup 中
For i = 1 To 17
If ServerHandle(i) = 0 Then MsgBox (“No” + Str(i) + “Add Error”)
End If
Debug.Print ServerHandle(i)
Next i mygroup.AsyncWrite 17, ServerHandle, Itemvalue, ItemError, 10, 12 ’在OPCGroup中异步写入OPCItems
For i = 1 To 17
Debug.Print ItemError(i)
Next I
6.断开客户机与服务器的连接
Private Sub Command1_Click()
Dim x As Integer mygroups.RemoveAll
myserver.Disconnect
For x = 1 To 17
Debug.Print ServerHandle(x)
Next x
End Sub