Loading CSV Files into WinCC Flexible RT Trend Views via VBScript

David Krause12 min read
HMI / SCADASiemensTutorial / How-to
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

Problem Overview

A six-channel analog acquisition script is writing production quality values to a comma-separated file on the engineering station. The next step is to feed that same file back into a WinCC flexible 2008 SP5 Runtime trend view so operators can verify the lot offline without consuming PLC tag memory. The connected controller is an S7-226 on the CP 243-1 Ethernet module, which exposes roughly 10 KB of V (variable) memory and a small data block area, so every byte retained for trending must live on the HMI/PC side.

This article documents the field-proven procedure to:

  1. Read a runtime-produced CSV with the WinCC flexible VBScript host.
  2. Parse the values into an internal tag array sized for the trend window.
  3. Bind that array to the WinCC flexible Trend view without touching PLC memory.
  4. Cover the closely related option of using the native archive system (and the CSV format it expects when you exchange data).
  5. Document a migration path for shops moving to WinCC Unified Runtime.
Target platforms: WinCC flexible 2008 SP5 / SP5 HF1 RT on Windows 7 / Windows 10 (compatibility mode), with a configured connection to an S7-200 station via the "SIMATIC S7 200" channel. The same pattern works on WinCC flexible 2007 RT.

Prerequisites

Item Required Value / Part Source Document
Engineering software WinCC flexible 2008 SP5 Siemens Support portal
Runtime target WinCC flexible Runtime (PC RT) license WinCC flexible manual "Runtime" chapter
CPU SIMATIC S7-226 (CPU 226, 6ES7 216-2BD23-0XB0 or later) S7-200 System Manual
Ethernet CP 243-1 (6GK7 243-1EX01-0XE0) CP 243-1 manual
Script host Windows Script Host 5.6 or newer (built into Win 7+) Microsoft WSH reference
File access FileSystemObject enabled on the RT PC Microsoft Scripting Runtime

The S7-226 family has two relevant memory budgets to keep in mind:

  • Data memory (V memory): 10 240 bytes on the CPU 226 DC/DC/DC; the area shrinks to ~6 528 bytes on the relay-output variant CPU 226XM.
  • Bit memory (M area): 32 bytes; flags are too valuable to use for trend buffering.

Therefore the entire 6-channel CSV trend must be stored on the HMI/PC in internal WinCC tags, with UpdateMode = "On change" or "Cyclic", so the PLC connection is one-shot read/write and never used as the trend buffer.

Architecture Decision Matrix

Three architectural options exist for replaying CSV data in a WinCC flexible trend. They are not equally easy — pick the one that matches the expected refresh interval and the lifetime of the file on disk.

Option Tag Source Refresh Mechanism Limit
A. External CSV → VBScript → Internal tags → Trend PC file (FSO) On click / schedule event 25 000 internal tags per RT project
B. Native Archive (.csv) WinCC flexible tag log Logging cycle Archive file locked while RT is running
C. RDB / OLE DB (TDE / OPC) ODBC data source Event-driven Requires PC-RT license with options
Locking caveat: While a WinCC flexible RT is active, it opens the configured archive *.csv files in shared mode. If you also try to write the file from your own VBScript using FileSystemObject, the second writer receives a permission error or the RT will skip the read. Decide beforehand whether RT or the external script is the single owner.

Option A: Reading the External CSV in RT via VBScript

Option A is the right answer when the production script (running on a different machine or as a scheduled task) generates the .csv and then drops it on a shared folder. The WinCC flexible Runtime becomes a pure read-only client. The boundary is clean and there is no contention with the RT archive system.

Step 1 — Configure internal tag array

  1. In the project tree open Tags → Internal Tags.
  2. Create six tags named Trend01_Ch1 .. Trend06_Ch6, all REAL, default value 0.0, persistence non-retain.
  3. Add one array tag TrendTime of type Date with a number of elements equal to the trend plot length (default 240 samples corresponds to 60 s at 4 Hz).
  4. Add a status tag CSV_RowIndex (Integer) used as the read head.

Trend plots in WinCC flexible ask for either a single tag with multiple time slices or multiple tags evaluated at the same trigger. For option A to work efficiently, use the second variant: declare six internal tags and they form one diagram with six curves automatically.

Step 2 — Drop the CSV into a stable folder

Define a known location such as C:\SCADA\Trend\lot_3071.csv. The path must be reachable from the Runtime service. On a Windows service account model (typical for WinCC flexible 2008 RT), the path needs Modify permission for the service identity; granting Everyone : Read plus the script identity Read is sufficient for option A.

Step 3 — Wire VBScript to the RT

Open Schedules / Scripts → Scripts, create CSV_LoadPlot. The script uses the built-in object model from the WinCC flexible RT scripting host:

Option Explicit
' VBScript inside WinCC flexible Runtime
' Reads a CSV: TimeStamp,Ch1,Ch2,Ch3,Ch4,Ch5,Ch6 (semicolon below)
Dim fso, ts, line, parts, i
Dim fPath
fPath = "C:\SCADA\Trend\lot_3071.csv"

Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(fPath) Then Exit Sub

Set ts = fso.OpenTextFile(fPath, 1, False, TristateFalse)  ' 1=ForReading
i = 0
Do While Not ts.AtEndOfStream And i < 240
    line = ts.ReadLine
    parts = Split(line, ",")
    If UBound(parts) = 6 Then
        SmartTags("CSV_RowIndex") = i
        SmartTags("TrendTime")(i) = CDate(parts(0))
        SmartTags("Trend01_Ch1")(i) = CSng(parts(1))
        SmartTags("Trend02_Ch2")(i) = CSng(parts(2))
        SmartTags("Trend03_Ch3")(i) = CSng(parts(3))
        SmartTags("Trend04_Ch4")(i) = CSng(parts(4))
        SmartTags("Trend05_Ch5")(i) = CSng(parts(5))
        SmartTags("Trend06_Ch6")(i) = CSng(parts(6))
        i = i + 1
    End If
Loop
ts.Close
Set ts = Nothing
Set fso = Nothing

Important runtime rules inside WinCC flexible:

  • SmartTags is the only legal way to touch tags. Plain VB Dim x does not bind to a tag.
  • For array tags the index access uses round brackets: SmartTags("TagArray")(i).
  • Calls to CSng / CDate are required because the parsed string is locale-sensitive. Set the project locale to a fixed en-US if the deployment PC uses German , as decimal separator.
  • The script runs synchronously inside the click or scheduled action. The 240-row read completes well under the default 10 s scheduling tick on any modern CPU.

Step 4 — Trigger the script from a button or schedule

Use a button event Press → Run Script → CSV_LoadPlot. For periodic re-read add a Scheduler task of Update = 60 000 ms. Make sure the rate does not exceed the trend update rate or you will see ghost samples.

Option B: Using the Native Archive System and Its CSV

If on-line logging is acceptable, this is simpler and gives you time-stamping, ring-buffer behaviour, file rotation, and built-in trend binding for free.

  1. Open Tags → Process Tags; mark the six external PLC tags that the S7-226 publishes (or six internal tags if you prefer an intermediary buffer).
  2. Open Logs → Tag Log; create Log_Production, add a TagLog entry per channel, set Logging cycle to 1 s (or to your acquisition script cycle).
  3. Configure Storage: set path (default C:\ProgramData\Siemens\Automation\WinCC flexible\<project>\Logs\), file size (typical 512 KB ring), write mode "Segmented, cyclic".
  4. Bind the trend view to the Log; under TrendView → Properties → Trends select the Log entry rather than raw tags.

The output format is a CSV with the header lines that WinCC flexible writes when exporting a Tag Log:

"Archive name";"Log_Production"
"Filename";"Log_Production.csv"
"User";"RT"
"Date";"31.10.2010"
"Time";"14:22:11"

"TimeString";"Date";"Time";"Ch1";"Ch2";"Ch3";"Ch4";"Ch5";"Ch6"
"31.10.2010";"14:22:11";"31.10.2010";"14:22:11";12.345;12.612;...;11.978
...

The internal separator is ;; the file uses German ("," decimal) when the PC locale is German, or . when the locale is English. Decoders must tolerate either.

File locking: When the Runtime is running, the CSV is open by RT (default shared = none). Trying to overwrite the file from your own script will fail. Stop the Runtime, write the file using Export Tag Log on a Tee or schedule, or copy it with the RT running only if WinCC flexible is configured with the "sequential ring" option enabled (deferred write).

Option C: External CSV Replay Using Graphics Primitives (Legacy Trick)

The original question also raised the possibility of drawing graphics primitives directly. While WinCC flexible does not expose a true vector canvas, a Bar diagram or Animated SVG import can be used for static, low-density charts.

  1. Convert the CSV to an SVG via a one-off script (Python, Excel macro, anything).
  2. Place a Graphic View on the screen; configure GraphicIOField with StateCount = 1.
  3. Drop the SVG into \Grafic\ of the project folder; the RT loads it on next screen cycle.

This option is documented only for completeness — the trend view is faster to configure and supports pan / zoom, which a static SVG does not.

Configuring the Trend View Object

Bind the Trend Control to the six internal tags, not the PLC tags:

  1. Insert the trend from Controls → Trend View.
  2. Properties → TrendSource: select the six tags, one per curve.
  3. Update: 1 s for visual freshness.
  4. TimeRange: 240 s for the 240-sample window. The trend reads SmartTags("TrendTime") as the timestamp index.
  5. Y-Axis: scale each curve individually with min/max matching the sensor range (e.g. 0 .. 25 bar for pressure; 0 .. 100 % for torque).
  6. Enable Buttons = Start/Stop + Clear so the operator can reset the chart after importing a new CSV.

If the curve count exceeds the internal tag array size the trend shows blank stripes; you can never address an index > the configured Number of elements. Size the array exactly once, in design phase, then change only through a full recompile.

Memory-Saving Techniques for the S7-226

The S7-226 has no realistic headroom for an in-PLC ring buffer of 240 floating-point samples × 6 channels = 5760 bytes (5 760 of 10 240 V bytes already used). Use these tactics:

Area Cost per Sample (bytes) Cost for 240 × 6 Mitigation
V data area (REAL) 4 5 760 Reject — exceed budget
V data area (INT × scaling) 2 2 880 Still risky
Internal HMI tags (WinCC flexible) 0 on PLC 0 Use as primary store
Tag log on PC RT HDD 0 on PLC 0 Best for long retention

Additional tricks:

  • Reduce sampling cadence. If the process is mechanical, 1 Hz logging suffices.
  • Drop redundant precision. The PLC scaling can be done in WinCC flexible, exporting only the integer engineering unit to the VBScript.
  • Avoid bit memory (M area) for trending. M is only 32 bytes on the S7-226; use it for handshakes, not data.
  • Pre-aggregate in the WinCC flexible calculation: configure Calculation Tag that averages once per 5 s and write the average into an internal tag fed to the trend.

CSV Format Specification Reference

When an external CSV must be consumed, the recommended column order and formatting is:

TimestampISO; LotID; Ch1_PT1001; Ch2_PT1002; Ch3_PT1003; Ch4_PT1004; Ch5_PT1005; Ch6_PT1006
2024-09-12T08:00:00;LOT3071; 102.456; 23.012; ...

Recommended:

  • Column 1: ISO-8601 timestamp YYYY-MM-DDTHH:MM:SS.
  • Column 2: optional lot ID text.
  • Column 3 .. 8: floating-point engineering units with dot decimal.
  • Line ending: CRLF (Windows).
  • Encoding: UTF-8 with no BOM (avoid double-byte parsing pitfalls).

Verification & Commissioning Checklist

  1. Place a sample 240-row CSV at the agreed path on the RT PC.
  2. Start the WinCC flexible Runtime. Open the screen containing the trend view.
  3. Click the "Load CSV" button. Verify CSV_RowIndex ends at 239 in the diagnostics window.
  4. Confirm the trend view shows 240 samples and the curves move as expected.
  5. Force-close and reopen RT. Because internal tags are non-retain, the trend is empty — this is intentional; load the CSV again.
  6. Stress-test by pointing the script at a 1 MB file. Runtime should complete the parse in < 5 s.
  7. Open the Windows event log. Look for "Component: WinCC flexible RT" and any "Script error 13 Type mismatch" entries; these indicate locale or CSV format drift.
  8. Confirm the CP 243-1 connection count on the PLC: while the script executes, only one TCP connection (PG/HMI) should be active. Trend data must not generate additional connections.

Migrating to WinCC Unified (Recommended Path Forward)

WinCC flexible is end-of-life and not compatible with Windows 10 LTSB 21H2 in many shop-floor configurations. The successor platform SIMATIC WinCC Unified Runtime (PC) handles CSV ingest in a far more forgiving way. The procedure is documented in the official TIA Portal Unified help:

  1. Within the Data tab of the trend control, click Retrieve data → From file → From text/CSV.
  2. Select the existing CSV — the import dialog accepts UTF-8, semicolon, and comma as separators.
  3. Map columns to the trend's data source. The columns are decoded once, persisted in the in-memory history, and shown in the trend control.
  4. For automated jobs use OPC UA Source Alarms & Conditions or the unified Logging Designer; CSV import is the manual equivalent.

The official guide is published at the TIA Portal help center:

For exporting trend data from current systems the AVEVA / Wonderware InTouch-based documentation is also informative:

License note: WinCC Unified PC RT requires a base RT license (16 / 64 / 256 / 100 / 500 / 3 600 tags) plus an "Trends and logs" option for in-process retention. The CSV import path is included in the base RT package.

Common Errors and Recovery

Symptom Root cause Fix
"Script error: permission denied" on OpenTextFile RT service identity lacks modify / read access Grant the WinCC flexible Runtime service account Modify on the folder
Type mismatch (err 13) Decimal separator mismatch (CSV: ., locale: ,) Use Replace(parts(1), ".", ",") under de-DE locale, or set project locale en-US
Trend shows only first sample Internal tag array Number of elements = 1 Resize array in "Properties → Array size" and recompile
Trend completely empty after script Reboot or relog of RT clears non-retain internal tags Either set persistence, or document the on-screen "Reload plot" button
"Windows Script Host: automation error" FSO not registered Run regsvr32 scrrun.dll as Administrator

FAQ

Can a WinCC flexible RT trend be driven by an internal tag array?

Yes. Configure six internal REAL tags or one 240-element REAL array, then bind the trend control's TrendSource to those tags in the Properties dialog. The trend will plot the entire array as 240 historical samples without touching the S7-226 tag memory.

How do I open a CSV from a VBScript in WinCC flexible Runtime?

Use the standard CreateObject("Scripting.FileSystemObject") via SmartTags. The example above opens the file with OpenTextFile(fPath, 1, False, TristateFalse), splits each comma-separated line, and assigns the values element-by-element to the internal array tag.

Why does the Runtime refuse to release its archive CSV while running?

WinCC flexible RT opens the configured archive CSV with an exclusive handle to guarantee write consistency. To overwrite from an external script you must either stop the Runtime or change the archive Write mode in the log settings so it rotates only on size or time and not on demand.

How much V memory does the S7-226 have for trend buffering?

The CPU 226 provides 10 240 bytes of V data and 32 bytes of M memory. A six-channel 240-sample FLOAT trend would consume 5 760 bytes of V — more than half of the budget. Use WinCC flexible internal tags instead and keep the PLC connection read-only.

What is the modern WinCC Unified equivalent of this procedure?

Open the trend control's Data tab and use Retrieve data → From file → From text/CSV. The dialog supports UTF-8 and locale-aware separators. See the further-processing RT data via CSV in RT Unified guide for details.

Back to blog