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
- Converting Bitwise C-Actions to VBScript Syntax
- The 32-Bit Signed Integer Limitation in VBScript (The Bit 31 Trap)
- Safe Bitwise Masking Across All 32 Bits in VBScript
- Comparative Syntax Matrix: C-Script vs VBScript Bit Operations
- Production-Grade VBScript Implementation for Dynamic Property Linking
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
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
Longwith values from-2,147,483,648to+2,147,483,647. - Bits 0 through 30 represent positive magnitude.
- Bit 31 is the Sign Bit: If Bit 31 of
QdwStateis set (1), VBS interprets the tag value as a negative number. - Attempting to write
If (val And &H80000000) <> 0in VBS can throw runtime error 'Overflow' because&H80000000exceeds the positive limit of a standard literal unless cast with the type-declaration character&H80000000&or checked viaval < 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
Andwith 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:
GetTagDWord("Tag")HMIRuntime.Tags("Tag").Readval & 0x10000val And &H10000And performs bitwise AND on integersval | 0x10000val Or &H10000Or performs bitwise OR on integersval & 0x80000000val < 0 or val And &H80000000&val |= 0x80000000val = val Or &H80000000&& suffix to avoid literal overflowProduction-Grade VBScript Implementation for Dynamic Property Linking
To implement the complete script on an object property (e.g. BackColor):
- Open the process picture in Graphics Designer.
- Right-click the graphical object -> Properties.
- Locate Colors -> Background Color.
- Right-click the lightbulb icon in the Dynamic column and select VBS-Action.
- 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 - Configure the trigger to execute on Tag Change of
.QdwStaterather than cyclic polling.
Verification
Validate script behavior in runtime:
- State Assertion: In CFC Test Mode, force the bit corresponding to
0x10000(Bit 16). Confirm object turns green (46336). - Secondary State Assertion: Force Bit 18 (
0x40000). Confirm object turns red (206). - Diagnostics Check: Open WinCC Diagnostics viewer (
apdiag.exe). Confirm zero VBS runtime error messages. - 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
- SIMATIC WinCC V7.4 / V7.5 VBS for External Applications System Manual (Entry ID: 109746516)
- Siemens Industry Online Support (SIOS) FAQ: Bit-by-Bit Linking in WinCC VBScript (Entry ID: 27039111)
- SIMATIC WinCC V7.4 Scripting: C and VBS Reference Manual (Entry ID: 109740190)
- Microsoft VBScript Language Reference (Entry ID: 109751825)
Related Articles
- Troubleshooting WinCC Web Client 12-Second APL Faceplate Opening Delays in SIMATIC PCS 7 V8.1
- Creating Custom AS Blocks and OS Faceplates in SIMATIC PCS 7
- Diagnosing 'F' Symbol on PCS 7 APL Block Icons - Bit 24-25 in EventState
Related Tags
- SIMATIC WinCC
- VBScript
- C-Script
- DWORD
- Bitmask
- Bit 31
- Graphics Designer
- APL Status