AnsweredAssumed Answered

How to get sensor value in real time?

Question asked by Junzou on Mar 18, 2015
Latest reply on Apr 2, 2015 by bpayne

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?

Outcomes