4 Replies Latest reply on May 17, 2016 1:07 PM by pthivierge

    Find previous state of an digital signal

    jainc_paresh

      Hello Team,

       

      Greetings for the day!

       

      I have a digital signal with 3 different states, say Red, Green and Blue.

       

      Now at a given time i need to programatically find out what was the last state before the given time state. To put it in form of example I have TagA with 3 digital state.

      The state of signal at Red, Blue and Green as mentioned

        

      Sr.No.StateTime
      1Blue9-May-2016 8:16:31 AM
      2Blue10-May-2016 8:16:31 AM
      3Blue11-May-2016 8:16:31 AM
      4Red12-May-2016 8:16:31 AM
      5Red12-May-2016 8:36:31 AM
      6Red12-May-2016 8:46:31 AM
      7Green12-May-2016 9:16:31 AM
      8Green17-May-2016 9:53:00 AM

       

      So suppose from the above case I would like to know the last (previous) status at 12-May-2016 8:46:31 AM should give me Blue at time 11-May-2016 8:16:31 AM.

      How could I achieve my requirement, I dont want to write a loop simply pass the time and need to know the last status, disregarding the current status at a given time. Please assist.

       

      Thanks & Regards,

      Paresh Jain

        • Re: Find previous state of an digital signal
          aksnk89

          Using PI SDK

           

          Dim srv as Server

          Dim previousVal as Variant

          Set srv = PISDK.Servers("ServerName")

          previousVal = srv.PIPoints.Item("YourPITag").Data.ArcValue("YourTimestamp", rtBefore)

            • Re: Find previous state of an digital signal
              jainc_paresh

              Hello Akash,

               

              Thank you for your response.

               

              Unfortunately this of no help to me and already tried before. Because this will just give me previous value/state of the give time. But my requirement is not this I want to know previous state if the state is same system should look more past and let me know previous state. You can have a look at example.

               

              Hope you have some other alternative.

               

              Thanks & Regards,

              Paresh Jain

                • Re: Find previous state of an digital signal
                  aksnk89

                  Hi Paresh,

                  Couldn't think much on how to retrieve expected values without loops but the below procedure should give what you need. Hope that helps.

                   

                   

                   

                  Sub test()

                      Dim srv As Server

                      Dim previousVal As PIValues

                      Dim filter As String

                      Dim prvval As PIValue

                      Dim val As Variant

                     

                      Set srv = PISDK.Servers("ServerName")

                     

                      val = srv.PIPoints.Item("YourDigitalTag").Data.ArcValue("TimeStamp", rtAtOrBefore)

                      filter = "tagval('YourDigitalTag','*')<>""" & val & """"

                     

                      Set previousVal = srv.PIPoints.Item("YourDigitalTag").Data.RecordedValuesByCount("Timestamp", 1, dReverse, btAuto, filter, fvRemoveFiltered)

                     

                     

                          For Each prvval In previousVal

                              MsgBox prvval.Value & " " & prvval.TimeStamp.LocalDate

                          Next

                  End Sub

                   

                  Regards,

                  Akash

              • Re: Find previous state of an digital signal
                pthivierge

                Hello Paresh,

                 

                What Akash Naik is proposing is a good logic.

                 

                Could you use AF SDK for this? We do recommend using AF SDK over PI SDK.

                 

                In the case you need to make this search many times ( e.g. more that 1000 times) With AF SDK, you could optimise your queries by using bulk calls or achieving multi-threading (PI SDK multi-threading can produce hard to understand results).

                If you look at AFData.RecordedValues Method there is a parameter called filter expression that you can use the same way as Akash demonstrated. 

                 

                To make use of the AFSDK bulk calls:

                1. You build a list of tags by making use of a PI Point List: PIPointList Class .
                2. To get the first value for all the tags at the same time for the entire list you just built, you can use: PIPointList.RecordedValue Method
                3. To get the previous value you then use : PIPointList.RecordedValuesByCount Method

                 

                Using bulk reduces the total number of network calls  and make better usage of ressources.

                 

                Hope this helps, Let us know if this works for you.

                1 of 1 people found this helpful