WinCC Flexible 2008: Start DataLog from BOOL Tag in VBScript

David Krause15 min read
HMI ProgrammingSiemensTutorial / 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

Overview

In WinCC Flexible 2008 SP3 (and throughout the WinCC Flexible family, including WinCC Comfort/Advanced in TIA Portal), a DataLog is the object that records multiple process tags - temperature, current, pressure, etc. - to a file at scheduled intervals or on demand. The common field requirement is: start the log when a BOOL goes to 1, stop the log when it returns to 0. Operators press a Start pushbutton, a process interlock is released, or a machine mode switches to "Auto-Run"; the HMI should record the batch from that point forward and stop cleanly when the BOOL is cleared.

The built-in LogTag function only writes a single tag/value pair to a log on demand, so it is the wrong tool when the log already references several tags. The correct surface for multi-tag DataLogs is the pair of VBScript functions StartLogging and StopLogging, driven either by a cyclic scheduler that polls the BOOL, or by the BOOL change-event configured directly in the DataLog's trigger properties.

This reference covers both methods, the scheduler pattern, the HMI-restart safety concern that trips up most first implementations, the full function reference, and a field-tested troubleshooting matrix.

Scope. The patterns shown apply to WinCC Flexible 2008, 2008 SP1, 2008 SP2, and 2008 SP3. They also work unchanged in WinCC Comfort/Advanced V11+ after the project is migrated to TIA Portal. VBScript is supported on all WinCE-based panels (TP 177 4", MP 177, MP 277, MP 377) and on PC Runtime; it is not available on the OP 73, TP 070, TP 170A, TP 170B, or OP 77A micro panels - on those, use the DataLog's built-in tag trigger instead.

Prerequisites

  1. Engineering system. SIMATIC WinCC Flexible 2008 SP3 installed (any edition: Standard, Advanced, or the ES2 bundle).
  2. Runtime target. A SIMATIC Panel with VBScript support, or PC Runtime. Confirm VBScript is enabled under Device settings > Runtime > Scripts.
  3. External BOOL tag. A BOOL tag connected to the PLC area that the operator or process toggles to control logging (e.g. HMI_Tag_StartLog mapped to DB10.DBX0.0).
  4. Internal state tag. A BOOL tag internal to the HMI used to remember the last requested state (e.g. HMI_LoggingActive).
  5. Configured DataLog. A DataLog (e.g. Data_log_1) that references the process tags to record. Configure the storage path, file format (CSV or RDB), and the cyclic logging interval under Data Log properties > Storage.

Method 1 - VBScript Called from a Cyclic Scheduler

The most flexible pattern is a short VBScript that checks the BOOL tag and calls StartLogging only on the 0-to-1 transition and StopLogging only on the 1-to-0 transition. The one-shot form prevents redundant state changes that can, on some panel firmware versions, cause log-file rollover glitches and loss of the last partial record.

Step 1 - Configure the Tags

  1. In the project tree, expand Tags and add an external tag HMI_Tag_StartLog of type BOOL pointing to the PLC address (e.g. DB10.DBX0.0). Acquire cycle: 1 s. Connection: the configured HMI-PLC connection.
  2. Add an internal BOOL tag HMI_LoggingActive for state tracking. This tag is read and written by the script and should be marked Persistent if you intend to use the restart-safety Pattern B described below.

Step 2 - Create the Scheduler

  1. In the project tree, right-click Schedulers and select Add scheduler.
  2. Name it LogScheduler. Set the trigger to Cyclic with a cycle of 1 s. The minimum cycle is 100 ms on MP 377 and 250 ms on TP 177 4".
  3. On the Event tab of the scheduler, add a VBScript function and paste the script below.

Step 3 - The VBScript

' One-shot DataLog control from a BOOL tag.
'   HMI_Tag_StartLog : external BOOL, 0 = stop, 1 = start
'   HMI_LoggingActive: internal BOOL, tracks the last requested state
'   Data_log_1       : the name of the configured DataLog

If SmartTags("HMI_Tag_StartLog") = 1 Then
    If SmartTags("HMI_LoggingActive") = 0 Then
        StartLogging hmiDataLog, "Data_log_1"
        SmartTags("HMI_LoggingActive") = 1
    End If
Else
    If SmartTags("HMI_LoggingActive") = 1 Then
        StopLogging hmiDataLog, "Data_log_1"
        SmartTags("HMI_LoggingActive") = 0
    End If
End If

The first argument of StartLogging/StopLogging is the provider constant: hmiDataLog for data logs, hmiAlarmLog for alarm logs, or hmiRecipeLog for recipe logs. The second argument is the configured log name and is case-sensitive.

Step 4 - Verification

  1. Download the project to the panel and start Runtime.
  2. Set the BOOL in the PLC to 1. Within one scheduler cycle the log file Data_log_1.csv (or .rdb) should begin to grow in the configured directory.
  3. Set the BOOL back to 0. New records should stop being appended; the existing file is not deleted - StopLogging pauses the log, it does not erase it.
  4. Watch HMI_LoggingActive on a diagnostics screen. It should track the requested state exactly.

Method 2 - DataLog's Built-in Tag Trigger

For a configuration-only solution with no VBScript, the DataLog object itself can be triggered by a tag change.

  1. Open the DataLog Data_log_1 and select the Trigger property in the inspector.
  2. Add a new trigger of type Tag.
  3. Select the BOOL tag HMI_Tag_StartLog. Set the trigger condition to On value change or, more typically, On value = 1 for the start event.
  4. Under Start/Stop behavior select Start log on rising edge and Stop log on falling edge.

This pattern uses the BOOL directly to start and stop the log without any script. The trade-off is that it only supports simple rising-edge / falling-edge behaviour and cannot apply preprocessing, timeouts, hysteresis, or compound conditions.

Method Comparison

Aspect VBScript + Cyclic Scheduler Built-in Tag Trigger
Configuration effort Scheduler + script DataLog property only
Hysteresis / debounce Possible in script Not available
Restart safety Requires explicit pattern (A, B, or C) Edge-trigger only, no state held
Multiple conditions (AND/OR) Yes, scripted No
Firmware dependence WinCC Flex 2008 SP2 and later All WinCC Flex versions
Debuggability Internal state tag, traceable Hard to trace
CPU load 1 s scheduler + small script None (no scheduler)

The HMI Restart Problem and Three Field-Proven Patterns

The naive script - "if BOOL = 1 then start, else exit" - has a well-known failure mode. If the panel power-cycles while the BOOL is 1, Runtime comes back up with the BOOL still 1 but no log running, because the script has not yet been called. Any code that fires only on the 0-to-1 transition will miss the event entirely. Three patterns address this.

Pattern A - Startup Script Reads the Initial Value

Add a second VBScript bound to the project's Runtime started event. It reads the BOOL once and, if it is 1, starts the log immediately. The cyclic scheduler takes over once Runtime is up.

' Startup script - run once when Runtime starts
If SmartTags("HMI_Tag_StartLog") = 1 Then
    StartLogging hmiDataLog, "Data_log_1"
    SmartTags("HMI_LoggingActive") = 1
End If

Bind this to the project event under Project tree > Runtime settings > Events > Runtime started.

Pattern B - Persistent State Tag

Mark the internal tag HMI_LoggingActive as Persistent. After a power cycle Runtime restores its last value, and the cyclic scheduler sees a consistent state. In the tag's properties, enable Persistent and assign a persistence area (default area 1, up to 32 kB on most panels). This area is battery-backed on panels that support it and is written to flash on graceful shutdown.

Pattern C - Heartbeat from the PLC

Have the PLC write a 1-Hz square wave into a separate BOOL HMI_Log_Heartbeat. Treat any heartbeat as "log active" and any missed heartbeat as "stop". This is the strongest pattern because it tolerates both HMI and PLC restarts, at the cost of some PLC code.

' Cyclic script with heartbeat supervision (assumes 1 s cycle)
Dim tNow
tNow = Now

If DateDiff("s", SmartTags("HMI_Heartbeat_Time"), tNow) > 3 Then
    ' No heartbeat received in 3 s - assume link or PLC is down
    If SmartTags("HMI_LoggingActive") = 1 Then
        StopLogging hmiDataLog, "Data_log_1"
        SmartTags("HMI_LoggingActive") = 0
    End If
End If

If SmartTags("HMI_Log_Heartbeat") = 1 And SmartTags("HMI_LoggingActive") = 0 Then
    StartLogging hmiDataLog, "Data_log_1"
    SmartTags("HMI_LoggingActive") = 1
    SmartTags("HMI_Heartbeat_Time") = tNow
End If

Tune the timeout to roughly three times the heartbeat period. Two seconds is the practical minimum on a 1 s scheduler cycle.

VBScript Function Reference

Function Syntax Purpose
StartLogging StartLogging provider, name Starts a configured log. provider is one of hmiDataLog, hmiAlarmLog, hmiRecipeLog.
StopLogging StopLogging provider, name Pauses an active log. The log file is not deleted.
LogTag LogTag tagname, value Writes a single tag/value pair to the active log. Intended for ad-hoc event records on a single tag, not multi-tag DataLogs.
ClearLog ClearLog provider, name Erases the contents of the named log file. Use with caution on panel flash.
GetDataLogName GetDataLogName(index) Returns the name of the i-th configured data log. Useful for iterating over all logs.
ExportDataLog ExportDataLog provider, name, target Exports a log file to the target path (e.g. a USB stick or network share).
hmiDataLog (constant) Provider identifier for data logs.
hmiAlarmLog (constant) Provider identifier for alarm logs.
Case sensitivity. The provider constant is case-sensitive in older service packs of WinCC Flexible 2008. Always use the lower-case form shown above.

Scheduler Configuration in Detail

Schedulers are project-tree objects that fire a configured event at a fixed interval or on a calendar time. They are global - one scheduler runs regardless of which screen is open.

  1. Right-click Schedulers and choose Add scheduler.
  2. Set Trigger to Cyclic and Cycle to 1 s. The shortest cycle is 100 ms on MP 377 and 250 ms on TP 177 4".
  3. Add an event of type VBScript. Either link to a function or paste a snippet directly.
  4. Scripts in schedulers are global to the project. Avoid naming collisions with screen-level scripts.

For lower CPU load, lengthen the cycle to 2 s or 5 s for batch-oriented logging, but be aware that the response time of the start/stop transition will also grow.

DataLog Storage and File Format

The default storage path and file naming are set in the DataLog's Storage property.

Setting Typical value Notes
Path \Storage Card\Logs\ or \Flash\Logs\ Storage Card is preferred on panels that support it - it survives firmware updates.
File name Derived from DataLog name + extension Default extension is .csv; .rdb for the binary form.
Records per file 5000 - 50000 On rollover a new file is created with a numeric suffix.
Encoding ANSI / UTF-8 Set in the project options before downloading.
Time stamp source HMI local time or UTC Choose UTC on panels that cross time zones.

On PC Runtime the default is C:\ProgramData\Siemens\Automation\Logs\ unless redirected in the project.

Architecture Diagram

BOOL-Triggered DataLog Control Flow PLC BOOL: HMI_Tag_StartLog HMI Tag Connection HMI_Tag_StartLog (BOOL) Cyclic Scheduler (1 s) VBScript one-shot check StartLogging hmiDataLog "Data_log_1" StopLogging hmiDataLog "Data_log_1" DataLog File: \Storage Card\Logs\Data_log_1.csv Appended by the DataLog's own cycle (e.g. 500 ms)

Code Examples

Example 1 - Two-Line Minimum

If SmartTags("HMI_Tag_StartLog") = 1 Then StartLogging hmiDataLog, "Data_log_1"
If SmartTags("HMI_Tag_StartLog") = 0 Then StopLogging  hmiDataLog, "Data_log_1"

This is the form most often quoted in support tickets. It is functionally correct but calls StartLogging on every cycle while the BOOL is 1, which is acceptable on most panels but may produce extraneous log rollover on the MP 277 with firmware < 4.15.

Example 2 - One-Shot with Internal State (Recommended)

The pattern shown in Method 1 above. Recommended for production deployments.

Example 3 - Stop After N Records

' Stop logging automatically after 1000 records
If SmartTags("HMI_LoggingActive") = 1 And SmartTags("HMI_RecordCount") >= 1000 Then
    StopLogging hmiDataLog, "Data_log_1"
    SmartTags("HMI_LoggingActive") = 0
    SmartTags("HMI_Tag_StartLog") = 0  ' optionally clear the request back to the PLC
End If

HMI_RecordCount is logged into the data log on each cycle and can be read back via SmartTags. Add it as one of the tags on the DataLog's tag list if it is not already present.

Example 4 - Mux: Different Log per BOOL Value

Select Case SmartTags("HMI_Tag_StartLog")
    Case 0
        If SmartTags("HMI_LoggingActive") <> 0 Then
            StopLogging  hmiDataLog, "Data_log_idle"
            SmartTags("HMI_LoggingActive") = 0
        End If
    Case 1
        If SmartTags("HMI_LoggingActive") <> 1 Then
            StartLogging hmiDataLog, "Data_log_run"
            SmartTags("HMI_LoggingActive") = 1
        End If
    Case 2
        If SmartTags("HMI_LoggingActive") <> 2 Then
            StartLogging hmiDataLog, "Data_log_fault"
            SmartTags("HMI_LoggingActive") = 2
        End If
End Select

Example 5 - Audit Trail with Operator ID

' Append a single event row whenever logging starts or stops
If SmartTags("HMI_LoggingActive") = 1 And SmartTags("HMI_LastLoggedState") = 0 Then
    LogTag "Operator_ID", SmartTags("HMI_Operator")
    LogTag "Event",       "LogStart"
    SmartTags("HMI_LastLoggedState") = 1
End If
If SmartTags("HMI_LoggingActive") = 0 And SmartTags("HMI_LastLoggedState") = 1 Then
    LogTag "Operator_ID", SmartTags("HMI_Operator")
    LogTag "Event",       "LogStop"
    SmartTags("HMI_LastLoggedState") = 0
End If

Verification Checklist

Check Expected result
BOOL = 0 at Runtime start No log file is opened. HMI_LoggingActive = 0.
BOOL = 1 set from PLC Within one scheduler cycle the log file begins growing.
BOOL = 0 set from PLC Log file stops growing. File is not deleted.
HMI power cycle with BOOL = 1 Within one scheduler cycle the log resumes, provided Pattern A or Pattern B is in use.
DataLog cycle 100 ms Records appear at 100 ms rate, not at the 1 s scheduler rate.
Display HMI_LoggingActive on a process screen Toggles in step with the BOOL transitions.
FTP/SCP export of the CSV File opens cleanly in Excel; timestamps in chronological order.

Troubleshooting Matrix

Symptom Likely cause Fix
Script does not fire Scheduler disabled, or VBScript runtime not licensed on the panel Verify the scheduler is enabled in the project and that the panel supports VBScript (not OP 73, TP 170A, OP 77A).
StartLogging raises "Function not available" Typo in the provider constant; wrong log name Use exactly hmiDataLog and the name as configured in the project (case-sensitive).
Log file is empty after start Storage medium not present or full; path is read-only Check \Storage Card\Logs\ and the configured Storage property of the DataLog.
Log file grows but is not closed cleanly on power loss File held open by Runtime; OS buffer has not flushed Enable Close file on stop in the DataLog properties, or rely on the next start to flush.
SmartTags(...) raises a runtime error Tag name typo, or tag is in a different connection Verify the tag exists in the project and is connected. Use HMI tag names, not PLC addresses, in SmartTags.
Logging does not resume after power cycle One-shot script lost its state Use Pattern A (startup script) or Pattern B (persistent tag) from the restart-safety section.
Records are duplicated after a stop/start cycle DataLog configured with Append but script is restarting mid-file Set Overwrite on the DataLog, or close the file before each new run.
Scheduler CPU load is high Cycle set to 100 ms with a heavy script Lengthen the cycle to 1 s and move heavy logic to event-driven scripts.
BOOL transitions are missed Scheduler cycle too long; PLC toggles faster than the scheduler polls Shorten the scheduler cycle, or switch to the event-trigger method (Method 2).
Tags on the DataLog show stale values Acquire cycle of the source tags is too long Reduce the acquire cycle of the process tags to 100 ms or 500 ms.

Performance and Limits

DataLog performance is bounded by the panel's flash or SD-card write speed. On WinCE panels with internal flash, sustained logging above 200 records per second is not recommended. On panels with a removable SD card (MP 277, MP 377), 1000 records per second is typical. PC Runtime on SSD is limited only by the SQL or CSV write path and the OS file system.

The 1 s scheduler cycle in Method 1 introduces at most 1 s of latency between the BOOL transition and the StartLogging call. If lower latency is required, switch to Method 2 (event trigger) or to a 100 ms scheduler cycle on panels that support it.

Migration to TIA Portal (WinCC Comfort/Advanced)

The same scripting model carries over to TIA Portal V13 and later (WinCC Comfort, WinCC Advanced, WinCC Professional). The VBScript surface, the hmiDataLog provider, and the scheduler model are unchanged. Code that works in WinCC Flexible 2008 SP3 generally works in TIA Portal without modification once the project is migrated. Two notes for the migration:

  • In TIA Portal, schedulers are now under Runtime settings > Events > Scheduled tasks.
  • The migration tool reports script errors as warnings; review each one before downloading the migrated project.

For reference, the WinCC Flexible 2008 SP3 documentation set is available in the Siemens Industry Online Support portal; the WinCC product page at Siemens SIMATIC HMI links to the current TIA Portal documentation and migration guides.

Why does LogTag not work for my multi-tag DataLog?

LogTag writes a single tag/value pair to the active log and is intended for ad-hoc event records on a single tag. A DataLog that references multiple process tags (Temp, Current, etc.) is started and stopped with StartLogging and StopLogging; the per-tag sampling rate and storage location are fixed by the DataLog's own configuration.

How do I make the log start automatically when the HMI reboots while the BOOL is 1?

Use either a startup script that reads the BOOL on Runtime start and calls StartLogging if it is 1, or mark the internal state tag as Persistent so the cyclic scheduler sees a consistent state. The Heartbeat pattern (Pattern C) is the strongest option if both HMI and PLC restarts must be tolerated.

Can I have one BOOL start one DataLog and another BOOL start a different one?

Yes. Use a Select Case on the BOOL value or two separate cyclic scripts, each guarding its own internal state tag. Example 4 in the code section demonstrates the Select Case approach for a 3-state (idle / run / fault) mux.

What is the default storage path for DataLog files on a panel?

On most panels the default is \Storage Card\Logs\ when a memory card is fitted, or \Flash\Logs\ on the internal flash. The path is configurable in the DataLog's Storage property; the file extension is typically .csv for text logs or .rdb for the binary format.

How do I export the logged CSV file from the panel to a USB stick?

Use the ExportDataLog VBScript function, or trigger the export from a button on a process screen configured to call the function. The destination is the external storage path, typically \Storage Card\USB_X61.1\ on panels with a USB port, or a configured network share on PC Runtime.

Back to blog