WinCC VBScript recipe and batch parameter management
This guide shows how to load, validate, stage and apply product parameters without treating a file read or HMI tag write as proof that the PLC accepted the recipe.
1. Choose native recipe functions before custom scripting
Use the supported WinCC recipe control or recipe system when it meets the operational requirement. A custom VBScript workflow is justified when an approved external CSV, legacy format, database interface or specialized audit process cannot be implemented cleanly with native configuration.
| Requirement | Preferred approach |
|---|---|
| Standard HMI recipe selection and transfer | Native WinCC recipe functions |
| Simple product parameter set | Versioned native data record |
| Approved legacy CSV exchange | Bounded VBS import/export with validation |
| Central multi-line recipe repository | Reviewed SQL or MES interface |
| Regulated electronic records | Validated platform and approved audit-trail design |
2. Define a versioned recipe data model
A recipe needs more than a list of values. Store identity and compatibility fields so the Runtime can reject the wrong product or schema.
- Recipe ID and product code
- Recipe name, revision and schema version
- Approved status and effective date
- Parameter name, value, unit and allowed range
- Created, reviewed and modified metadata where required
Do not identify a recipe only by its filename. Validate the internal ID and schema after opening the record.
3. Validate each parameter before staging
Function IsRecipeValueValid(ByVal parameterName, ByVal proposedValue)
IsRecipeValueValid = False
If Not IsNumeric(proposedValue) Then Exit Function
Dim value
value = CDbl(proposedValue)
Select Case CStr(parameterName)
Case "FillTime_s"
IsRecipeValueValid = (value >= 0.5 And value <= 30)
Case "TargetVolume_ml"
IsRecipeValueValid = (value >= 100 And value <= 2000)
Case "ConveyorSpeed_pct"
IsRecipeValueValid = (value >= 10 And value <= 100)
End Select
End FunctionKeep master limits in the PLC or approved configuration source. The HMI validation provides early feedback but is not the final control limit.
4. Parse a controlled CSV recipe file
Use a documented delimiter, invariant field order and explicit encoding. Reject missing columns, duplicate parameters and unknown names.
Function ParseRecipeLine(ByVal lineText)
Dim fields
fields = Split(CStr(lineText), ",")
If UBound(fields) <> 2 Then
ParseRecipeLine = ""
Exit Function
End If
If Not IsRecipeValueValid(Trim(fields(0)), Trim(fields(1))) Then
ParseRecipeLine = ""
Exit Function
End If
ParseRecipeLine = Trim(fields(0)) & "|" & _
Trim(fields(1)) & "|" & Trim(fields(2))
End FunctionFor text values containing delimiters or quotes, use a proper reviewed CSV parser or a simpler approved exchange format rather than expanding ad hoc splitting logic.
5. Stage recipe values before PLC application
Write validated values to dedicated staging tags. Do not overwrite active control parameters one by one while parsing the file.
Sub StageBottleRecipe(ByVal fillTime, ByVal targetVolume, ByVal speedPercent)
HMIRuntime.Tags("Recipe_Stage_FillTime_s").Write CDbl(fillTime)
HMIRuntime.Tags("Recipe_Stage_TargetVolume_ml").Write CDbl(targetVolume)
HMIRuntime.Tags("Recipe_Stage_ConveyorSpeed_pct").Write CDbl(speedPercent)
HMIRuntime.Tags("Recipe_Stage_Valid").Write 1
HMIRuntime.Trace "Recipe values staged for PLC validation" & vbCrLf
End SubClear the valid flag whenever parsing, validation or communication fails.
6. Use an apply-request and acknowledgment handshake
- WinCC stages every validated value and the recipe ID.
- WinCC increments a request ID or sets an apply-request bit.
- The PLC confirms machine state and validates the entire parameter set.
- The PLC copies accepted values atomically into active parameters.
- The PLC returns accepted, rejected or timeout status with a reason code.
- WinCC displays the applied recipe ID and PLC acknowledgment.
This prevents a partial recipe from becoming active when communication fails during transfer.
7. Save approved parameters to CSV or SQL Server
When exporting a recipe, write to a temporary file or transaction first, verify completion and then publish the new revision. For shared recipe repositories, use SQL constraints, transactions and role-based access.
- Do not overwrite the only approved recipe copy.
- Store a new revision with a clear effective state.
- Parameterize SQL values and allowlist identifiers.
- Record affected-row count and transaction result.
Continue with Export WinCC tag data to CSV and WinCC VBScript SQL Server CRUD operations.
8. Record recipe and batch audit context
A useful record identifies the recipe, revision, batch, equipment, request time, PLC result and responsible user where collection is approved.
| Audit field | Purpose |
|---|---|
| Recipe ID and revision | Identify the parameter set requested |
| Batch or order ID | Connect parameters to production context |
| Request and acknowledgment time | Measure transfer and acceptance sequence |
| PLC result code | Distinguish applied, rejected and timed-out requests |
| Changed parameters | Support review and troubleshooting |
Regulated audit trails require validated controls beyond an ordinary text log. Follow the applicable quality and records-management requirements.
9. Recover from missing, invalid or partial recipes
- Keep the currently active PLC recipe unchanged after a failed import.
- Clear HMI staging-valid state and show the exact validation failure.
- Do not retry an apply request indefinitely.
- Preserve the last known approved recipe and its revision.
- Require a controlled rollback rather than editing active values manually.
10. Batch execution and recipe changes
Do not permit an uncontrolled recipe change during an active batch. The PLC or batch system should expose states such as Idle, Ready, Running, Held and Complete, with a defined rule for when a new recipe may be staged or applied.
11. Commissioning test matrix
| Test | Expected result |
|---|---|
| Unknown recipe ID | Rejected before staging |
| Wrong schema version | Rejected with compatibility message |
| Value outside range | No apply request; failed parameter identified |
| Communication loss during transfer | Active PLC recipe remains unchanged |
| Recipe request during active batch | PLC rejects or defers according to specification |
| Duplicate request ID | No duplicate application |
| Authorized rollback | Previous approved revision restored and recorded |
12. Bottling-line recipe example
A bottle recipe contains target volume, fill time and conveyor speed. WinCC loads and validates a selected revision, stages all values, and submits one apply request. The PLC accepts the recipe only while the filler is Idle and all limits are valid. The resulting batch record stores the recipe revision and PLC acknowledgment rather than assuming that file import alone completed the changeover.
Frequently asked questions
Should a custom VBScript replace WinCC Recipe Management?
No. Use native recipe functions when they meet the requirement. Custom VBS is appropriate only for a justified and reviewed external workflow.
Can WinCC write active PLC parameters directly?
The safer design stages values and lets the PLC validate and apply the complete set atomically.
Is CSV suitable for every recipe system?
No. CSV can suit small controlled exchanges but lacks the concurrency, access control and transaction support of a database or validated recipe platform.
How should recipe rollback work?
Restore a known approved revision through the same validation and PLC acknowledgment process, and record the rollback result.
