6 Replies Latest reply on Nov 22, 2018 4:44 PM by gregor

    Using PISDK in Python: How do I access PISDK constants?

    Unitech

      Hi,

       

      I'm trying to retrieve data from a PI server using Python (2.5) and the following (simplified) code seems to work:

       

      # Python PISDK test
      import win32com.client

       

      pi_sdk = win32com.client.Dispatch('PISDK.PISDK')
      conn   = win32com.client.Dispatch('PISDKDlg.Connections')

       

      pi_server = pi_sdk.Servers('WIN-SERVER2008') # server host name, must be configured in the "known servers table"
      conn.Login(pi_server,'pidemo','',1,0)

       

      # using point tag: SINUSOID (pre-configured by default in demo PI server)
      pi_point = piserver.PIPoints['SINUSOID']

       

      # retriving recorded data for the last 6 hours:
      recorded_values = pi_point.Data.RecordedValues('*-6h','*',3,"",0,None)
      time.sleep(0.1) # allow time for the call to complete.

       

      print "recorded_values.Count = ", recorded_values.Count
      for sample in recorded_values:
          v = sample.Value
          t = sample.TimeStamp.LocalDate
          print "Value =",v, " (timestamp =",t,")"

       

      Program output:

       

      >>>
      recorded_values.Count =  7
      Value = 4.74060535431  (timestamp = 09/09/11 08:19:44 )
      Value = 0.769782066345  (timestamp = 09/09/11 08:39:52 )
      Value = 3.78370571136  (timestamp = 09/09/11 09:44:52 )
      Value = 24.1980323792  (timestamp = 09/09/11 10:57:52 )
      Value = 85.7742614746  (timestamp = 09/09/11 13:31:22 )
      Value = 96.3372421265  (timestamp = 09/09/11 14:15:52 )
      Value = 96.3372421265  (timestamp = 09/09/11 14:19:44 )
      >>>

       

      SUCCESS! However,  I have used numbers as arguments in the .Data.RecordedValues(,,,,,) - call.  I would like to used the PISDK constants instead, with  something like:

       

      bt = btAuto  # (not working)
      bt = win32com.client.Constants.btAuto  # (not working)
      bt = win32com.client.Constants.BoundaryTypeConstants.btAuto  # (not working)
      bt= 0    # working 
      fv = 3    # working
      recorded_values = pi_point.Data.RecordedValues('*-6h','*', bt ,"", fv ,None)

       

      But this is not working, and I haven't found documentation on how to do this. So, how do I access the SDK constants?

       

      Any help would be appreciated.

       

      regards Andreas østrem

       

       

       

       

        • Re: Using PISDK in Python: How do I access PISDK constants?

          Try the following:

           

           

           

          for i in pi_sdk.PIConstants["BoundaryTypeConstants"]:
             print i.Name, i.Value

           

           

           

          pi_sdk.PIConstants["BoundaryTypeConstants"]["Automatic"].Value

           

           

           

           

           

          Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32

           

          Type "copyright", "credits" or "license()" for more information.

           

          DreamPie 1.1.1

           

          >>> import win32com.client

           

          ... pi_sdk = win32com.client.Dispatch('PISDK.PISDK')

           

          >>> pi_sdk.PIConstants["BoundaryTypeConstants"]

           

          0: <COMObject <unknown>>

           

          >>> for i in pi_sdk.PIConstants["BoundaryTypeConstants"]:

           

          ...     print i.Value

           

          0

           

          1

           

          2

           

          3

           

          >>> for i in pi_sdk.PIConstants["BoundaryTypeConstants"]:

           

          ...     print i.Name, i.Value

           

          Inside 0

           

          Outside 1

           

          Interpolated 2

           

          Automatic 3

           

          >>> pi_sdk.PIConstants["BoundaryTypeConstants"]["Automatic"]

           

          1: <COMObject <unknown>>

           

          >>> pi_sdk.PIConstants["BoundaryTypeConstants"]["Automatic"].Value

           

          2: 3

           

          >>> 

           

          These are enumerations, that's why putting the value directly works.

           


          1 of 1 people found this helpful
            • Re: Using PISDK in Python: How do I access PISDK constants?

              By the way, one can get the full list via something like this

               

              for constantSet in pi_sdk.PIConstants:

               

                 print constantSet.Name

               

                 for constant in constantSet:

               

                     print "\t", constant.Name, "=", constant.Value

                • Re: Using PISDK in Python: How do I access PISDK constants?
                  Unitech

                  Thank you, just what I was looking for!

                   

                  Andreas

                    • Re: Using PISDK in Python: How do I access PISDK constants?
                      mikeloria

                      I found the sample code very interesting, thank you.

                       

                      There was a couple of fixes I needed to add:

                       

                      Need to add:

                       

                       import time

                       

                      pi_server object is called piserver

                       

                      # Python PISDK test
                      import win32com.client
                      import time

                       

                      pi_sdk = win32com.client.Dispatch('PISDK.PISDK')
                      conn   = win32com.client.Dispatch('PISDKDlg.Connections')

                       

                      pi_server = pi_sdk.Servers('xxxxxx') # server host name, must be configured in the "known servers table"
                      conn.Login(pi_server,'pidemo','',1,0)

                       

                      # using point tag: SINUSOID (pre-configured by default in demo PI server)
                      pi_point = pi_server.PIPoints['SINUSOID']

                       

                      # retriving recorded data for the last 6 hours:
                      recorded_values = pi_point.Data.RecordedValues('*-6h','*',3,"",0,None)
                      time.sleep(0.1) # allow time for the call to complete.

                       

                      print "recorded_values.Count = ", recorded_values.Count
                      for sample in recorded_values:
                          v = sample.Value
                          t = sample.TimeStamp.LocalDate
                          print "Value =",v, " (timestamp =",t,")"

                        • Re: Using PISDK in Python: How do I access PISDK constants?
                          mhamel

                          @Mike: A PI SDK constants is called with this syntax: pi_sdk.PIConstants['Collection']['Constant']. I have added what your call to RecordedValues method looks like with the use of PIConstants

                           
                          # ...
                          
                          # retriving recorded data for the last 6 hours:
                          recorded_values = pi_point.Data.RecordedValues('*-6h','*',pi_sdk.PIConstants['BoundaryTypeConstants']['Inside'],"",
                          pi_sdk.PIConstants['FilteredViewConstants']['fvShowFilteredState'],None)
                          
                          # ...
                          

                           

                           

                          Let me know if that helped.

                  • Re: Using PISDK in Python: How do I access PISDK constants?
                    gregor

                    Dear PI geek or visitor interested in PI,

                     

                    Please note that PI SDK is announced for deprecation and we suggest using AF SDK instead. Please see the link above referring a resource on how to code in Python against AF SDK.