Get the Change Based on Period

Introduction to Monitoring Data Point Commands

Monitoring data points and ensuring they return to a predefined state after a certain threshold is a common requirement in automation systems. This guide presents a programming trick in WinCC OA to check for changes in data points of a specific class and reset them if they remain in a changed state beyond a specified threshold.

Monitoring Data Point Changes

The following code continuously monitors a list of data points, checking if they have been altered. If a data point remains in a changed state for longer than the defined threshold, the system will reset it to its original state. This approach helps in handling commands that might get stuck or remain active for too long.

// Function to Monitor and Reset Data Point Commands
void MonitorDataPoints(dyn_string devices) {
    while(TRUE) {
        for(int i = 0; i < dynlen(devices); i++) {
            dyn_string dataPoints = dpNames(devices.at(i) + "_Command" + "*");
            CheckAndResetDataPoints(dataPoints);
        }
        delay(4);
    }
}

void CheckAndResetDataPoints(dyn_string dataPoints) {
    for(int i = 0; i < dynlen(dataPoints); i++) {
        bool currentValue;
        time timestampStr;
        if(dpElementType(dataPoints.at(i)) == 23){
            dpGet(dataPoints.at(i), currentValue);
            if(currentValue) {
                dpGet(dataPoints.at(i) + ":_original.._stime", timestampStr);
                time currentTime = getCurrentTime();
                if(period(currentTime) - period(timestampStr) >= threshold) {
                    DebugN("Resetting command due to threshold ", dataPoints.at(i), currentValue);
                    dpSet(dataPoints.at(i), FALSE);
                }
            }
        }
    }
}

Understanding the Code

Let's break down the key parts of the code:

  • MonitorDataPoints(dyn_string devices): This function runs indefinitely, monitoring a list of devices. For each device, it retrieves the data points and checks their state.
  • CheckAndResetDataPoints(dyn_string dataPoints): This function iterates through the data points, checks their current value, and retrieves the timestamp of the last change using dpGet. If the data point has been active for longer than the specified threshold, it resets the data point.
  • dpElementType(dataPoints.at(i)) == 23: This line ensures that the data point is of the correct type (in this case, type 23).
  • dpGet(dataPoints.at(i) + ":_original.._stime", timestampStr);: This retrieves the time of the last change of the data point without the use of archiving, allowing for real-time monitoring.
  • period(currentTime) - period(timestampStr) >= threshold: This checks if the duration since the last change exceeds the defined threshold.
  • dpSet(dataPoints.at(i), FALSE);: This resets the data point if the threshold is exceeded.

Conclusion

This programming trick provides a robust method for monitoring and resetting commands in WinCC OA. By continuously checking the state of data points and ensuring they revert to a safe state after a defined period, you can enhance the reliability and stability of your automation systems.