'(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
上一篇:智能化小区与信息化小区