您当前的位置:五五电子网电子知识plc技术PLC入门OPC Client 动态连接库开发和应用 正文
OPC Client 动态连接库开发和应用

OPC Client 动态连接库开发和应用

点击数:7224 次   录入时间:03-04 11:59:32   整理:http://www.55dianzi.com   PLC入门
        mMSecond = Format(Fix((Timer - Fix(Timer)) * 1000), "000")
        f.Writeline "[" & mHour & ":" & mMinute & ":" & mSecond & "." & mMSecond & "]       " & TraceMsg
        f.Close
        Set fs = Nothing
        Set f = Nothing
    End If
End Function

'(1).DLL初始化
Public Function Dll_Initial(strConfigFile As String) As Boolean
    Dim Result As String * 255, fs As Object, f As Object
    ConfigFile = strConfigFile
    GetPrivateProfileString "TRACE", "TraceOn", _
                        "ERROR", Result, 255, ConfigFile
    If Result <> "ERROR" Then
        If Result = 1 Then
            GetPrivateProfileString "TRACE", "TraceFile", _
                        "ERROR", Result, 255, ConfigFile
            If Result <> "ERROR" Then
                TraceFile = Result
            Else
                TraceFile = App.Path & "\Trace.log"
            End If
            Set fs = CreateObject("SCRJPTing.FileSystemObject")
            Set f = fs.CreateTextFile(TraceFile, True)
            f.Writeline ("*** BCA_OPC Trace Started , BeiChen Automation 2003 / Zhang Peng ***")
            f.Close
            TraceOn = True
        Else
            TraceOn = False
        End If
        Dll_is_Initial = True
        Dll_Initial = True
        Trace ">Dll_Initial"
        Trace "<Dll_Initial OK"
    Else
        MsgBox "无法找到配置文件: " & strConfigFile, vbOKOnly, "错误"
        Dll_is_Initial = False
        Dll_Initial = False
    End If
End Function

'(2).连接OPC服务器
Public Function ConnectServer(Optional IPAddress As String) As Boolean
    Trace ">ConnectServer"
    If Dll_is_Initial = False Then
        Trace "<ConnectServer Cancelled,Because Dll_Initial has not been called"
        Exit Function
    End If
    If Not ServerConnected Then
        ServerName = GetServerName
        On Error GoTo ErrorHandler
            Set ServerObj = New OPCServer
            ServerObj.Connect ServerName, IPAddress
            ServerConnected = True
            Trace "<ConnectServer OK"
    Else
            Trace "<Server has been connected,Please do not connect it again"
    End If
    ConnectServer = ServerConnected
    Exit Function
ErrorHandler:
    Trace "<ConnectServer Error,Please be sure that Server is running"
    ConnectServer = False
End Function

'(3).组态OPC客户机
Public Function SetConfiguration() As Boolean
    Trace ">SetConfiguration start..."
    If Dll_is_Initial = False Then
        Trace "<SetConfiguration Cancelled,Because Dll_Initial has not been called"
        Exit Function
    End If
    If ServerConnected = False Then
        Trace "<SetConfiguration Cancelled,Because ConnectServer has not been called"
        Exit Function
    End If
    If Configuration_is_Set = True Then
        Trace "<SetConfiguration Cancelled,Because configuration has been set"
        Exit Function
    End If
    'Begin to configure
    Dim f_ret As Long, ReturnedString As String * 1024, Valid_ReturnedString As String
    Dim ReturnedString1 As String * 1024, Valid_ReturnedString1 As String
    Dim Space_pos As Integer, GroupName As String
    Dim Space_pos1, Equal_pos As Integer, ItemName As String, ItemIndex As Long
    Dim NumItems As Long, ItemIDs(1) As String, ClientHandles(1) As Long, Serverhandles() As Long
    Dim Errors() As Long
    ReturnedString = ""
    ReturnedString1 = ""
    On Error GoTo ErrorHandler
        Set GroupCollection = ServerObj.OPCGroups
        GroupCollection.DefaultGroupIsActive = False
        f_ret = GetPrivateProfileSection("GROUP", ReturnedString, 1024, ConfigFile)
        Valid_ReturnedString = Left(ReturnedString, f_ret + 1)
        Do Until InStr(Valid_ReturnedString, Chr(0)) < 0
            Space_pos = InStr(Valid_ReturnedString, Chr(0))
            GroupName = Left(Valid_ReturnedString, Space_pos - 1)
            If GroupName = "" Then
                GoTo nxt3
            End If
            Set GroupObj = GroupCollection.Add(GroupName)
            GroupObj.IsSubscribed = False
            Trace "<Add group: " & GroupName & " OK"
            Set ItemCollection = GroupObj.OPCItems
            ItemCollection.DefaultIsActive = True

上一页  [1] [2] [3] [4] [5]  下一页


本文关键字:动态  开发  PLC入门plc技术 - PLC入门