Evaluating DWORD Bitmasks in WinCC VBScript: Converting C-Script Bitwise Logic and Handling the Bit 31 Sign Limitation

Evaluating DWORD Bitmasks in WinCC VBScript: Converting C-Script Bitwise Logic and Handling the Bit 31 Sign Limitation

To convert a WinCC C-Script bitmask evaluation (GetTagDWord & 0x10000) to VBScript, use the VBS bitwise operator And with hexadecimal literals: (HMIRuntime.Tags('.QdwState').Read And &H10000) <> 0. When writing VBScript in WinCC, developers must account for the fact that VBS treats all 32-bit integers as signed Long values (-2,147,483,648 to 2,147,483,647). Evaluating Bit 31 (hex &H80000000) directly causes an overflow error unless represented as negative integer &H80000000& or evaluated using arithmetic sign checks.

Table of Contents

C-Script vs VBScript Runtime Engines in WinCC Graphics Designer

In SIMATIC WinCC, dynamic graphics can be programmed using ANSI-C or VBScript. C-Scripts provide direct memory access and unsigned data types (DWORD, WORD, BYTE). However, modern engineering standards frequently favor VBScript for graphic animations due to easier syntax, better object encapsulation via Item and HMIRuntime, and superior crash isolation (VBS runtime exceptions rarely crash the entire pdlrt.exe graphics runtime).

A common conversion requirement involves decoding technological status words (e.g. QdwState from APL blocks or custom FBs) to dynamically change colors, visibility, or text.

Converting Bitwise C-Actions to VBScript Syntax

In ANSI-C, bitwise testing uses the & operator against hexadecimal literals:

if ((GetTagDWord(TAG_1)) & 0x10000) return 46336;
else if ((GetTagDWord(TAG_1)) & 0x40000) return 206;

In VBScript, the equivalent bitwise operator is And, and hexadecimal literals use the &H prefix:

Dim val, tag
Set tag = HMIRuntime.Tags(".QdwState")
val = tag.Read
If (val And &H10000) <> 0 Then
    Item.BackColor = 46336
ElseIf (val And &H40000) <> 0 Then
    Item.BackColor = 206
ElseIf (val And &H100000) <> 0 Then
    Item.BackColor = 12632256
Else
    Item.BackColor = 12632256
End If

The 32-Bit Signed Integer Limitation in VBScript (The Bit 31 Trap)

A critical trap exists in VBScript: VBScript does not support unsigned 32-bit integers (DWORD).

  • In VBS, any 32-bit integer is treated as a signed Long with values from -2,147,483,648 to +2,147,483,647.
  • Bits 0 through 30 represent positive magnitude.
  • Bit 31 is the Sign Bit: If Bit 31 of QdwState is set (1), VBS interprets the tag value as a negative number.
  • Attempting to write If (val And &H80000000) <> 0 in VBS can throw runtime error 'Overflow' because &H80000000 exceeds the positive limit of a standard literal unless cast with the type-declaration character &H80000000& or checked via val < 0.

Safe Bitwise Masking Across All 32 Bits in VBScript

To evaluate any bit from Bit 0 to Bit 31 without overflow exceptions:

  • Bits 0 to 30: Use standard bitwise And with hex constants:
    IsBit16Set = ((val And &H10000) <> 0)
    IsBit18Set = ((val And &H40000) <> 0)
    IsBit20Set = ((val And &H100000) <> 0)

  • Bit 31 (Highest Bit): Simply check whether the value is negative:
    IsBit31Set = (val < 0)
    Because two's-complement arithmetic assigns the sign bit to bit 31, a negative integer strictly indicates Bit 31 is asserted.

Comparative Syntax Matrix: C-Script vs VBScript Bit Operations

The following table compares syntax and evaluation between C-Script and VBScript in WinCC:

OperationWinCC C-ScriptWinCC VBScriptCaveat / DifferenceRead Tag ValueGetTagDWord("Tag")HMIRuntime.Tags("Tag").ReadVBS reads as signed 32-bit LongBitwise ANDval & 0x10000val And &H10000And performs bitwise AND on integersBitwise ORval | 0x10000val Or &H10000Or performs bitwise OR on integersBit 31 Testingval & 0x80000000val < 0 or val And &H80000000&Unsigned vs Two's Complement signedBit 31 Settingval |= 0x80000000val = val Or &H80000000&Must append & suffix to avoid literal overflowTrigger BehaviorCyclic or Tag TriggerCyclic or Tag TriggerVBS supports direct Tag Change events

Production-Grade VBScript Implementation for Dynamic Property Linking

To implement the complete script on an object property (e.g. BackColor):

  1. Open the process picture in Graphics Designer.
  2. Right-click the graphical object -> Properties.
  3. Locate Colors -> Background Color.
  4. Right-click the lightbulb icon in the Dynamic column and select VBS-Action.
  5. Enter the converted VBS code:
    vbscript Function BackColor_Trigger(ByVal Item) Dim val val = HMIRuntime.Tags(".QdwState").Read If (val And &H10000) <> 0 Then BackColor_Trigger = 46336 ' Green ElseIf (val And &H40000) <> 0 Then BackColor_Trigger = 206 ' Red ElseIf (val And &H100000) <> 0 Then BackColor_Trigger = 12632256 ' Grey Else BackColor_Trigger = 12632256 ' Default Grey End If End Function
  6. Configure the trigger to execute on Tag Change of .QdwState rather than cyclic polling.

Verification

Validate script behavior in runtime:

  1. State Assertion: In CFC Test Mode, force the bit corresponding to 0x10000 (Bit 16). Confirm object turns green (46336).
  2. Secondary State Assertion: Force Bit 18 (0x40000). Confirm object turns red (206).
  3. Diagnostics Check: Open WinCC Diagnostics viewer (apdiag.exe). Confirm zero VBS runtime error messages.
  4. Performance Check: Verify graphics display responds without lag during rapid status transitions.

Limitations and Operational Precautions

Observe the following scripting best practices:

  • Tag Trigger Optimization: Always bind VBS actions to Tag Triggers rather than 250 ms or 1 s cyclic timers to avoid CPU starvation.

  • Parent Faceplate Scope: If the script is located inside a faceplate picture, prepend the tag name with Item.Parent.TagPrefix.

  • Commercial Accuracy Disclaimer: Software order numbers (MLFBs), package structures, and hardware capacity prerequisites vary across PCS 7 versions and hardware platforms. System integrators must confirm exact software entitlements and licensing requirements against the official Siemens Catalog CA 01 and obtain an authorized Siemens distributor quotation prior to purchasing.

Engineer FAQ

Is VBScript faster than C-Script in WinCC Graphics Designer?

For simple property dynamics with tag triggers, both execute within milliseconds. VBScript is easier to maintain and isolates exceptions better than C-Script.

What does the trailing ampersand in &H80000000& mean?

In VBScript, the ampersand suffix explicitly declares the literal as a signed Long type, preventing the compiler from raising a constant overflow.

Can I read tag quality code in VBScript alongside the value?

Yes, using Set tag = HMIRuntime.Tags("MyTag") followed by tag.Read, you can query tag.QualityCode to ensure the data is valid before evaluating bits.

Technical References

Related Articles

Related Tags

  • SIMATIC WinCC
  • VBScript
  • C-Script
  • DWORD
  • Bitmask
  • Bit 31
  • Graphics Designer
  • APL Status
6 respuestas