Examples of Advanced Formulas
  • 02 Feb 2024
  • PDF

Examples of Advanced Formulas

  • PDF

Article summary

This section contains examples of building advanced formulas. Follow the links below.

Basic IO Example

Imports Microsoft.VisualBasic

Imports Microsoft.Win32

Imports System

Imports System.Math

Imports System.IO

Imports Microsoft.VisualBasic.FileIO.FileSystem

Imports Microsoft.VisualBasic.Constants

Imports System.DateTime

Imports PARCCalcScript

Imports Opc.Da.Quality

Imports Opc.Hda

Module PARCModule

Public Sub Main(ByVal Factor As Double)

Dim dtStartTime As DateTime

Dim dtDate As DateTime

Dim ArrayCnt As Long

Dim i As Long

Dim PV As PARCValue

dtStartTime = C.StartTime.AbsoluteTime

dtDate = dtStartTime.AddMinutes(-15)

C.StartTime.AbsoluteTime = dtdate

C.AddInputTag(C.DataUTag)

C.NormalizeToTag(C.DataUTag)

Dim DPos As Integer = C.Normalized.FindTag(C.DataUTag)

ArrayCnt = C.Normalized.Values(DPos).Count

For i=0 To ArrayCnt-1

If C.Normalized.Values(DPos).Item(i).TimeStamp >= dtStartTime.AddMinutes(-1) Then

PV = New PARCValue(C.Normalized.Values(DPos).Item(i).TimeStamp,C.Normalized.Values(DPos).Item(i).Value * Factor,CShort(192))

C.OutValues.Add(PV)

End If

Next i

End Sub

End Module

TagStat Example

Imports Microsoft.VisualBasic

Imports Microsoft.Win32

Imports System

Imports System.Math

Imports System.IO

Imports Microsoft.VisualBasic.FileIO.FileSystem

Imports Microsoft.VisualBasic.Constants

Imports System.DateTime

Imports PARCCalcScript

Imports Opc.Da.Quality

Imports Opc.Hda

Namespace PARCScriptEngine

Class PARCModule

Inherits PARCCalcSupportWrapper

PublicSub Main(ByVal AggregateCode AsDouble,ByVal AggregateDurationMin AsDouble,ByVal AggregateStartHour AsDouble)

Dim PV As PARCValue

Dim i AsLong

Dim Interval AsDecimal = Convert.ToDecimal(AggregateDurationMin * 60.0) 'this in seconds

Dim AverageValues As Opc.Hda.ItemValueCollection

Dim dtStart AsDate

              dtStart = C.StartTime.AbsoluteTime

              dtstart = dtStart.AddMinutes(-AggregateDurationMin) ' in case this is a big number (>1D)

              dtStart = dtStart.AddDays(-1) ' Get the time back to make sure we go forward from the start hour cleanly

              dtStart = New DateTime(dtStart.Year,dtStart.Month,dtStart.Day)

        dtStart = dtStart.AddHours(AggregateStartHour)

              C.StartTime.AbsoluteTime = dtStart

              AverageValues = C.ReadProcessed(C.DataUTag,CInt(AggregateCode),C.StartTime,C.EndTime,Interval)

For i = 0 To AverageValues.Count - 1

IfNot AverageValues(i).Value IsNothingThen

                           PV = New PARCValue

                           PV.Value = AverageValues(i).Value

                           PV.Quality =  AverageValues(i).Quality 'Opc.Da.Quality.Good

                           PV.Timestamp = AverageValues(i).Timestamp

                           C.OutValues.Add(PV)

EndIf

Next i

EndSub

EndClass

EndNamespace

PARC_DebugExample

Imports Microsoft.VisualBasic.FileIO.FileSystem

Imports Microsoft.VisualBasic.Constants

Imports PARCCalcScript

Imports System.datetime

Module PARCModule

Public Sub Main()

Dim msg As String

Dim fn As String

fn = "C:\Test" & now.minute & now.second & ".txt"

Msg = "Upon entry startime = " & C.StartTime.AbsoluteTime & ", endtime = " & c.EndTime.AbsoluteTime & vbcrlf

WriteAllText(fn,Msg,False)

End Sub

End Module

Multiple Inputs Examples

Example Where Values Normalized to Tag

Imports Microsoft.VisualBasic

Imports Microsoft.Win32

Imports System

Imports System.Math

Imports System.IO

Imports Microsoft.VisualBasic.FileIO.FileSystem

Imports Microsoft.VisualBasic.Constants

Imports System.DateTime

Imports PARCCalcScript

Imports Opc.Da.Quality

Imports Opc.Hda

Module PARCModule

Public Sub Main(ByVal Factor As Double)

Dim dtStartTime As DateTime

Dim dtDate As DateTime

Dim ArrayCnt As Long

Dim i As Long

Dim PV As PARCValue

dtStartTime = C.StartTime.AbsoluteTime

dtDate = dtStartTime.AddMinutes(-15)

C.StartTime.AbsoluteTime = dtdate

C.AddInputTag(C.DataUTag)

C.AddInputTag("Plantname.SAMPLE.2-Square")

C.NormalizeToTag(C.DataUTag)

Dim DPos As Integer = C.Normalized.FindTag(C.DataUTag)

Dim DPos2 As Integer = C.Normalized.FindTag("Plantname.SAMPLE.2-Square")

ArrayCnt = C.Normalized.Values(DPos).Count

For i=0 To ArrayCnt-1

If C.Normalized.Values(DPos).Item(i).TimeStamp >= dtStartTime.AddMinutes(-1) Then

PV = New PARCValue(C.Normalized.Values(DPos).Item(i).TimeStamp,C.Normalized.Values(DPos).Item(i).Value * Factor - C.Normalized.Values(DPos2).Item(i).Value,CShort(192))

C.OutValues.Add(PV)

End If

Next i

End Sub

End Module

Example with Normalize to Step

Imports Microsoft.VisualBasic

Imports Microsoft.Win32

Imports System

Imports System.Math

Imports System.IO

Imports Microsoft.VisualBasic.FileIO.FileSystem

Imports Microsoft.VisualBasic.Constants

Imports System.DateTime

Imports PARCCalcScript

Imports Opc.Da.Quality

Imports Opc.Hda

Module PARCModule

Public Sub Main(ByVal Factor As Double)

Dim dtStartTime As DateTime

Dim dtDate As DateTime

Dim ArrayCnt As Long

Dim i As Long

Dim PV As PARCValue

Dim dStep As Decimal

dtStartTime = C.StartTime.AbsoluteTime

dtDate = dtStartTime.AddMinutes(-15)

dtDate = dtDate.AddSeconds(-dtDate.Second)

C.StartTime.AbsoluteTime = dtdate

C.AddInputTag(C.DataUTag)

C.AddInputTag("Plantname.SAMPLE.2-Square")

dStep = 180.0

C.NormalizeToStep(dStep)

Dim DPos As Integer = C.Normalized.FindTag(C.DataUTag)

Dim DPos2 As Integer = C.Normalized.FindTag("Plantname.SAMPLE.2-Square")

ArrayCnt = C.Normalized.Values(DPos).Count

For i=0 To ArrayCnt-1

If C.Normalized.Values(DPos).Item(i).TimeStamp >= dtStartTime.AddMinutes(-1) Then

PV = New PARCValue(C.Normalized.Values(DPos).Item(i).TimeStamp,C.Normalized.Values(DPos).Item(i).Value * Factor - C.Normalized.Values(DPos2).Item(i).Value,CShort(192))

C.OutValues.Add(PV)

End If

Next i

End Sub

End Module

Read Multiple Raw Example

Imports Microsoft.VisualBasic

Imports Microsoft.Win32

Imports System

Imports System.Math

Imports System.IO

Imports Microsoft.VisualBasic.FileIO.FileSystem

Imports Microsoft.VisualBasic.Constants

Imports System.DateTime

Imports PARCCalcScript

Imports Opc.Da.Quality

Imports Opc.Hda

Module PARCModule

Public Sub Main(ByVal Tag As String)

Dim dtStartTime As DateTime

Dim dtDate As DateTime

Dim ArrayCnt As Long

Dim i As Long

Dim j As Long

Dim PV As PARCValue

Dim Tag1RawData As Opc.Hda.ItemValueCollection

Dim Tag2RawData As Opc.Hda.ItemValueCollection

dtStartTime = C.StartTime.AbsoluteTime

Tag1RawData = C.ReadRaw(C.DataUTag,C.StartTime.AbsoluteTime.AddHours(-2),C.EndTime.AbsoluteTime,True)

ArrayCnt = Tag1RawData.Count

For i = 0 To ArrayCnt - 2

If Tag1RawData(i).Quality = Opc.Da.Quality.Good Then

'Loop forward looking for a good quality point

For j = i+1 To ArrayCnt - 2

If Tag1RawData(j).Quality = Opc.Da.Quality.Good Then Exit For

Next j

'Read in the raw data between my two good points

Tag2RawData = C.ReadRaw(Tag,Tag1RawData(i).Timestamp,Tag1RawData(j).Timestamp,True)

PV = New PARCValue

PV.Timestamp = Tag1RawData(i).TimeStamp

PV.Value = Tag2RawData.Count

PV.Quality = Opc.Da.Quality.Good

C.OutValues.Add(PV)

'Clear the raw array for the second tag just to be safe

Tag2RawData.Clear

End If

Next i

End Sub

End Module


Was this article helpful?

What's Next