示例:处理标签更改事件
本示例阐述了如何在标签值或标签的质量发生变化时获取事件。第一个子程序创建了 TagGroup 对象,并对其添加了标签并激活 TagGroup。第二个子程序处理 TagGroup 更改事件,并为 TagGroup 对象中标签的值或质量发生变化的每个标签记录消息。
Public WithEvents oGroup As TagGroup Private Sub Button1_Released() Dim TagsInError As StringList On Error GoTo errHandler If oGroup Is Nothing Then Set oGroup = Me.Application.CreateTagGroup(Me.AreaName, 500) oGroup.Add "system\Second" oGroup.Add "system\Minute" oGroup.Active = True 'after setting tag group active, use RefreshFromSource() method before any tag reads or writes oGroup.RefreshFromSource TagsInError End If Exit Sub errHandler: LogDiagnosticsMessage "VBA Error in SetUpTagGroup(). Error: " & Hex(Err.Number) & " - " & Err.Description, ftDiagSeverityError 'can add code to handle any specific errors End Sub Private Sub Display_Unload() 'ensure object memory is released when closing display Set oGroup = Nothing End Sub Private Sub oGroup_Change(ByVal TagNames As IGOMStringList) ' the Change()event can occur if the tag value and/or quality changes Dim vTagName As Variant Dim vValue As Variant Dim TimeStamp As Date Dim cQuality As tagQualityConstants Dim cSubStatus As tagSubStatusConstants Dim cLimit As tagLimitConstants Dim sQuality As String Dim oTag As Tag 'error handling added as error can be encountered during tag read On Error GoTo errHandler For Each vTagName In TagNames Set oTag = oGroup.Item(vTagName) 'reading only value vValue = oTag.Value 'reading tag value and other tag info such as Quality oTag.GetTagData vValue, TimeStamp, cQuality, cSubStatus, cLimit Select Case cQuality Case 0 sQuality = "Bad" Case 1 sQuality = "Uncertain" Case 2 sQuality = "Good" End Select If VarType(vValue) <> vbEmpty Then LogDiagnosticsMessage "Tag " & oTag.Name & " value = " & vValue & " Quality: " & sQuality Else LogDiagnosticsMessage "Unable to read value of tag " & oTag.Name & ". The quality of the value is " & sQuality, ftDiagSeverityWarning End If Next Exit Sub errHandler: LogDiagnosticsMessage "VBA Error in oGroup_Change(). Error: " & Hex(Err.Number) & " - " & Err.Description, ftDiagSeverityError 'can add code to handle any specific errors End Sub
提供反馈