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

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

点击数:7224 次   录入时间:03-04 11:59:32   整理:http://www.55dianzi.com   PLC入门
             f_ret = GetPrivateProfileSection(GroupName, ReturnedString1, 1024, ConfigFile)
            Valid_ReturnedString1 = Left(ReturnedString1, f_ret + 1)
            Do Until InStr(Valid_ReturnedString1, Chr(0)) < 0
                Space_pos1 = InStr(Valid_ReturnedString1, Chr(0))
                ItemName = Left(Valid_ReturnedString1, Space_pos1 - 1)
                If ItemName = "" Then
                    GoTo nxt2
                End If
                If InStr(ItemName, "UpdateRate") > 0 Or InStr(ItemName, "IsSubscribed") > 0 Then
                    GoTo nxt1
                End If
                ItemCollection.DefaultRequestedDataType = GetItemDataType(ItemName)
                ItemIndex = ItemIndex + 1
                NumItems = 1
                ItemIDs(1) = ItemName
                ClientHandles(1) = ItemIndex
                ItemCollection.AddItems NumItems, ItemIDs, ClientHandles, Serverhandles, Errors
                AllItemsInfo.ItemInfo_Add ItemName, GroupName, ItemIndex, Serverhandles(1)
                Trace "<Add Item: " & ItemName & " OK"
nxt1:           Valid_ReturnedString1 = Mid(Valid_ReturnedString1, Space_pos1 + 1)
            Loop
nxt2:       f_ret = GetPrivateProfileInt(GroupName, "UpdateRate", 0, ConfigFile)
            GroupObj.UpdateRate = f_ret
            Trace "<Set group: " & GroupName & " UpdateRate=" & f_ret & " OK"
            f_ret = GetPrivateProfileInt(GroupName, "IsSubscribed", 0, ConfigFile)
            GroupObj.IsSubscribed = IIf(f_ret = 1, True, False)
            GroupObj.IsActive = True
            Trace "<Set group: " & GroupName & " IsSubscribed=" & f_ret & " OK"
            Valid_ReturnedString = Mid(Valid_ReturnedString, Space_pos + 1)
            Set GroupObj = Nothing
            Set ItemCollection = Nothing
        Loop
nxt3:   Trace "<SetConfiguration end"
        Configuration_is_Set = True
        SetConfiguration = True
        Exit Function
ErrorHandler:
    Trace "<SetConfiguration Error,Please be sure that config file is correct"
    Configuration_is_Set = False
End Function

'获取服务器名称
Private Function GetServerName() As String
  Dim Result As String * 255
  GetPrivateProfileString "SERVER", "Server", _
                          "ERROR", Result, 255, ConfigFile
  GetServerName = RemoveSpaces(Result)
End Function

'获取服务器连接状态
Private Function GetConnectStatus() As Boolean
    GetConnectStatus = ServerConnected
End Function

' 功能块:移除空格
Private Function RemoveSpaces(Item As String) As String
  Dim Result As String
  Dim i As Integer
  i = 1
  While (Mid$(Item, i, 1) <> Chr(0))
    Result = Result & Mid$(Item, i, 1)
    i = i + 1
  Wend
  RemoveSpaces = Result
End Function

Private Sub Class_Initialize()
    Configuration_is_Set = False
    Dll_is_Initial = False
    ServerConnected = False
End Sub

'DLL终止
Private Sub Class_Terminate()
    Set ServerObj = Nothing             ' 释放ServerObj
    Set GroupCollection = Nothing       ' 释放GroupCollection
    Set ItemCollection = Nothing        ' 释放ItemCollection
    ServerConnected = False
    Dll_is_Initial = False
    Configuration_is_Set = False
    Trace "<Dll is terminate"
End Sub
'全局数据改变
Private Sub GroupCollection_GlobalDataChange(ByVal TransactionID As Long, ByVal GroupHandle As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)
    Dim i As Integer, GroupName As String, ItemName As String
    Trace "<== GlobalDataChange!Following is the data:"
    GroupName = GroupCollection.GetOPCGroup(GroupHandle).Name
    Trace "<== GroupName: " & GroupName & " Number of Items:" & NumItems
    For i = 1 To NumItems
        ItemData(ClientHandles(i)) = ItemValues(i)
        Trace "<== Item's Name: " & AllItemsInfo.GetItem_Name(ClientHandles(i)) & " Values: " & ItemValues(i) & " Changed Data: " & TimeStamps(i)
    Next
    Trace "<== GlobalDataChange End"
End Sub
'获取单个变量数据
Public Function GetData(ItemName As String)
    Trace ">GetData start:ItemName= " & ItemName
    If Dll_is_Initial = False Then
        Trace "<GetData Cancelled,Because Dll_Initial has not been called"
        Exit Function
    End If
    If ServerConnected = False Then
        Trace "<GetData Cancelled,Because ConnectServer has not been called"
        Exit Function
    End If
    If Configuration_is_Set = False Then

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


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