WinCC ComboBox: Writing Text Values to CFC Tags via C Script

David Krause10 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

WinCC ComboBox: Writing Text Values to CFC Tags via C Script

Siemens WinCC operator stations in PCS 7 plants frequently use a ComboBox control on a faceplate to let the operator pick a process category (for example a charge identifier, recipe code, or unit number) and write that value to a CHAR input of a CFC (Continuous Function Chart) running in the AS (Automation Station). Because CFC chart I/Os are not exposed as flat tag names, the standard flat SetTagChar("TagName", value) pattern fails silently. This reference documents the correct WinCC C script, the dot-notation required for CFC inputs, the Tag Management prerequisites, and the faceplate cache bug that masks successful writes during commissioning.

1. Prerequisites

Component Requirement
WinCC version WinCC V7.0 SP3 or later (V7.2, V7.3, V7.4, V7.5) on the OS / Engineering Station
PCS 7 environment PCS 7 V8.0 or later with CFC chart generated in the S7 PLC program (AS)
PLC tag attribute CFC input "Charge" exposed with operator-controllable attribute (S7_m_c / operator authorization)
WinCC Tag Management Tag "Charge" (or chart-instance prefixed tag) added with correct AS-OS connection
Graphic object WinCC ComboBox placed on the base picture or faceplate
C scripting engine Global C actions enabled, runtime license present
Note: The C scripting functions covered here (SetTagChar, GetTagChar) are part of the WinCC V7 API. They are not available in WinCC Professional (TIA Portal). For TIA Portal HMI tag scripts the equivalent is the SetTag / GetTag VBScript calls or the new C#/VB script environment in TIA V17+.

2. Anatomy of the SetTagChar C Script

The WinCC C scripting API exposes a family of typed tag access functions. The most relevant for ComboBox-to-CFC wiring are listed below.

Function PLC Data Type Return Use
SetTagChar(LPCTSTR TagName, CHAR Value) CHAR (1 byte signed) BOOL (TRUE = success)
GetTagChar(LPCTSTR TagName) CHAR CHAR
SetTagByte(LPCTSTR TagName, BYTE Value) BYTE (unsigned) BOOL
SetTagCharWait(LPCTSTR TagName, CHAR Value, DWORD Timeout) CHAR DWORD (error code)

For a ComboBox that selects a single ASCII code (for example the operator picks '1', '2', or '3' to represent a charge state), SetTagChar is the correct call. The function name is case-sensitive in older WinCC builds; misspelling it as SetTagchar compiles without error but never writes the tag.

// Minimum working example
BOOL bRet = SetTagChar("Charge", 'A');
if (!bRet)
{
    // Optional logging through internal tag or trigger tag
    SetTagBit("ScriptError", TRUE);
}

For ComboBox entries that exceed a single character (full text strings), the CHAR variant is not appropriate. Use the string-aware path described in Section 9.

3. Configuring the ComboBox Event

The WinCC ComboBox control (MFC CComboBox class in the underlying Graphics Designer) exposes a small set of events that can be wired to a C action.

  1. Open Graphics Designer and select the ComboBox on the faceplate or base picture.
  2. Right-click the control and choose Properties > Events.
  3. Under Miscellaneous locate the event Selected text / Change (WinCC V7.4+) or the older SelectionChanged event (V7.0–V7.3).
  4. Right-click the event and select C action.
  5. Add the function call. The value parameter is provided automatically by the event interface as the current text of the ComboBox.
// C action on the "Change" event of a ComboBox named cmbCharge
CHAR cSel = (CHAR)cmbCharge.Text;  // first byte only
SetTagChar(".Charge", cSel);
Important: The system variable value available in older WinCC V6 / V7.0 build actions is a string. Casting it to CHAR truncates everything after the first byte. If the CFC input is actually a STRING type, do not use SetTagChar — see Section 9.

4. CFC Tag Addressing: The Dot Notation

CFC charts are compiled into the S7 program as FB (Function Block) instances. When a CFC chart named CHRG_CTRL contains an input named Charge, the PLC symbol is DBx.CHARG_CTRL.Charge. WinCC imports that symbol as a structured tag and requires a leading dot when accessed from the scripting API:

SetTagChar(".Charge", cSel);  // Correct — accesses CHRG_CTRL.Charge

The dot tells the WinCC runtime that the tag is a member of the currently open faceplate / picture. Without the leading dot the runtime searches the global tag namespace and returns the failure code silently.

Call Resolution Result
SetTagChar("Charge", v) Global tag named "Charge" Failure (tag not found)
SetTagChar(".Charge", v) CFC input Charge in current picture Success
SetTagChar("CHRG_CTRL.Charge", v) Fully qualified CFC instance Success when picture is the chart host

For multi-instance CFC blocks the path may be deeper, e.g. SetTagChar("UNIT1.CHRG_CTRL.Charge", v). Always confirm the full instance path in the WinCC Tag Management tree view before assuming the short form will resolve.

5. WinCC Tag Management Requirements

Even with the correct dot-notation call, the tag will not write if any of the following conditions are missing.

  • Tag must exist in WinCC Tag Management on the OS. It is not enough for the tag to exist in STEP 7 / PCS 7; WinCC must have imported it via the S7 connection configured under Tag Management > SIMATIC S7 PROTOCOL SUITE > TCP/IP (or PROFIBUS for older systems).
  • Operator-controllable attribute on the CFC input must be set. In the CFC editor, open the block I/O properties, switch to the Operator tab, and enable Operator-controllable. This sets the S7_m_c attribute on the underlying tag.
  • Authorization on the Operator Station must allow the user level that was assigned to the tag in CFC. Operator level 5 (Process controlling) is the typical minimum.
  • No Read-only tick in Tag Management > Properties of the tag. PCS 7 sometimes marks imported CFC I/Os read-only when they have not been declared operator-controllable in the AS project.

Verifying these conditions can be done without writing C code: open the tag in the WinCC Tag Simulator or use Start > SIMATIC > WinCC > Tools > WinCC Tag Simulator, then manually drive the value. If the simulator cannot write the tag, the C script will not be able to either.

6. The Faceplate Cache Issue

A particularly frustrating failure mode is the silent no-op: the C action compiles, runtime executes it, no error appears in the diagnostics window, but the CFC input value never changes. In many field cases the root cause is the WinCC faceplate instance cache.

When a faceplate is opened, WinCC instantiates a copy of its scripts and tags. Editing the C action and saving it does not always reload the cached instance — the runtime continues to use the old script object already bound to the open faceplate window. The fix is:

  1. Close every open instance of the faceplate on the OS runtime.
  2. Stop and restart Graphics Runtime (or trigger a Reinit via the WinCC Explorer > Computer > Properties > Graphics Runtime).
  3. Open the faceplate afresh and exercise the ComboBox.

To avoid this loop in commissioning, enable Activate Global Script Runtime and turn on the Reload Scripts flag in the WinCC Explorer during development. The flag forces the runtime to recompile C actions each time a picture is loaded.

7. Verification Procedure

After the script is wired, verify end-to-end with the following steps before declaring the work complete.

  1. Tag Management test — In the WinCC Explorer, navigate to Tag Management > [S7 connection] and confirm the Charge tag is listed with the correct structure (dot prefix, instance path).
  2. Tag Simulator test — Use the WinCC Tag Simulator to force-write the tag. The CFC input should update immediately (visible in the CFC online view in STEP 7 if the AS connection is online).
  3. GDI diagnostics — Open WinCC Explorer > Tools > GDI Diagnostics and look for "Tag write failed" entries.
  4. Script trace — Add a temporary internal bit tag and toggle it at the end of the C action. Watch the bit change when the ComboBox event fires; if the bit changes but the Charge tag does not, the script is running and the problem is downstream (tag management or PLC side).
  5. Online CFC monitor — In STEP 7, open the CFC online view, monitor the actual DBx.CHRG_CTRL.Charge value. This is the definitive ground truth.

8. Reading the Value Back

For two-way binding (for example, to highlight the current ComboBox selection when the faceplate re-opens), read the CFC value on the OpenPicture event of the picture or faceplate:

CHAR cCurrent = GetTagChar(".Charge");
cmbCharge.Text = cCurrent;

Use this on the picture-level event rather than the ComboBox Change event to avoid triggering a write loop when the user selects an option.

9. Extended Pattern: String Type CFC Inputs

If the CFC input Charge is declared as STRING[16] (typical for charge IDs in process industries), SetTagChar is wrong. The proper sequence uses the WinCC string functions:

// C action on ComboBox Change event
const char* szSel = (const char*)cmbCharge.Text;
int nLen = strlen(szSel);
// Copy the first 16 chars into the structured STRING type
for (int i = 0; i < nLen && i < 16; i++)
    SetTagByte(szSel[i]);  // not real API — illustrative

For actual production use, prefer the modern WinCC V7.4+ string helper SetTagString:

BOOL bOK = SetTagString(".Charge", cmbCharge.Text);

Note that SetTagString requires the CFC input to be a STRING type on the PLC side and the corresponding tag in WinCC must also be of type STRING with matching length. The Siemens support entry 28921535 covers this multi-language dynamic ComboBox pattern in detail, including the use of text libraries and language switching.

10. Multilingual ComboBox Considerations

PCS 7 installations commonly run with multiple OS languages. The ComboBox text displayed to the operator is not the same byte sequence as the internal CHAR / STRING value. Two design options exist:

  • Index-based writing — Configure the ComboBox with numeric indices (0, 1, 2). The C action writes the index using SetTagChar, and the PLC interprets the index against a text library. This is the most robust pattern.
  • Language tag writing — Write the displayed text directly. This couples the PLC logic to UI strings and is brittle during language updates.

For multilingual plants, prefer the index-based approach. The Siemens KB article on dynamic ComboBox filling with different language texts (Siemens Support entry 28921535) demonstrates the recommended text-library approach.

11. Troubleshooting Matrix

Symptom Likely Cause Remedy
Compile error "undefined identifier SetTagChar" Function spelled incorrectly or wrong build of WinCC Use SetTagChar (capital C) and confirm WinCC V7.0+
No error, but CFC input never changes Tag missing dot prefix Use ".Charge" not "Charge"
Tag Simulator can write, C script cannot Script not reloaded Close faceplate, reinit Graphics Runtime
Operator can write some users, not others Authorization level too low Set user to level 5 (Process controlling) for the tag
ComboBox text is multi-character, CHAR input only takes first byte Wrong data type pairing Change CFC input to STRING, switch to SetTagString
Reads work, writes return FALSE Tag marked read-only in Tag Management Enable operator-controllable attribute in CFC; rebuild OS
Writes work in test OS, fail in production OS Different user authorization on production OS Compare User Administrator config between OSes

12. Reference Checklist

  • Function name: SetTagChar (capital C in middle, case-sensitive on older builds).
  • CFC tag reference format: ".<InputName>" (leading dot mandatory when calling from a picture-bound C action).
  • PLC-side requirement: Operator-controllable attribute set on the CFC I/O (S7_m_c).
  • Runtime requirement: User authorization level 5 or higher for the tag's configured operator class.
  • Verification: WinCC Tag Simulator must be able to write the tag before the C script can.
  • Caching rule: Close the faceplate and reinitialize Graphics Runtime after editing the C action.

Why does SetTagChar("Charge", value) fail silently in WinCC?

WinCC resolves the tag name against the current picture context. CFC chart inputs are not flat global tags, so "Charge" returns "not found" without raising a compile error. Use the leading-dot form ".Charge" to target the CFC input of the current picture / faceplate.

Is the function spelled SetTagChar or SetTagchar?

SetTagChar with a capital C is correct. The lowercase variant compiles in some WinCC V7 builds but the symbol is not exported by the C scripting engine, so the call resolves to a no-op. Always use the documented capitalisation.

The C action runs but the CFC input never updates. What should I check first?

Confirm three things in order: (1) the tag exists in WinCC Tag Management with the S7 connection; (2) the CFC input has the operator-controllable attribute set in STEP 7; (3) the faceplate instance was re-opened after editing the script. WinCC caches the compiled C action per picture instance, so an edit on a still-open faceplate will not take effect until the faceplate is closed and the Graphics Runtime is reinitialized.

Can I write a multi-character ComboBox text to a CFC CHAR input?

No. A CHAR input is a single signed byte (range -128 to +127). Use SetTagChar for single characters, and switch to SetTagString plus a STRING[n] CFC input if the value is longer. Siemens support document 28921535 covers the string-handling pattern for dynamic ComboBoxes.

What user authorization is required to write CFC inputs from a ComboBox?

The CFC input must have the operator-controllable attribute set (S7_m_c), and the logged-in OS user must be at the authorization level assigned to that tag — typically level 5 (Process controlling). Higher levels (level 6 Process management) are required for tuning values that affect production.

Back to blog