龙空技术网

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

慧都科技 613

前言:

目前朋友们对“vbnet基础知识”大致比较关注,朋友们都想要分析一些“vbnet基础知识”的相关知识。那么小编同时在网上汇集了一些关于“vbnet基础知识””的相关资讯,希望咱们能喜欢,大家快快来了解一下吧!

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

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

Declaration

Option ExplicitPublic OpcServer As IOPCServerDispPublic OpcItemMgt As IOPCItemMgtDispPublic OpcItem As IOPCItemDispPrivate Const ItemMax = 8Private Const OPC_DS_CACHE = 1Private Const OPC_DS_DEVICE = 2Dim ClientHandles(ItemMax) As LongDim ServerHandles As VariantDim 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 Suberr_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, ErrorsEnd Sub

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

↓↓↓

标签: #vbnet基础知识