Switching WinCC PLC Connections via VBScript: S7 MPI Setup

David Krause15 min read
SiemensTutorial / How-toWinCC
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

1. Overview

Engineers running multi-PLC architectures with a single WinCC station frequently encounter the requirement to share one HMI project across several controllers. A typical configuration pairs two or more S7-300 CPUs (for example, 6ES7313-5BF03-0AB0) on a shared MPI or PROFIBUS segment, each driving a similar process unit such as a boiler, pump skid, or repeating machine cell. Maintaining duplicated WinCC graphics and tag structures for each unit doubles engineering effort and creates ongoing maintenance overhead.

The engineering goal is therefore to keep one set of screens, one set of tags, and one set of dynamic links, and to logically retarget those tags at runtime toward the controller that is currently in focus. WinCC does not support a true "connection swap" by directly renaming an active channel at runtime. The supported mechanism is the built-in Redundant Connection feature, which exposes a system tag called @AlternateConnectionAddress. By writing a properly formatted connection string into that tag, the HMI runtime re-resolves the active logical path to the alternate PLC.

This article documents the constraint, the exact procedure, the connection string syntax, the VBScript and C-Script calls required, the diagnostics, and the limitations you must respect when implementing this pattern on WinCC V7 with S7-300 CPUs.

2. The Runtime Constraint You Cannot Avoid

WinCC Runtime cannot rename a configured logical connection (e.g., changing the MPI address of an active channel) directly from a script. The connection name, network type, and station address are compiled into the runtime database at project startup. The supported workaround leverages the Software Redundancy feature in WinCC, which was originally designed for redundant S7 H systems but works for any two physical stations when the runtime is non-redundant.

Hard limit: Direct modification of network parameters (address, rack, slot) of a live WinCC connection from VBScript is not supported. Stopping and restarting WinCC Runtime is the only method to change a primary connection's identity, which is unacceptable for a process that must run continuously.

The Redundant Connection wizard generates a shadow connection and creates the system tag @<ConnectionName>@AlternateConnectionAddress. Writing to this tag tells the runtime which of the two preconfigured paths is currently active. This is the only sanctioned route for runtime connection switching without a project restart.

3. System Architecture: Two-Boiler / Two-PLC Reference Design

Consider the canonical use case: two identical boilers, each controlled by a CPU 313C, supervised by a single WinCC station. The same PLC program, the same screens, and the same tag names are required on both units. Only the underlying MPI station address changes between unit #1 (MPI 2) and unit #2 (MPI 3).

WinCC Runtime Connection Switching Architecture WinCC Station (RT) @Test@AlternateConnectionAddress MPI,2,0,,0,2,02 (or MPI,3,0,,0,2,02) MPI / PROFIBUS Segment 187.5 kbps default PG/OP + S7 Communication CPU 313C #1 (Boiler 1) MPI Address 2, Rack 0, Slot 2 CPU 313C #2 (Boiler 2) MPI Address 3, Rack 0, Slot 2 Active path selected by @AlternateConnectionAddress

The VBScript on the operator button writes the alternate connection string into the system tag, and the runtime resolves the next tag read/write against the new path. From the operator's perspective, the same screen and the same tag names refer to a different physical CPU.

4. Prerequisites

Item Specification Notes
WinCC Version V7.0 SP3 or later (V7.2 / V7.4 / V7.5 also valid) Redundant Connection wizard availability depends on installed options.
WinCC License WinCC RT 2048 / 8192 / 65536 tags (matching project size) Redundancy option not strictly required for this pattern.
PLC Hardware SIMATIC S7-300, e.g. CPU 313C (6ES7313-5BF03-0AB0) Two CPUs with distinct MPI station addresses required.
Network MPI cable (6XV1 830-0EH10) or PROFIBUS DP with MPI protocol Terminating resistors set on both ends (ON at first and last node).
MPI Addresses Distinct addresses, e.g. CPU1=2, CPU2=3 Default MPI address 2 on a factory-fresh CPU.
STEP 7 Version STEP 7 V5.5 or TIA Portal (V13+ for S7-300 with selected CPUs) Used to set MPI address and download hardware config.
Tag Set Identical tag names in both PLCs Process image must match for screens to be reusable.
Computer Properties "Global Script Runtime" checkbox enabled Mandatory; otherwise VBScript actions will not execute.

Reference the official WinCC V7 documentation for the redundancy option and system tag namespace at the Siemens Industry Online Support portal.

5. WinCC Connection String Format

WinCC stores each logical connection as a comma-separated string. The format is fixed per network type. For MPI and PROFIBUS connections, the canonical syntax is:

NetworkType, StationAddress, SegmentID, , ASStation, Rack, Slot
Field Example (MPI,2,0,,0,2,02) Description
NetworkType MPI Transport protocol. Common: MPI, TCP, ISO, PROFIBUS, NamedConnection.
StationAddress 2 MPI/PROFIBUS station address of the target CPU (0-126, default 2).
SegmentID 0 Segment ID; usually 0 on a single-segment MPI bus.
Reserved (empty) Reserved field; leave empty.
ASStation 0 Automation station number (from STEP 7 NetPro).
Rack 2 Rack number on which the CPU is mounted. For S7-300 in a single rack, value is typically 0. Value of 2 may appear when a different S7 station number is configured.
Slot 02 Slot of the CPU within the rack. For CPU 313C, slot 02 is standard.
Verification path: Open WinCC Explorer > Tag Management > right-click the connection > Connection Parameters. The dialog shows the same string. Compare against the live @AlternateConnectionAddress value in a test screen to confirm the format before scripting.

6. Step-by-Step Configuration

  1. Create the primary WinCC connection. In WinCC Explorer, open Tag Management, right-click and add a new driver (SIMATIC S7 PROTOCOL SUITE). Add a new connection, name it (for example, Test), and set its parameters to MPI, 2, 0, , 0, 2, 02 targeting the first CPU 313C.
  2. Define external tags. Create the tag set (boiler status, temperatures, setpoints, etc.) under the Test connection. Use identical names on both PLC projects so the WinCC tag namespace is shared.
  3. Open a graphics page. Launch the Graphics Designer and open any picture in your project. The Dynamic Wizards pane must be visible; if hidden, enable it via View > Toolbars > Dynamic Wizard.
  4. Launch the Redundant Connection wizard. Double-click Create Redundant Connection in the Dynamic Wizards list. Follow the wizard's form navigation. When prompted for the Alternate Address, change only the MPI station address from 2 to 3. All other fields (segment, AS station, rack, slot) remain identical.
  5. Enable automatic switching. In the wizard's summary step, check the Change connection Automatically checkbox so the runtime is allowed to evaluate the alternate path. Save and close the picture.
  6. Enable Global Script Runtime. In WinCC Explorer, right-click the computer name, choose Properties, and on the Runtime tab enable Global Script Runtime. Without this, the script action in step 8 will not execute.
  7. Inspect generated tags. Open Tag Management and expand the Test connection group. The wizard has generated a system tag named @Test@AlternateConnectionAddress of data type Text tag (8-bit character set) or Text tag (16-bit character set) depending on WinCC version. Confirm it appears with the correct name and that it is set to the alternate connection string.
  8. Place a button and write the script. Drop a button onto the graphics screen. In the Event tab, configure the mouse click action (Left Mouse Click, Press, or Release per your standard) and enter the script. Example code is given in section 7.
  9. Compile and test. Save the picture, activate WinCC Runtime, and verify the behavior with the diagnostics in section 10.

For background on the wizard, see the WinCC V7 documentation set linked from WinCC V7 Manuals (entry page) and Siemens Knowledge Base article ID 2987284: https://support.industry.siemens.com/cs/document/2987284.

7. Runtime Scripting: SetTagChar and HMIRuntime Equivalents

The wizard generates C-Script scaffolding. You may keep the C-Script call or convert it to VBScript. The exact call writes the alternate connection string to the system tag.

7.1 C-Script (generated / minimal)

// Triggered on left mouse click of the change-connection button
SetTagChar("@Test@AlternateConnectionAddress","MPI,3,0,,0,2,02");

7.2 VBScript equivalent (preferred for new projects)

' VBS_ChangeConnection_ToBoiler2
' Trigger: Button "Switch to Boiler 2" - Mouse Action > Left Click

Dim sConnString
sConnString = "MPI,3,0,,0,2,02"

HMIRuntime.Tags("@Test@AlternateConnectionAddress").Write sConnString

' Optional: log the change to a diagnostics tag for verification
HMIRuntime.Tags("Diagnostics_ActiveConnection").Write sConnString

7.3 VBScript with safety wrapper

' VBS_ChangeConnection_Safe
' Validates runtime state and confirms the tag write

Const EXPECTED_PRIMARY = "MPI,2,0,,0,2,02"
Const EXPECTED_ALT     = "MPI,3,0,,0,2,02"

Dim oTag
Set oTag = HMIRuntime.Tags("@Test@AlternateConnectionAddress")

If Not oTag Is Nothing Then
    oTag.Write EXPECTED_ALT
    ' Read back after a short delay (50 ms typical)
    HMIRuntime.Trace "Connection switched to: " & EXPECTED_ALT & vbNewLine
Else
    HMIRuntime.Trace "ERROR: @Test@AlternateConnectionAddress not found" & vbNewLine
End If
Encoding caveat: The @AlternateConnectionAddress tag is created as a character set tag. On WinCC versions where the wizard creates an 8-bit text tag, only ASCII characters in the connection string will round-trip correctly. The standard WinCC connection strings are pure ASCII, so this is normally a non-issue, but never write non-ASCII comments or extended characters into this tag.

8. State Machine and Timing

The runtime does not switch instantly. After the script writes the new connection string, the next tag access resolves through the new path. The figure below shows the relevant state transitions.

WinCC Connection State Machine Primary Active MPI,2,0,,0,2,02 Default on RT start Switching 50-200 ms window Tag requests queued Alternate Active MPI,3,0,,0,2,02 Tags re-resolve SetTagChar Next read cycle Fault / timeout Connection Fault Quality Code = Bad

Practical implication: do not chain multiple rapid button presses expecting an instantaneous tag update. Build a 200 ms minimum dwell between the SetTagChar call and the first tag read of the newly active path.

9. Indirect Addressing Alternative (for Many-Boiler Topologies)

If the goal is to support more than two units or to share a deeper set of object properties dynamically, the recommended engineering pattern is indirect addressing using the WinCC @-prefix tag system combined with the import/export tool. The approach decouples the visible tag name from the connection path entirely.

  1. Define each unit's tag set in the WinCC project using a unique prefix: Boiler1_Temp_SP, Boiler2_Temp_SP, and so on. The PLC programs and STEP 7 symbol tables are aligned to match.
  2. On every screen object that must switch context, configure the dynamic property to use a tag-prefix tag, e.g. \Boiler\Temp_SP, where \Boiler\ is a text variable holding the current prefix string ("Boiler1_" or "Boiler2_").
  3. Use the WinCC Import/Export tool (Tag Management right-click > Import/Export All Tags) to duplicate the tag set across prefixes, keeping them in lockstep.
  4. Script the prefix change on the navigation button: HMIRuntime.Tags("BoilerPrefix").Write "Boiler2_". All dynamic properties that reference the indirect tag automatically resolve against the new prefix.

This is more scalable than the Redundant Connection trick for >2 units because it requires no per-unit connection and the project structure remains flat. It is also robust to WinCC version differences, since the @-prefix mechanism has been stable since WinCC V6.

10. Limitations and Edge Cases

Limitation Impact Mitigation
Cannot change network type (MPI ↔ TCP) Switching between physically different networks (e.g., MPI on one unit, PROFINET on another) is not supported by this tag. Use a single network topology for all units, or fall back to indirect addressing.
Connection name cannot be rewritten The logical connection name is fixed at compile time; only the address path switches. Pre-create both connection objects (primary + alternate) via the wizard.
Runtime must not be reloaded mid-swap If the operator triggers the swap and then Runtime is restarted, the alternate becomes primary only after re-running the wizard. Persist the intended target in an internal tag and re-apply on RT start via a startup script.
Tag quality during transition Briefly (50-200 ms) tag quality may return "Bad" for values on the new path. Apply a 200 ms minimum dwell; mask displays during transition.
License impact Two connections count toward the configured connection limit (16 / 64 / 256 by license). Verify your WinCC RT license supports both connections.
Tags must be identical on both PLCs If Boiler 2 has a different data block layout, the WinCC tag namespace will not align. Standardize PLC programs; use a shared FB library for process code.
Archive / alarm tags Archived values and messages switch the active connection on the same cycle as data tags. Be careful with cross-unit time synchronization. Use a single time source (one of the CPUs as master clock or a GPS receiver).
Safety consideration: When you swap active connections on a process that includes write-authority tags (setpoints, mode selectors, actuator commands), the runtime will dispatch the next write to the new CPU. Confirm with operations that no spurious write to the inactive boiler can occur during the transition window. A common protection is to gate the swap behind a permissive tag that asserts "no commands pending" before the switch.

11. Verification and Diagnostics

After implementing the change-connection script, validate with the following checklist:

  1. Tag Management inspection: Confirm the system tag @Test@AlternateConnectionAddress exists, is of character type, and is configured with the alternate string by default.
  2. Graphics Designer test screen: Place an I/O field bound to the @AlternateConnectionAddress tag and a separate I/O field bound to a process tag (e.g., Boiler_Temp). Toggle the connection and verify both values update.
  3. Trace output: Enable the WinCC Runtime trace (Tools > Trace in WinCC Explorer) and confirm the HMIRuntime.Trace lines from your script appear.
  4. Tag simulator test: Use the WinCC Tag Simulator to inject values into a non-critical tag on each CPU and confirm the HMI follows the swap.
  5. Connection diagnostics: Open Tools > Connection Status in WinCC Explorer to monitor which logical path is active. The status indicator must change within 1-2 seconds of the script execution.
  6. Edge case - hot restart: Trigger WinCC Runtime restart and confirm the connection returns to the primary (MPI 2) by default. Re-arm the swap if needed.

For a deep dive on diagnostics and the redundancy fault model, see the WinCC V7 Communication manual on the Siemens support portal: Siemens Industry Online Support.

12. S7-300 CPU 313C Reference Data

The hardware used in the canonical example is the S7-300 CPU 313C, article number 6ES7313-5BF03-0AB0 (later revisions may apply). The table lists the fields relevant to the WinCC connection.

Parameter Value Source
Article number 6ES7313-5BF03-0AB0 Siemens S7-300 module data
Work memory 64 KB code, 64 KB data CPU 313C datasheet
Integrated I/O DI16 / DO16 (24 V DC) + AI5 / AO2 CPU 313C datasheet
MPI interface Yes, 187.5 kbps default CPU 313C datasheet
PROFIBUS DP master/slave Yes (integrated) CPU 313C datasheet
Default MPI address 2 Factory setting
Slot position Slot 2 in rack 0 (typical S7-300 configuration) STEP 7 HW Config default
PG/OP communication Supported on both interfaces CPU 313C manual
S7 communication (PUT/GET) Supported, server only on CPU 313C CPU 313C manual

Confirm current revision status and any successor part numbers via the official Siemens product page on the Siemens Industry Online Support portal before ordering or migrating projects.

13. Field-Commissioning Tips

  • Document the connection strings. Print both the primary and alternate strings and pin them next to the operator panel. The 7-field comma-separated syntax is easy to mistype.
  • Keep one CPU as the time master. When you share a WinCC alarm archive across units, the time source must be unique to avoid scrambled time stamps in the alarm log.
  • Use the same DB layout on both PLCs. If the boiler programs diverge even slightly, the WinCC tag namespace breaks immediately on swap. The simplest safeguard is a shared STEP 7 source library.
  • Test with both CPUs powered on and off. Runtime must behave correctly even if the alternate CPU is unreachable. A connection fault should be raised, not a hang.
  • Apply a 200 ms dwell before reading after a swap. This is empirically the minimum safe window for the WinCC tag cache to invalidate and re-resolve through the new path.
  • Avoid swapping during write actions. Lock operator writes while a swap is in flight; otherwise the write may land on the wrong CPU during the transition window.

Can WinCC change the active PLC connection name from VBScript at runtime?

No. The connection name and its underlying network parameters (address, rack, slot) are compiled into the runtime database at project startup. The only supported runtime mechanism is to preconfigure a primary and an alternate connection via the Redundant Connection wizard and write the alternate's connection string to the system tag @<ConnectionName>@AlternateConnectionAddress. This swaps the active path without renaming the logical connection.

What is the exact syntax of the WinCC MPI connection string?

The standard MPI / PROFIBUS string format is NetworkType, StationAddress, SegmentID, , ASStation, Rack, Slot. For the canonical S7-300 example: MPI,2,0,,0,2,02 targets MPI address 2, segment 0, AS station 0, rack 2, slot 02. The alternate string differs only in the StationAddress field (e.g., MPI,3,0,,0,2,02 for address 3).

Why does my VBScript for the connection switch not run?

The most common cause is that Global Script Runtime is not enabled. Open WinCC Explorer, right-click the computer name, choose Properties, and on the Runtime tab tick Global Script Runtime. The second common cause is binding the script to the wrong event (e.g., configuring the click handler on the wrong mouse event or on the wrong button object).

How long does a connection switch take in WinCC Runtime?

Empirically, the system tag write is synchronous (sub-millisecond) but the tag cache invalidation and first successful read through the new path take 50-200 ms. Apply a 200 ms minimum dwell between the SetTagChar call and the first read of a newly active tag to avoid stale or "Bad" quality values.

Can I share one WinCC project across more than two PLCs?

Yes, but the Redundant Connection pattern is binary (primary + one alternate). For three or more units, use the indirect addressing approach: define a tag-prefix tag (for example BoilerPrefix) whose value is "Boiler1_", "Boiler2_", etc., and bind object properties to \Boiler\.... The Import/Export tool duplicates tag sets across prefixes, keeping them synchronized.

Is this pattern supported on TIA Portal WinCC Professional / Comfort Panels?

The exact @AlternateConnectionAddress mechanism is a WinCC V7 (Classic) feature. TIA Portal WinCC Professional supports connection switching via the Connection Switch function on Comfort Panels and via the redundancy option on WinCC Professional, but the script API and tag namespace differ. Verify the option packages installed before migrating a V7 project to TIA Portal.

Back to blog