本文共 3005 字,大约阅读时间需要 10 分钟。
OPC技术论坛 OPC服务器,客户程序技术讨论
一、VB新建窗体如下详见相册OPC技术。二、引用如下详见相册OPC技术。三、代码如下Dim WithEvents g_Server As OPCServerDim g_Browser As OPCBrowserDim BrowseFilter As Long
Dim Vt_Filter As Integer
Dim nodX As NodeDim Value As VariantDim FullName As StringDim Relative As StringDim i As Integer
Private Sub cmbAccessRights_Click()
Select Case cmbAccessRights.Text Case "OPC_ACCESS_WRITE" g_Browser.AccessRights = OPCWritable Case Else g_Browser.AccessRights = OPCReadable End SelectEnd Sub
Private Sub cmbBrowseFilter_Click()
Select Case cmbBrowseFilter.Text Case "OPC_FLAT" BrowseFilter = OPC_FLAT Case Else BrowseFilter = OPC_BRANCH End SelectEnd SubPrivate Sub cmbVtFilter_Click()
Select Case cmbVtFilter.Text Case "VT_EMPTY" g_Browser.DataType = VT_EMPTY Case "UINT1" g_Browser.DataType = VT_UI1 Case "UINT2" g_Browser.DataType = VT_UI2 Case "TEXT" g_Browser.DataType = VT_BSTR Case "BOOLEAN" g_Browser.DataType = VT_BOOL End SelectEnd SubPrivate Sub Command1_Click()
Dim i As Integer Dim Key As String MousePointer = 11 ' Mousepointer as hourclass Set nodX = Nothing TreeView.Nodes.Clear Select Case BrowseFilter Case OPC_FLAT BuildFlat Case OPC_BRANCH BuildTree End Select MousePointer = 0 ' Normal mousepointer End Sub'----------------------------------------------------------------------------' Sub BuildFlat()' Purpose: show all items'----------------------------------------------------------------------------Public Sub BuildFlat() Dim Key As String Dim Leaf As Variant g_Browser.MoveToRoot g_Browser.ShowLeafs True For Each Leaf In g_Browser i = i + 1 Key = "#" & i Set nodX = TreeView.Nodes.Add(Null, tvwNext, Key, g_Browser.GetItemID(Leaf)) DoEvents Next LeafEnd Sub'----------------------------------------------------------------------------
' Sub BuildTree()' Purpose: recursive building of the tree view'----------------------------------------------------------------------------Public Sub BuildTree(Optional Relative As Variant) Dim Branch, Leaf As Variant Dim Key As String g_Browser.ShowBranches For Each Branch In g_Browser i = i + 1 Key = "#" & i Set nodX = TreeView.Nodes.Add(Relative, tvwChild, Key, Branch) 'Go down to next level g_Browser.MoveDown (Branch) BuildTree (Key) 'Go up to next level g_Browser.MoveUp DoEvents Next Branch g_Browser.ShowLeafs False For Each Leaf In g_Browser i = i + 1 Key = "#" & i Set nodX = TreeView.Nodes.Add(Relative, tvwChild, Key, g_Browser.GetItemID(Leaf)) Next Leaf End SubPrivate Sub Command2_Click()
Set g_Server = Nothing Set g_Browser = Nothing EndEnd SubPrivate Sub Form_Load()
Set g_Server = New OPCServer g_Server.Connect ("Matrikon.OPC.Simulation")'OPC SERVER名称 Set g_Browser = g_Server.CreateBrowser cmbVtFilter.Text = "VT_EMPTY" cmbVtFilter.AddItem "UINT1" cmbVtFilter.AddItem "UINT2" cmbVtFilter.AddItem "TEXT" cmbVtFilter.AddItem "BOOLEAN" cmbVtFilter.AddItem "VT_EMPTY" BrowseFilter = OPC_BRANCH g_Browser.AccessRights = OPCReadable g_Browser.DataType = VT_EMPTY TreeView.LineStyle = tvwRootLines ' Linestyle 1End Sub
转载地址:http://akini.baihongyu.com/