OPCcalc code sample

The following code demonstrates common functionality built into our calculation language.

OPCNODE = myserver

'This is an example OPCcalc library
'A library contains 1 or more equations (output tags)
'Each equation is defined by the line
'
'{$TAG(outtag)
'
'which defines the equation output tag
'The Default OPC Server is defined by the first line OPCNODE=

'If you are connected To the server (use the connect button on the toolbar)
'your tag names will be colored. Holding your mouse over a tag will display the current value
'$TAG(tagname) specifies an OPC item (tag) for input or output.
'$TAG(inputtag) retrieves the current value of the tag
'$(tagname) is equivalent to $TAG(tagname)
{initialization

'This equation block has no output tag since the equation line "{" above
'does Not name an output tag. Initialization blocks allow you to perform
'logic, dimension variables, etc which may be required for the library.
'An alternative to an initialization block is entering this initialization code
'within an equation below
'These constants can be used In any equation after the init block
Const Base_TempA As Single = 54.4
Const Base_TempB As Single = 56.7

{$TAG(otagf100)
'This equation will output To tag OTAGF100
'Output tag = the sum Of 2 other tags divided by 54.4
$TAG(otagf100) = ($TAG(itagf002) + $TAG(itagf003)) / Base_TempA

{$(otagf101)
'This equation will output To tag OTAGF101
'Use shorthand $(tagname) instead Of $TAG(tagname)
'Same logic as previous equation Using $(tagname) instead Of $TAG(tagname)
$(otagf101) =  ($(itagf002) + $(itagf003)) / Base_TempA

{$(otagf102)
'This equation will output to tag OTAGF102
'Use shorthand $OUTPUT
'Same logic as previous equation using $OUTPUT
$OUTPUT= ($(itagf002) + $(itagf003)) / Base_TempA

{$(otagf103)
'This equation will output To tag OTAGF103
'Use VB.Net variables And VB.Net constructs
'Since we are generating VB.Net code, you can use most VB.Net
'language statements, variables, etc.
'Variables can only be declared once per library before first usage.
Dim myval1 As Single
Dim myval2 As Single

myval1 = $(itagf002)
myval2 = $(itagf003)

'Use VB.Net If/Then/Else construct
If (myval1 > myval2) Then
    $OUTPUT = myval1
Else
    $OUTPUT = myval2
End If

{$(otagf104)
'This equation will output To tag OTAGF104
'$CONTINUE: skip sending a value to the output tag during an execution
'Only output a value this execution If a condition exists
If ($(itagf002) > $(itagf003)) Then
    'output the average Of the 2 tags
    $OUTPUT = ($TAG(itagf002) + $TAG(itagf003)) / 2
Else
    'skip output Of this equation
    $CONTINUE
End If

{$(otagf105)
'This equation will output to tag OTAGF105
'Change the default output time (OPC HDA required)
'The default output timeStamp is $CURTIME
'Set the output time to 1 hour ago using $OUTTIME and the VB.Net Date Function AddHours
$OUTTIME = $CURTIME.AddHours(-1)

$OUTPUT = ($TAG(itagf002) + $TAG(itagf003)) /2

{$(otagf106)
'This equation will output To tag OTAGF106
'Change the default output quality (OPC HDA Required)
'based on the quality of the input tags
'The default output quality Good
'Set the output quality to bad if any input quality is not good
If ($QUALITY(itagf002) <> qualGOOD Or $QUALITY(itagf003) <> qualGOOD) Then
    'Set quality Of output tag (otagf106)
    $SETQUAL(qualUNCER)
Else
    'The next line is not required since default output quality is good
    $SETQUAL(qualGOOD)
End If

$OUTPUT= ($TAG(itagf002) + $TAG(itagf003)) / 2

{$(otagf107) QUALCHK
'This equation will output To tag OTAGF107
'Change the default output quality (OPC HDA required) using QUALCHK
'QUALCHK (located On the first line Of the equation)
'will Set the quality of the output tag to the worst quality of the Input tags
$OUTPUT = ($TAG(itagf002) + $TAG(itagf003)) / 2

{$(otags001)
'This equation will output to tag OTAGS001
'Output To a String tag (OTAGS001 Is a String/Char tag)
'Use the VB.Net "Select Case" To determine the output String
Select Case ($TAG(itagf002) - $TAG(itagf003))
    Case<0
        $OUTPUT = "First tag is less"
    Case 0
        $OUTPUT = "Tags are equal"
    Case > 0
        $OUTPUT = "Second tag is less"
    Case Else
        $OUTPUT = "We have a problem"
End Select

{$(otagf108)
'This equation will output To tag OTAGF108
'Demostrate the TRACE (debug) Function
'TRACE allows you to output debug statements to a file during execution.
TRACE_FILENAME = "c:\temp\mydebugfile.out"
TRACE("Starting value of otagf108 = ", $(otagf108), $CURTIME)

If ($(otagf108) > 100) Then
    $OUTPUT = $(itagf107) + $(itagf108) / 3
Else
    $OUTPUT=$(itagf107) + $(itagf108) / 5.4
End If
TRACE("Ending value of otagf108 = ", $(otagf108), $CURTIME)

{$(otagf109)
'This equation will output to tag OTAGF109
'Call aggregate Function xlTagTimeAverage
'Use line continuation character
'Use $CONTINUE so no output occurs
'Use $SETQUAL to set the quality of the output value
'OPCcalc has many built-in Functions For performing aggregates
'Use the Functions TAB on the Right To view the list Of built-in Functions

Dim OneHrAvg As Single
Dim avgQual As Integer, status As Integer, statusmsg As String

'Get the average value of a tag over the last hour
'Passing the optional status arguments allows you to handle any errors
OneHrAvg = xlTagTimeAverage("itagf005", $CURTIME.addhours(-1), _
  $CURTIME , avgQual, status, statusmsg)

If (status <> 0) Then
    'Error getting average for the tag, output to the debug file
    TRACE(statusmsg, , $CURTIME)

    'Do Not send a value To the output tag this execution
    $CONTINUE
Else
    'Success getting the average value'Send the average To the output tag
    $OUTPUT = OneHrAvg

    'Set the quality Of the output tag To the returned quality Of the average value
    $SETQUAL(avgQual)
End If

{$(otagf110)
'This equation will output To tag OTAGF110
'Exception-based calculation Using $NEWTIME(tagname) Function'Only perform an output if either of the 2 input tags has a
'new timestamp since the last execution (exception-based calculation)
If ($NEWTIME(itagf100) Or $NEWTIME(itagf101)) Then
    'one Of the tags has a New current TimeStamp
    $OUTPUT = $(itagf100) / $(itagf101)
Else
    'do not perform an output
    $CONTINUE
End If

{$(otagf111) NEWTIME
'This equation will output to tag OTAGF111
'Exception-based calculation Using NEWTIME equation-level keyword
'Note: "input tag" refers To tags used In $Functions'Only perform an output If either Of the 2 Input tags has a New
'timestamp since the last execution (exception-based calculation)
$OUTPUT = $(itagf100) / $(itagf101)

{$(otagf112) NEWTIME
'This equation will output To tag OTAGF112
'Exception-based calculation Using NEWTIME equation-level keyword
'And an xlFunction'Note: "input tag" refers To tags used In $Functions
'This equation will never execute since NEWTIME Is used And
'this equation has no Input tags (no $Function tags - see Next equation)
$OUTPUT= xlTagAverage("otagf002", $CURTIME.addminutes(-60), $CURTIME)

{$(otagf113) NEWTIME
'This equation will output to tag OTAGF113
'Exception-based calculation using NEWTIME equation-level keyword
'And an xlFunction
'Note: "input tag" refers to tags used in $Functions
'This equation will execute since $NAME makes the tag otagf002 an "input tag" for this equation
$OUTPUT= xlTagAverage($NAME(otagf002), $CURTIME.addminutes(-60), $CURTIME)

{$(otagf114)
'xlFunctions and the optional Status argument
'$EQERR and $EQERRMSG to catch and reset a runtime error
'$CONSOLE to output messages to the console (Console can be viewed using the Test button on the toolbar)
'We will not pass the optional Status And statusmsg arguments to xlTagAverage
'Therefore, any errors (e.g. unknown tag name) will cause a run-time error in this equation.
Dim avgval As Single
avgval = xlTagAverage("badtagname", $CURTIME.AddMinutes(-60), $CURTIME)

'Check For run-time error
If ($EQERR)
    'Output run-time error message To the console
    $CONSOLE ("->" & $EQERRMSG)
    'reset the error flag so this equation will not automatically set the output quality to bad
    $EQERR = False
    'Default the average value
    avgval = 10
End If

'output the average value
$OUTPUT= avgval

{$(otagf115)
'Use $SENDNOW to output multiple times during one execution
'During each execution, OPCcalc will output a single value to each output tag
'This output occurs once at the end of each equation after your equation logic has executed.
'The default output time is $CURTIME
'If you assign the output value multiple times ($OUTPUT=), OPCcalc will only output the last assigned value.
'$SENDNOW allows you to output multiple times during 1 execution

'Assume this library is executing every 10 seconds
'Output to otagf115 for now and the previous 5 seconds
For i As Integer = 1 To 5
    $OUTTIME = $CURTIME.AddSeconds(-1* i)
    $OUTPUT = i
    $SENDNOW
Next i

'There is still a "normal" output for this equation
unless $CONTINUE Is used
$OUTTIME = $CURTIME
$OUTPUT = 0