IMDTouchKnowledge BaseShop main store

Explainer

Can an HMI Script Count PLC Bit Changes Reliably

HMI and touchscreens Published 7 min read

Short answer

Yes, but only as an approximate, non-critical count. A runtime script sees the tag at discrete reads, so a bit that turns on and off between reads is missed; reported field experience treats signals held 2 s per state as reliable and anything below 500 ms as unusable. Any count that matters belongs in the PLC, displayed on the HMI.

On this page
  1. What the runtime actually sees
  2. The timing numbers you can design to
  3. Define the edge before counting
  4. Worked example: a bit at the reported floor every cycle
  5. Writing the script when the count is non-critical
  6. What each symptom tells you
  7. Where the counter belongs
  8. Questions asked

Yes, an HMI runtime can count a PLC binary tag's changes with a script, but only as an approximate total, never as a number the process depends on. The runtime reads the tag at discrete times through its communications path, and a bit that turns on and back off between two reads never arrives at the panel: both reads return the same value. Field experience with this exact method sets a reliable state duration and a hard floor well below it, detailed in the timing section. The design rule is older than any script — the HMI is the visualization layer and the PLC is the logic layer — so a count that matters is counted in the PLC and displayed on the panel.

What the runtime actually sees #

A script does not watch the bit. It asks the communications driver for a tag value at discrete moments, and everything between those moments is gone. If the bit goes 0 → 1 → 0 between two reads, both reads return 0 and the runtime records no change. That is a sampling limit, not a scripting defect: no trigger and no code recovers an event that never reached the panel.

The spacing of the reads is not fixed either. The PLC services HMI traffic as a lower-priority task than its own logic, so the interval between reads stretches when the scan is busy, and stretches again when the panel polls a large tag list. This is why the same script can pass on the bench and miss on the plant floor.

WinCC-family runtimes start a script from a cyclic timer, an acyclic timer, or a tag change; in WinCC Flexible 2008 the tag-change route is the tag's "Change Value" event in the Events tab of Tag Management. All three start the code from values the runtime received. The choice changes when the script runs, never what the panel can see.

The timing numbers you can design to #

The working numbers come from field experience with this method, not from a vendor timing specification, and they are the ones to design to. The reporting engineer would not use an HMI-side counter below 500 ms of signal duration or toggle period at the very lowest, and found signals held on for 2 s per state reliable. What moves the boundary is network speed, the size of the tag list the panel polls, PLC load, and how long each state is held.

The design rule follows directly: an HMI script count is defensible only for a non-critical total on a signal held at or beyond that reliable state duration; below the reported floor the method is off the table; between the two it is a bet, not a design. No timing makes an HMI count operational — if the number feeds sequencing, alarms, or records, it belongs in the PLC at any pulse width.

Define the edge before counting #

A binary tag has two states, so "how many times it changed" needs a definition before any code is written. A complete on/off cycle contains two transitions: a rising edge, 0 → 1, and a falling edge, 1 → 0. If the operator wants to know how many times the signal turned on, count rising edges — one per cycle. If the operator wants state changes, count both edges — two per cycle.

The classic bug is counting the state instead of the edge. A script that increments whenever the bit reads 1 climbs on every execution while the bit stays on, so the total grows with script frequency instead of with the process. The fix is structural: compare the current value against a saved previous value, increment once when they differ, then update the saved value exactly once per evaluation.

Worked example: a bit at the reported floor every cycle #

Inputs, each with its source:

  • On time per cycle: 0.5 s — the shortest signal duration the field report supports at all, converted from the floor stated above.
  • Off time per cycle: 0.5 s — assumed equal to the on time; a symmetric pulse is the typical case.
  • Reliable state duration: 2.0 s — the same field report's reliable figure, in seconds.
  • 1 min = 60 s — a conversion, no source needed.

Rule in words: the runtime can count only the transitions it observes, so convert the state duration into a transition rate and compare it with the reliable-per-state mark.

Arithmetic, written out:

  • Cycle time = 0.5 s + 0.5 s = 1.0 s.
  • Full on/off cycles per minute = 60 s ÷ 1.0 s = 60.
  • Transitions per minute = 60 cycles × 2 = 120 counting both edges, or 60 × 1 = 60 counting rising edges only.
  • Margin check: 2.0 s ÷ 0.5 s = 4, so each state lasts a quarter of the duration the field report calls reliable. At that reliable mark the cycle is 2.0 s + 2.0 s = 4.0 s, which gives 60 ÷ 4.0 = 15 cycles per minute.

Result: the signal runs at 60 full cycles, 120 transitions, per minute — four times the rate of the 15 cycles per minute that the reliable per-state signal represents.

Decision: an HMI script count is defensible at 15 full on/off cycles per minute or slower, which is the reliable state duration or longer, and only for a non-critical total. This floor-level signal sits exactly on the reported limit with no margin, so its counter goes in the PLC.

Writing the script when the count is non-critical #

  1. Fix the edge definition first: rising edges only, or both transitions. Everything downstream depends on it.
  2. Hold the saved previous state and the accumulator in internal tags — the documented pattern is an internal 32-bit unsigned integer with an initial value of zero, written from the VBScript.
  3. When the session starts, initialize the saved state from the current tag value, and do not count that first read as a transition.
  4. On every execution, read the tag; if the value differs from the saved state, increment once — for rising-only counting, only when the new value is 1 and the saved value was 0 — then write the current value into the saved state.
  5. Start the script from a cyclic timer or from the tag's "Change Value" event in the Events tab of Tag Management on WinCC Flexible 2008; both routes see only values the runtime received.
  6. Prove it before trusting it: drive a known number of transitions at production timing and normal communications load, and compare the display against a reference count. One missed event fails the method for that signal, and the counter moves to the PLC.

What each symptom tells you #

Symptom on the totals display Cause Decision
Count reads low Transitions happened between runtime reads Move the counter to the PLC
Count climbs while the bit stays on Script counts the state, not the edge Compare against the saved previous value, update it once per evaluation
Count is half or double the expected total Edge definition does not match the requirement Choose rising-only or both edges and re-verify against a reference
Count returns to zero after a restart The internal tag restarts from its initial value Keep the running total in the PLC if it must survive restarts

Where the counter belongs #

Counting edges is native work for the control layer. IEC 61131-3 defines the count-up function block, CTU, that every PLC programming environment carries, and it executes against the scan with no dependence on HMI read timing. The panel then does the one thing it is built for: showing the number.

Split the decision by consequence, not by timing alone:

  • Non-critical total, each state held at or beyond the reliable duration, restart loss acceptable — an HMI script count is defensible.
  • Sequencing, alarms, maintenance totals, production records — a PLC counter, always, at any pulse width.
  • Anything with a safety function behind it — PLC, and the HMI count has no role at all: the panel is a supervisory device with no rated safety function, and its total can miss events silently.

Once the counter lives in the PLC, the remaining traps are on the display side. Displaying a PLC counter current value with a signed HMI tag and why an HMI counter display jumps from 9 to 16 cover the data-type mistakes that make a correct count read wrong on the panel.

Questions asked

Does starting the script from the tag's change event instead of a timer catch more transitions?

No. A change event fires only when the runtime receives a new value over the communications path, so a pulse that completes between two reads fires nothing, exactly as with a timer. What the change trigger does buy is economy: the script runs once per observed change instead of on every timer tick, which matters on a panel already polling a large tag list.

Why keep the count in an internal tag instead of a variable inside the script?

VBScript local variables exist only for the duration of one execution, so a count or a saved previous state stored there is rebuilt every time the script runs. An internal tag holds its value across executions for the whole runtime session, and a numeric display can be bound to it directly without another script writing to the screen.

The count matched in the office but misses events on the plant floor. Why?

Bench testing usually runs the panel with a light tag list and an idle PLC, so reads arrive close together. In production the same panel polls every tag on the screen, the PLC services that traffic only in its spare time, and the gaps between reads stretch. Retest at production timing and full communications load; a single missed event means the method has failed for that signal.

Are the reported floor and reliable-duration figures guaranteed limits?

No. They are one engineer's reported field experience with this method over an HMI communications link, not a vendor timing specification, and they are used here as a working rule, not a promise. The only guaranteed count is the PLC counter, evaluated once per scan, independent of any read timing on the panel side.

Keep reading

  • Troubleshooting

    HMI numeric input does not change FX2N timer preset

    A numeric input works only when it writes the exact D register the timer or counter instruction reads as its preset. Open the device monitor, enter a known value, and watch that register: if it does not move, fix the link, driver or address; if it moves but the delay stays fixed, the rung still reads a K constant or another register — change the preset operand to the register, mind the timer's time base, then test two settings before production.

    HMI and touchscreens

  • Comparison

    TIA Portal vs STEP 7 plus WinCC hardware configuration

    Configuring the same PLC and HMI in TIA Portal instead of STEP 7 plus WinCC needs no extra PLC, panel, module or network hardware — the cabinet stays exactly as it is. The difference is where devices, networks and tags are configured: one integrated project instead of two. The one hard limit: S7-1200 and S7-1500 CPUs engineer only in TIA Portal, and CPU firmware sets the minimum release, so a V2.9 CPU needs TIA Portal V17 or higher.

    HMI and touchscreens

  • How-to

    Display large PLC array history on an HMI with a buffer page

    Put a display buffer in the PLC sized to one visible page (10 rows here) and let the HMI show only that. On each page change the PLC clamps a start index and copies the requested slice of four 5000-entry test arrays plus a timestamp array into five 10-element buffers, so 1000 exposed entries become 100 pages and 50 HMI-facing values instead of one object per tag.

    HMI and touchscreens

Searching Vijeo Designer Recipes by Label Code