智到功成
当前位置:首页 - 亚博国际APP下载 >

OPC Server示例教程:OPC客户端——VB.NET示例

2019-09-11来源:央广网

OPC Server是一套利用微软的COM/DCOM技术实现工业自动化资料获取的架构。OPC Server提供OPC接口,它将与之相连的物理设备(PLC)的信息值通过接口返回到客户端应用程序。也就是说,客户端通过这些接口,可以获得与OPC Server连接的物理设备的信息。对于集成应用程序,只要支持OPC接口,就能轻易访问物理设备,而无需相关的技术信息。 程序设计者可以使用相同的程序代码,操作不同的硬件装置,充分达成软件复用的目的。


它使用OPC Server引入了简单的示例程序。此示例的代码使用OPC 如果从.NET访问OPC Server,则需要交换COM和.NET的包装器。 自动化包装器(OPCDAAUTO.DLL),因此它几乎等于VB6.0。

OPC Server示例教程:OPC客户端——VB.NET示例

Declaration

Option Explicit
Public OpcServer As IOPCServerDisp
Public OpcItemMgt As IOPCItemMgtDisp
Public OpcItem As IOPCItemDisp
Private Const ItemMax = 8
Private Const OPC_DS_CACHE = 1
Private Const OPC_DS_DEVICE = 2
Dim ClientHandles(ItemMax) As Long
Dim ServerHandles As Variant
Dim bConnect As Boolean

连接到OPC服务器,创建OPCGroup和添加标签

Private Sub CONNECT_Click()
On Error GoTo err_hd
Dim i%
If bConnect Then
CONNECT.Caption = "Connect"
Set OpcServer = Nothing
Set OpcItemMgt = Nothing
Set OpcItem = Nothing
bConnect = False
READ_Button.Enabled = False
WRITE_Button.Enabled = False
For i% = ItemName.lbound To ItemName.ubound
ItemName(i%).Enabled = True
Next i%
Exit Sub
End If
'*** Connect to OPC Server ***
Set OpcServer = CreateObject(Form1.ServerName.Text)
If TypeName(OpcServer) = TypeName(Nothing) Then Return
'*** Add OPCGroup ***
Dim UpdateRate As Long
Dim ServerHdl As Long
UpdateRate = UpdateRateSet.Text
Set OpcItemMgt = OpcServer.AddGroup("Group One", True, UpdateRate, 22, 1, 0, ServerHdl, UpdateRate)
If TypeName(OpcItemMgt) = TypeName(Nothing) Then Return
'*** Add OPCItems ***
Dim ItemIDs(ItemMax) As String
Dim AccessPaths(ItemMax) As String
Dim Active(ItemMax) As Boolean
Dim Errors As Variant
Dim ItemObjects As Variant
Dim DataType(ItemMax) As Variant
For i% = 0 To ItemMax - 1
Active(i%) = True
ClientHandles(i%) = 22 + i%
AccessPaths(i%) = ""
ItemIDs(i%) = Form1.ItemName(i%).Text
DataType(i%) = 2
Next i%
OpcItemMgt.AddItems ItemMax, ItemIDs, Active,ClientHandles,
ServerHandles, Errors, ItemObjects, AccessPaths, DataType
Set OpcItem = ItemObjects(0)
bConnect = True
CONNECT.Caption = "DisConnect"
READ_Button.Enabled = True
WRITE_Button.Enabled = True
For i% = ItemName.lbound To ItemName.ubound
ItemName(i%).Enabled = False
Next i%
Exit Sub
err_hd:
MsgBox "Error connecting"
For i% = 0 To ItemMax - 1
Form1.Value(i%) = "Error"
Next i%
End Sub

缓存读取

Private Sub READ_Button_Click()
Dim ReadValue As Variant
Dim pQuality As Variant
Dim pTimestamp As Variant
Dim Errors As Variant
Dim i%
Dim io As IOPCSyncIODisp
Set io = OpcItemMgt
'*** Cache Read ***
io.OPCRead OPC_DS_CACHE, ItemMax, ServerHandles, ReadValue, pQuality, pTimestamp, Errors
For i% = 0 To ItemMax - 1
Form1.Value(i%) = ReadValue(i%)
pTimestamp(i%) = DateAdd("h", 9, pTimestamp(i%))
Form1.Time(i%) = pTimestamp(i%)
Form1.Quality(i%) = pQuality(i%)
Next i%
End Sub

SyncWrite

Private Sub WRITE_Button_Click()
Dim WriteValue(ItemMax) As Variant
Dim io As IOPCSyncIODisp
Dim Errors As Variant
Dim i%
Set io = OpcItemMgt
For i% = 0 To ItemMax - 1
WriteValue(i%) = Form1.Value(i%)
Next i%
'*** SyncWrite ***
io.OPCWrite ItemMax, ServerHandles, WriteValue, Errors
End Sub

点击“了解更多”下载产品最新试用版

↓↓↓

转载文章地址:http://www.lbhfjd.com/jiazhao/38915.html
(本文来自智到功成整合文章:http://www.lbhfjd.com)未经允许,不得转载!
标签:
硬件 微软 物理
相关推荐
网站简介 联系我们 网站申明 网站地图

版权所有:www.lbhfjd.com ?2017 智到功成

智到功成提供的所有内容均是网络转载或网友提供,本站仅提供内容展示服务,不承认任何法律责任。