11 Replies Latest reply on Apr 2, 2015 2:00 PM by bpayne

    How to get sensor value in real time?

    Junzou

      I get different sensor values every time when I execute

      the following program several times.

      However, I can get the only same sensor value when I execute

      the process several times in 1 program.

       

      void getSensor()
      {
          // Get PI-SDK COM object
          IPISDKPtr pPISDK(__uuidof(PISDK));
      
      
          // Get and set server pointer
          ServersPtr pServers = pPISDK->Servers;
          ServerPtr  pServer  = pServers->Item[pcServerName];
      
      
          // Get Points information
          _PointListPtr pPointLst = pServer->GetPoints("tag='TargetTAG1' or tag='TargetTAG2'", 0);
          IEnumVARIANTPtr pEnumPoints = pPointLst->_NewEnum;
      
      
          // Load contents of Points in a sequential order
          _variant_t valValue;
          ULONG ulFetched;
          HRESULT lResult = pEnumPoints->Next(1, &valValue, &ulFetched);
          while (SUCCEEDED(lResult) && 0 < ulFetched)
          {
              // Get sensor value (float)
              float fValue = valValue.fltVal;;
      
      
              std::cout << "float value is [" << fValue << "]" << std::endl;
      
      
              // Next sensor value
              lResult = pEnumPoints->Next(1, &valValue, &ulFetched);
          }
      }
      
      
      int main()
      {
          getSensor();
      }
      

       

      Execution result

      float value is [5.057384e-036]

      float value is [5.056947e-036]

       

       

      float value is [4.408875e-037]

      float value is [4.376108e-037]

       

       

      float value is [2.622677e-036]

      float value is [2.600565e-036]

             :

             :

       

       

       

       

      ※All aqcuired values by calling getSensor() 10 times as follows are the same.

      int main()

      {

          for(int i = 0; i < 10; i++)

          {

              getSensor();

              Sleep(3000);

          }

      }

       

       

      float value is [4.761025e-036]

      float value is [4.763613e-036]

      float value is [4.761025e-036]

      float value is [4.763613e-036]

      float value is [4.761025e-036]

      float value is [4.763613e-036]

             :

             :

       

      Can anyone tell me how to modify the source code to get a sensor value

      at that point by calling getSensor() multiple times?

        • Re: How to get sensor value in real time?
          jfuchs

          I've moved this thread to the Developer's Club PI .NET Framework (PI AF SDK).

          • Re: How to get sensor value in real time?
            dng

            Hi Junzou,


            In your code, I saw that you are looping through the PointList collection (with 2 tags), but did not see any explicit call to retrieve values at a specific time. Are you trying to retrieve the snapshot value for TargetTAG1 and TargetTAG2? If not, what values are you trying to achieve? If you are trying to get the snapshot value, how often is your sensor value changing? How is this compared to the time interval between your code execution? If your sensor value is changing faster than subsequent execution of your code, you will get different results.

              • Re: How to get sensor value in real time?
                bpayne

                Hi Junzou,

                 

                Running your code I get a vt=9, which is a Dispatch and the values are not correct. It looks like this is point information and not snapshot values.

                  • Re: How to get sensor value in real time?
                    bpayne

                    Hi Junzou,

                    If you are looking to get the snapshot. Maybe something like this?

                     

                    void Test()
                    {
                    try
                          {  
                                 CoInitialize(NULL);
                    
                                // Get PI-SDK COM object
                                   IPISDKPtr pPISDK(__uuidof(PISDK));
                    
                    
                              // Get and set server pointer
                              ServersPtr pServers = pPISDK->Servers;
                              ServerPtr  pServer  = pServers->Item[pcServerName];
                    
                              _PointListPtr pPointLst = pServer->GetPoints("tag='TargetTAG1' or tag='TargetTAG2'", 0);
                            
                             for ( long x = 1; x = pPointLst->Count; ++x)
                             {
                                   _variant_t vVar(x);
                                   PIPointPtr pt = pPointLst->GetItem(&vVar);
                                   pt = pPointLst->Item[&vVar];
                                  
                                   // now get the snapshot
                                   _PIValuePtr pv(pt->Data->Snapshot );
                                   variant_t v( pv->GetValue() ) ;
                                   cout << "PIvalue = " << v.fltVal << endl;
                             }
                       }
                          catch( ... )
                          {
                             cout <<  "exception" << endl;
                       }
                    
                    }
                    

                    Returns (CDT158 and SINUSOID):

                    PIvalue = 98.1014

                    PIvalue = 98.1014

                    PIvalue = 98.1014

                    PIvalue = 98.1014

                    PIvalue = 98.1014

                    PIvalue = 98.1014

                    PIvalue = 98.1014

                    PIvalue = 98.1014

                    PIvalue = 98.1014

                    PIvalue = 98.1014

                  • Re: How to get sensor value in real time?
                    Junzou

                    Hi Daphne,

                     

                    Thank you for your opinion.

                    Actually I just started studying about PI system and I do not understand what snapshot is clearly.

                    I will check that first and think about this problem again.

                      • Re: How to get sensor value in real time?
                        bpayne

                        The most recently-acquired value for a point is called the snapshot.

                        PI Snapshot Subsystem gets the new data from the interface node and holds the most recent value for each point. This most recent value is called the snapshot for that point.

                        When a new event comes in, it becomes the snapshot for that point. The PI Server evaluates the previous snapshot according to the compression specifications and either sends the old value to the event queue or discards it.

                        Note:Sometimes an event enters PI Snapshot Subsystem with a timestamp that is older than the current snapshot value. This is called an out-of-order event. PI Server sends out-of-order events directly to the event queue for archiving, without compression testing.

                        These values in PI Snapshot Subsystem are called snapshot events or sometimes, just snapshots. The collection of all the snapshot values for all the points is the Snapshot Table

                    • Re: How to get sensor value in real time?
                      bpayne

                      Hello Junzou,

                       

                      Did the proposed solution work for you?

                      If so, you can mark it as "correct answer", otherwise please let us know if you if you have more questions or what you did to solve your problem.

                       

                      Regards,

                      Butch



                      hackathon_sig.png

                       

                         Register for the TechCon Programming and Security hackathon on April 29, 2015!