WinCC Trend Control: TlgGetRulerValueTrend Ruler Value Guide

David Krause14 min read
SiemensTechnical ReferenceWinCC
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Overview

Siemens WinCC Online Trend Control displays live and archived process values as a function of time. Operators can drag a vertical ruler (also called a lineal or cursor) over a trend curve to read the archived value at that exact instant. A recurring engineering task is to mirror the ruler value into other screen elements, such as a bar graph, a numeric I/O field, or a status panel, so the operator sees a consistent snapshot of the data point that the trend is currently pointing at.

The native WinCC C/Basic API exposes a set of Tlg... functions that read the trend control's state and that return values associated with the ruler position. The principal function is TlgGetRulerValueTrend(). This reference covers the function signature, parameter conventions, the difference between Window Title and ObjectName, the behaviour when the ruler is hidden, and the supported approaches for synchronising an on-screen bar display with the ruler readout on WinCC V6.2 SP2 through to V7.x, with a forward-looking note on WinCC Unified Trend Control.

Prerequisites and Environment

  • Engineering Station: SIMATIC WinCC V6.2 SP2 (or later) with the Graphics Designer installed. The same scripts and call patterns apply to V7.0, V7.4, and V7.5; the only differences are in the property sheet layout and the introduction of the ribbon UI in V7.3+.
  • Runtime: WinCC Runtime with a configured project. The Online Trend Control must be embedded into a PDL (Process Designer Library) picture.
  • Tag Logging: At least one archive configured under Tag Logging → Archives with the relevant temperature tag inserted as a process value archive.
  • Licensing: A valid WinCC RT license; the trend control OCX ships with the standard installation and requires no add-on package.
  • Scripting access: C scripting (ANSI C, Project Functions) or VBScript. Both engines can call the Tlg... C-API functions through the Internal → Tlg include path when VBScript is used.
Note. The TlgGetRulerValueTrend function is part of the WinCC C API and is exposed to VBScript through the global Tlg... wrapper. In WinCC V6.2 SP2 it is documented in the WinCC Information System under Working with WinCC → ANSI-C function descriptions → Functions → Tlg. The exact C declaration is not directly available in VBScript Intellisense; it must be invoked as an untyped external function call.

Configuring the Online Trend Control

Before the ruler function can return a value, the control itself must be correctly parameterised. The relevant configuration steps are summarised in the Siemens article How to Configure Trends in Online Trend Control - WinCC V7.4 and apply equally to V6.2 SP2.

  1. Open Graphics Designer and drop an Online Trend Control from the Controls palette into your picture.
  2. Open the configuration dialog and switch to the Curves tab.
  3. Use the + key to add as many curves as the picture needs. For a temperature profile, one curve per physical sensor is typical.
  4. Define the order of curves with the Up and Down keys. The order here is the same order the nTrend parameter in TlgGetRulerValueTrend will index.
  5. On the General tab, set the update rate and archive source. The ruler acts on whichever data is currently visible; if the control is displaying the live (online) buffer, the ruler reads live values; if it is in Back.../archive mode, the ruler reads archived values.
  6. Click OK and then open Object Properties → Control Properties → Miscellaneous → Caption. The text in this field is the value the C API treats as the Window Title of the control. This is the parameter the script must pass as lpszTemplate; it is not the picture-level object name.
Critical distinction. The WinCC Online Trend Control exposes two identifiers:
  • ObjectName – the variable name used by the project navigator and the C script's GetObject lookups. Defaults to Control1, Control2, …
  • Window Title / Caption – the user-visible header on the control. Defaults to TrendControl_01, but is fully editable and is the value TlgGetRulerValueTrend resolves.
A common failure mode is to pass the ObjectName as lpszTemplate. The function then returns the value previously held when the ruler was last active (or zero on a freshly opened picture), with no error code emitted. Verify which identifier the script is using before debugging the call further.

TlgGetRulerValueTrend Function Reference

The function reads the value of a single curve at the current ruler position. Its C signature is:

double TlgGetRulerValueTrend(const char* lpszTemplate, int nTrend);
Parameter Type Meaning
lpszTemplate const char* Pointer to the Window Title of the trend control. Must be null-terminated. Case sensitive.
nTrend int Zero-based index of the curve as ordered on the Curves tab. 0 = first curve, 1 = second, etc.

Return value: double. The numeric value of the curve at the time the ruler is positioned. If the ruler is currently off (hidden), the function returns the value of the curve at the time the ruler was last switched off. If the ruler has never been activated in the lifetime of the picture, the function returns 0.0 for numeric tags and the empty string for string tags.

On a WinCC V6.2 SP2 engineering station, the corresponding VBScript invocation uses the same prototype with a string literal as the first argument:

RulerValue = TlgGetRulerValueTrend("TrendControl_01", 0)

The function has no separate error code channel; the caller must validate the returned value against a sentinel (e.g. compare against the previous reading) and decide whether the value represents fresh or stale data.

Implementing Ruler-Triggered Bar Updates in C

The cleanest implementation is a C Project Function that runs on a trigger event. Wire the function call to a C-action on the bar graph so that the bar refreshes whenever the script is fired. The trigger should be either a one-second timer or, preferably, a Triggered event on the trend control if the project exposes one. The minimum C implementation is:

// Project function: GetRulerValue_BarFeed
#include "apdefap.h"
double GetRulerValue_BarFeed(void)
{
    return TlgGetRulerValueTrend("TrendControl_01", 0);
}

Bind the bar's Process Value property to GetRulerValue_BarFeed() through a tag prefix in the Dynamic Wizard. The bar will then mirror the trend's first curve at the ruler position once the picture has been opened and the user has activated the ruler at least once.

Implementing Ruler-Triggered Bar Updates in VBScript

WinCC V6.2 SP2 also supports VBScript actions on the same OCX. A VBScript equivalent of the C function above is:

' VBS action attached to the bar object (event: "Property Change" or a 1s timer)
Dim dblRuler
dblRuler = TlgGetRulerValueTrend("TrendControl_01", 0)

If IsNumeric(dblRuler) Then
    BarGraph.ProcessValue = CDbl(dblRuler)
Else
    BarGraph.ProcessValue = 0
End If

The IsNumeric guard is mandatory when the underlying tag is a string type, because the function returns a Variant of VT_BSTR for string-valued trends; coercing it to CDbl would raise a type mismatch error.

Detecting Ruler State On/Off

The principal limitation of TlgGetRulerValueTrend is the absence of a companion call that returns the visibility state of the ruler. If the operator hides the ruler (or it has never been activated), the function silently returns the last held value. Two field-proven workarounds are documented below.

Workaround 1 – Suppress the Native Ruler Button and Drive It Externally

Hide the integrated ruler button in the trend control's toolbar and place a custom button on the picture, wired to TlgTrendWindowPressLinealButton. The button toggle is then used to write an internal tag (e.g. TrendRulerActive as a binary tag) which the bar-driving script can read to decide whether the value is fresh.

  1. Open Object Properties → Toolbar → Button Configuration and disable the Ruler (lineal) entry. The toolbar's visible buttons shrink accordingly.
  2. Add a WinCC button to the picture and assign a C action that calls TlgTrendWindowPressLinealButton("TrendControl_01"); and then flips the internal tag:
// C action on custom button click
TlgTrendWindowPressLinealButton("TrendControl_01");
SetTagBit("TrendRulerActive", (GetTagBit("TrendRulerActive") == 0));
  1. On the picture's Open event, set the internal tag to 0 so the state cannot leak from a previous navigation:
// C action on picture open
SetTagBit("TrendRulerActive", 0);
  1. In the bar-driving function, gate the assignment:
if (GetTagBit("TrendRulerActive") == 1)
    BarGraph.ProcessValue = TlgGetRulerValueTrend("TrendControl_01", 0);
else
    BarGraph.ProcessValue = GetTagFloat("Live_Temperature"); // fallback to live

Workaround 2 – Detect Ruler Activity by Polling the Value

When the project cannot add custom toolbar buttons, a coarse but functional heuristic is to compare successive samples of TlgGetRulerValueTrend and treat a value change as evidence of ruler activity. The approach is sensitive to genuinely changing process values, so it is best combined with the TrendRulerActive internal tag from Workaround 1 for confidence.

TlgGetRulerTimeTrend for Time-Based Lookups

The companion API is TlgGetRulerTimeTrend, which returns the time stamp at the ruler position:

double TlgGetRulerTimeTrend(const char* lpszTemplate);

The return is a double representing the time in WinCC's internal time format (days since 30.12.1899, including the fractional day). It is the same encoding returned by GetTagValue for internal time tags, so the value can be forwarded directly to archive queries. The typical use case is to combine it with TlgGetRulerValueTrend to populate a bar chart and its associated time stamp label simultaneously.

double dblTime, dblValue;
dblTime  = TlgGetRulerTimeTrend("TrendControl_01");
dblValue = TlgGetRulerValueTrend("TrendControl_01", 0);
SetTagFloat("Ruler_Time",  dblTime);
SetTagFloat("Ruler_Value", dblValue);

For string-format display, convert the double through SysTime / Format in the C action before writing to a text I/O field.

Linking Bar Graph Displays to Ruler Values

Two integration patterns are common in production screens.

Pattern Mechanism When to use
Live mode Bar graph bound to the live process tag. Independent of the trend control. When the operator is not actively inspecting history; the bar always shows the current value.
Archive mode Bar graph driven by a C/VBS action that calls TlgGetRulerValueTrend. Triggered on a 1 s timer or on a picture-level hotkey. When the operator is reviewing past temperature profiles and the bar should follow the ruler.
Operator-selectable Two-state selector on the picture switches the bar's source between the live tag and the ruler-driven function. A status bit drives the binding via the Dynamic Wizard. Recommended for production screens: the operator can flip between "actual" and "at-ruler" without losing the bar's context.

The third pattern is the one most commonly shipped in WinCC V6.2 SP2 retrofit projects, because it preserves the live display as a safety baseline while still giving the operator the historical read-out.

Performance and Triggering Considerations

  • Timer resolution. A 250 ms timer is sufficient for a bar graph that drives a single curve. Using a 100 ms or faster timer does not improve the bar's visual smoothness because the bar's own refresh is rate-limited by the picture cycle.
  • Multiple curves. When bars are linked to several curves, call TlgGetRulerValueTrend once per curve. Each call walks the trend's internal value cache and is O(log n) in the number of archived samples. The function is safe to call from a 1 Hz timer in projects with up to ~50 curves.
  • Picture navigation. Always reset the TrendRulerActive internal tag in the picture's Open event. Stale tags from a previously open picture will otherwise leave the bar locked on a stale value.
  • Cross-picture trends. If the same trend control instance is embedded in more than one picture, the function returns values from the currently visible instance only. The lpszTemplate must match the active picture's control, otherwise the call silently returns the cache value from the most recent instance.

Troubleshooting Matrix

Symptom Likely cause Diagnostic step Resolution
Function returns 0.0 immediately after picture opens lpszTemplate is the ObjectName, not the Caption Right-click the trend control → Object Properties → Control Properties → Caption; verify against the string in the script Change the script argument to match the Caption field exactly, including case
Function returns the previous value after the ruler is hidden No companion ruler-state API; the function holds the last active value Check whether the operator toggled the ruler button on the control's toolbar Implement Workaround 1 (suppress native ruler, drive it from a custom button, gate via internal tag)
Type mismatch error in VBScript Trend is bound to a string tag; CDbl() cannot coerce the result Wrap the call in IsNumeric() and add a string branch Use CStr() for string trends; reserve CDbl() for numeric trends only
Bar jumps to 0 the moment the picture opens Picture-open event does not initialise the bar; function returns 0.0 until the ruler has been activated Add a debug message in the C action to print the returned value to the diagnostics window Set an initial value in the picture's Open event; gate display on TrendRulerActive
Wrong curve's value is shown on the bar nTrend index does not match the curve's order on the Curves tab Open the configuration dialog and count curves from the top, zero-indexed Adjust the index; remember that the order is set by the Up/Down keys, not by the underlying tag names
Ruler value is correct, but time stamp on the adjacent label is wrong TlgGetRulerTimeTrend returns a WinCC double-time that is forwarded as raw to a text field Print the raw double and check it against the ruler's visible time Format the double with a Time tag type or with the Format C function before display
Function works in the ES, returns 0 in RT Script access rights differ between ES and RT; the script may be marked not executable in RT Open the script in Graphics Designer and check the Runtime flag Set the script's Runtime attribute to Yes and re-transfer the project

Cross-Version Migration: V6.2 SP2 to V7.x and Unified

WinCC V6.2 SP2, V7.0, V7.4, and V7.5 share the same TlgGetRulerValueTrend C signature; scripts written for V6.2 typically compile and run unchanged on V7.x, provided the project is migrated via the WinCC Project Migrator. The Visible differences between the versions that affect this scenario are:

  • Toolbar configuration – V7.3 introduced a ribbon-style dialog. The Ruler button is still under the Toolbar tab but the icon set differs.
  • Object naming – Picture Window references in V7.4 are case-insensitive on the PDL level but remain case-sensitive at the C API level. Verify case after migration.
  • WinCC Unified (TIA Portal V17+) – The OCX is replaced by an HTML5-based trend control. The TlgGetRulerValueTrend C API is not available. The Unified equivalent is exposed through JavaScript in the runtime API as TrendControl.GetRulerValue(trendIndex). Operators drag a cursor across the trend; the function returns the value at the cursor position in the same units as the configured trend. For migration projects, the legacy C function must be re-implemented in JavaScript and the VBScript wrapper must be removed. Refer to Trend control (RT Unified) for the runtime configuration model.
Backward compatibility. A common retro-fit pattern is to keep the V6.2 SP2 picture untouched in WinCC Runtime and to develop a parallel Unified picture for the new TIA Portal runtime. Both pictures can coexist in a plant where the HMI server still runs V6.2 SP2 on Windows 7 and a new Unified server runs on a modern IPC. The TlgGetRulerValueTrend logic does not need to be ported until the legacy picture is retired.

Best Practices Summary

  • Always pass the Window Title (Caption), never the ObjectName, to TlgGetRulerValueTrend.
  • Track ruler visibility through a custom button and an internal tag; do not rely on the function to signal whether the ruler is active.
  • Reset the ruler-state tag in the picture's Open event.
  • Pair the value call with TlgGetRulerTimeTrend when the bar graph is part of a larger archive view.
  • Wrap VBScript calls in IsNumeric() when the trend carries a string-typed tag.
  • For new projects, evaluate WinCC Unified before committing to V6.2 SP2; the JavaScript API of the Unified trend control is more idiomatic and exposes the ruler state directly.

FAQ

What does TlgGetRulerValueTrend return when the ruler is turned off?

It returns the curve's value at the time the ruler was last active, not zero. To detect the off-state, drive the ruler from an external button via TlgTrendWindowPressLinealButton and write the toggle to an internal tag (e.g. TrendRulerActive) that the bar-driving script reads.

Is the first parameter the ObjectName or the Caption?

The Caption. Open Object Properties → Control Properties → Miscellaneous → Caption on the trend control and pass that exact string, case sensitive, as lpszTemplate. Passing the ObjectName causes the function to silently return 0.0 on a fresh picture and the last held value afterwards.

How do I read multiple curves from the same trend window?

Call TlgGetRulerValueTrend once per curve and increment the nTrend index. The index order is the same as the curve order on the Curves tab of the configuration dialog, modified by the Up/Down keys.

Can the same function be used in WinCC Unified (TIA Portal)?

No. The TlgGetRulerValueTrend C API is not available in Unified. Use the JavaScript runtime API on the TrendControl object instead, where the equivalent is TrendControl.GetRulerValue(trendIndex). The two APIs are not binary compatible; the legacy C/VBS wrapper must be re-implemented.

Why does the function return a time-like value for the second parameter call?

It does not; you are likely calling TlgGetRulerTimeTrend, which returns a WinCC double-time (days since 30.12.1899). The two functions share the same lpszTemplate argument but the second returns a time stamp rather than a process value. Format the double before display.

Back to blog