2 Replies Latest reply on Nov 17, 2011 3:58 AM by hanyong

    how to translate the PE code to VBA in PB?

    xwang

        I need to use this  macro code in PI processbook . This code works in PI calculations.  How do I code this in Pi Processbook?
       
       Test =  (tagval('71pca5','*') - prevval('71pca5',prevevent('71pca5','*'))) /((prevevent('71pca5','*')) - (prevevent('71pca5',prevevent('71pca5','*'))))

       

       

       

      Note: it is from one customer, who do not buy the vCampus License.  I wish I could get the useful solution here, and suggest the customer to join in vCampus.  Thanks for your guy help.

        • Re: how to translate the PE code to VBA in PB?
          hanyong

          I think it will not be that straight-forward to translate that equation to a equivalent VBA code, because you probably need to use PI SDK to get the reference to the PIPoint and read the appropriate value for the calculation.

           

          Just wondering, is the user just thinking of displaying the result of the calculation in a trend or value? Why not use the PI Calculation Dataset in ProcessBook?

            • Re: how to translate the PE code to VBA in PB?
              hanyong

              To perform a PE calculation like the one you have put up in VBA, I can think of doing it in 2 ways both using PI SDK in VBA

               

              1. pass the PE equation to PI Server using IPICalculation.Calculate method

              Sub
              calculatePE()
                  Dim svr As Server
                  Set svr = PISDK.Servers.DefaultServer
                 
                  ' cast Server to IPICalculation interface
                  Dim ipicalc As IPICalculation
                  Set ipicalc = svr
                 
                  Dim PE As String
                  PE = "(tagval('CDT158','*') - prevval('CDT158',prevevent('CDT158','*'))) /((prevevent('CDT158','*')) - (prevevent('CDT158',prevevent('CDT158','*'))))"
                 
                  Dim values As PIValues
                  ' use the IPICalculation.Calculate method to calculate the PE at current time
                  Set values = ipicalc.Calculate("*", "*", PE, stInterval, 1)
                 
                  Dim value As PIValue
                  Set value = values(1)
                  ' display result in a message box
                  MsgBox (value.value)
              End Sub
               

               

              2. read the last 2 archive values of the tag and perform the calculation (since the PE equation is basically finding the rate of change between the current value and the last archive value of the tag in PI server)

              Sub
              calculatePE2()
                  Dim svr As Server
                  Set svr = PISDK.Servers.DefaultServer
                 
                  ' get the required tag
                  Dim pt As PIPoint
                  Set pt = svr.PIPoints("cdt158")
                 
                  ' get the last 2 values of the tag in PI archive
                  Dim values As PIValues
                  Set values = pt.Data.RecordedValuesByCount("*", 2, dReverse, btInside)
                 
                  Dim currentvalue As PIValue
                  Dim previousvalue As PIValue
                 
                  Set currentvalue = values(1)
                  Set previousvalue = values(2)
                 
                  ' perform the calculation
                  Dim ROC As Double
                  ROC = (currentvalue.value - previousvalue.value) / (currentvalue.TimeStamp.UTCSeconds - previousvalue.TimeStamp.UTCSeconds)

                  ' display result in a message box
                  MsgBox ROC
              End Sub

              Without more information, I would still say that using a PI Calculation Dataset will be a better option, since doing the calculation in VBA also means that you need to handle when the calculation takes place or refreshes (probably calling the subroutine from Display_Open and Display_DataUpdate event?)