Softwell WinCC VBScript — Handwritten M-Step Manual
PLC to PLC Communication Using VBScript
Practical Outcome: Read WinCC source tags, validate values, write destination PLC tags and prepare a reusable transfer script.
'Basic read/write pattern using SmartTags
Dim level
level = SmartTags("PLC1_TankLevel")
SmartTags("PLC2_TankLevel_SP") = level
'Button action: CopyPLC1ToPLC2
Dim tags(3)
tags(0) = "TankLevel" : tags(1) = "Pressure"
tags(2) = "Temperature" : tags(3) = "MotorCmd"
Dim i
For i = 0 To 3
SmartTags("PLC2_" & tags(i)) = SmartTags("PLC1_" & tags(i))
Next
SmartTags("SYS_LastTransferStatus") = "TRANSFER OK"
SmartTags("SYS_LastTransferTime") = Now()
Dim srcLevel, srcPressure
srcLevel = SmartTags("PLC1_TankLevel")
srcPressure = SmartTags("PLC1_Pressure")
If IsNumeric(srcLevel) And srcLevel >= 0 And srcLevel <= 100 Then
If IsNumeric(srcPressure) And srcPressure >= 0 And srcPressure <= 10 Then
SmartTags("PLC2_TankLevel") = srcLevel
SmartTags("PLC2_Pressure") = srcPressure
SmartTags("SYS_LastTransferStatus") = "OK"
Else
SmartTags("SYS_LastTransferStatus") = "ERR: PRESSURE OUT OF RANGE"
End If
Else
SmartTags("SYS_LastTransferStatus") = "ERR: INVALID LEVEL"
End If
SmartTags("SYS_LastTransferTime") = Now()
Daily Practice / Viva
Explain code line-by-line, show Runtime working screen, SQL query result and one troubleshooting point for Day 01.
Softwell WinCC VBScript — Handwritten M-Step Manual
Internal Tag Logging to SQL Server
Practical Outcome: Create Softwell_DB table and log WinCC internal tags into SQL Server using ADODB VBScript.
-- Run in SQL Server Management Studio
USE Softwell_DB;
CREATE TABLE InternalTagLog (
Id INT IDENTITY(1,1) PRIMARY KEY,
LogTime DATETIME NOT NULL DEFAULT GETDATE(),
BatchNo NVARCHAR(50),
SetPoint FLOAT,
ActualValue FLOAT,
OperatorName NVARCHAR(100),
ShiftName NVARCHAR(20)
);
Dim cn, sql, connStr
Set cn = CreateObject("ADODB.Connection")
connStr = "Provider=SQLOLEDB;" & _
"Data Source=.\SQLEXPRESS;" & _
"Initial Catalog=Softwell_DB;" & _
"Integrated Security=SSPI;"
cn.Open connStr
sql = "INSERT INTO InternalTagLog" & _
"(LogTime, BatchNo, SetPoint, ActualValue, OperatorName, ShiftName) VALUES (" & _
"GETDATE(), '" & SmartTags("INT_BatchNo") & "', " & _
SmartTags("INT_SetPoint") & ", " & _
SmartTags("INT_ActualValue") & ", '" & _
SmartTags("INT_OperatorName") & "', '" & _
SmartTags("INT_ShiftName") & "')"
cn.Execute sql
cn.Close
Set cn = Nothing
Dim cn, sql, connStr, errMsg
On Error Resume Next
Set cn = CreateObject("ADODB.Connection")
connStr = "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;" & _
"Initial Catalog=Softwell_DB;Integrated Security=SSPI;"
cn.Open connStr
If Err.Number <> 0 Then
SmartTags("SYS_DBStatus") = "CONN ERR: " & Err.Description
Err.Clear
Else
sql = "INSERT INTO InternalTagLog(LogTime, BatchNo, SetPoint, ActualValue) VALUES (" & _
"GETDATE(), '" & SmartTags("INT_BatchNo") & "', " & _
SmartTags("INT_SetPoint") & ", " & SmartTags("INT_ActualValue") & ")"
cn.Execute sql
If Err.Number <> 0 Then
SmartTags("SYS_DBStatus") = "SQL ERR: " & Err.Description
Err.Clear
Else
SmartTags("SYS_DBStatus") = "OK " & Now()
End If
cn.Close
End If
Set cn = Nothing
On Error GoTo 0
Daily Practice / Viva
Explain code line-by-line, show Runtime working screen, SQL query result and one troubleshooting point for Day 02.
Softwell WinCC VBScript — Handwritten M-Step Manual
External PLC Tag Logging to SQL Server
Practical Outcome: Log live PLC/OPC/S7 process values from WinCC external tags into SQL with quality and batch information.
-- SQL table for external (PLC) process data
USE Softwell_DB;
CREATE TABLE ProcessTagLog (
Id INT IDENTITY(1,1) PRIMARY KEY,
LogTime DATETIME NOT NULL DEFAULT GETDATE(),
TagName NVARCHAR(100) NOT NULL,
TagValue FLOAT,
Unit NVARCHAR(20),
QualityCode INT,
BatchNo NVARCHAR(50)
);
CREATE INDEX IX_ProcessLog_Time ON ProcessTagLog(LogTime DESC);
CREATE INDEX IX_ProcessLog_Tag ON ProcessTagLog(TagName, LogTime DESC);
'Cyclic action: LogPLCTagsToSQL (cycle: 5 s)
Dim cn, connStr, cmd, rs
Dim tagNames(3), tagUnits(3), i
Dim val, batchNo
tagNames(0) = "EXT_Reactor_Temp" : tagUnits(0) = "degC"
tagNames(1) = "EXT_Reactor_Pressure" : tagUnits(1) = "bar"
tagNames(2) = "EXT_FlowRate" : tagUnits(2) = "L/min"
tagNames(3) = "EXT_AgitatorRPM" : tagUnits(3) = "rpm"
batchNo = SmartTags("INT_BatchNo")
On Error Resume Next
Set cn = CreateObject("ADODB.Connection")
connStr = "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;" & _
"Initial Catalog=Softwell_DB;Integrated Security=SSPI;"
cn.Open connStr
If Err.Number = 0 Then
For i = 0 To 3
val = SmartTags(tagNames(i))
If IsNumeric(val) Then
cn.Execute "INSERT INTO ProcessTagLog(LogTime,TagName,TagValue,Unit,BatchNo)" & _
" VALUES(GETDATE(),'" & tagNames(i) & "'," & val & _
",'" & tagUnits(i) & "','" & batchNo & "')"
End If
Next
SmartTags("SYS_DBStatus") = "PLC LOG OK " & Now()
cn.Close
Else
SmartTags("SYS_DBStatus") = "CONN ERR: " & Err.Description
Err.Clear
End If
Set cn = Nothing
On Error GoTo 0
'Extended cyclic logger with quality check
Dim cn, i, val, qual, tagName, batchNo
Dim tagNames(3)
tagNames(0) = "EXT_Reactor_Temp"
tagNames(1) = "EXT_Reactor_Pressure"
tagNames(2) = "EXT_FlowRate"
tagNames(3) = "EXT_AgitatorRPM"
batchNo = SmartTags("INT_BatchNo")
On Error Resume Next
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;" & _
"Initial Catalog=Softwell_DB;Integrated Security=SSPI;"
If Err.Number = 0 Then
For i = 0 To 3
tagName = tagNames(i)
val = SmartTags(tagName)
qual = SmartTags(tagName).QualityCode 'WinCC quality 192 = Good
If IsNumeric(val) And qual = 192 Then
cn.Execute "INSERT INTO ProcessTagLog(LogTime,TagName,TagValue,QualityCode,BatchNo)" & _
" VALUES(GETDATE(),'" & tagName & "'," & val & "," & qual & ",'" & batchNo & "')"
End If
Next
cn.Close
End If
Set cn = Nothing
On Error GoTo 0
Daily Practice / Viva
Explain code line-by-line, show Runtime working screen, SQL query result and one troubleshooting point for Day 03.
Softwell WinCC VBScript — Handwritten M-Step Manual
Conditional Logic IF / ELSE Writing to SQL
Practical Outcome: Use If, ElseIf, Select Case, threshold logic, shift detection and de-bounce before writing events to SQL.
'De-bounce pattern: only write when state changes
Dim curState, prevState
curState = SmartTags("EXT_Reactor_Temp")
prevState = SmartTags("INT_PrevReactorTemp")
If Abs(curState - prevState) > 0.5 Then
'Value changed by more than 0.5 degrees - log it
SmartTags("INT_PrevReactorTemp") = curState
'...INSERT to SQL...
End If
-- EventLog table for condition-based logging
USE Softwell_DB;
CREATE TABLE EventLog (
Id INT IDENTITY(1,1) PRIMARY KEY,
EventTime DATETIME NOT NULL DEFAULT GETDATE(),
EventType NVARCHAR(50) NOT NULL, -- 'THRESHOLD','SHIFT','FAULT'
TagName NVARCHAR(100),
TagValue FLOAT,
Description NVARCHAR(255),
ShiftName NVARCHAR(20),
OperatorName NVARCHAR(100)
);
'Cyclic action: ThresholdLogger (cycle: 2 s)
Dim temp, pressure, flow, desc, evType
Dim cn, prevFaultFlag
temp = SmartTags("EXT_Reactor_Temp")
pressure = SmartTags("EXT_Reactor_Pressure")
flow = SmartTags("EXT_FlowRate")
prevFaultFlag = SmartTags("INT_FaultLogged")
desc = ""
evType = ""
If temp > 95 Then
evType = "THRESHOLD"
desc = "CRITICAL: Reactor Temp = " & temp & " degC (limit 95)"
ElseIf temp > 85 Then
evType = "WARNING"
desc = "HIGH WARN: Reactor Temp = " & temp & " degC"
End If
If pressure > 8 Then
evType = "THRESHOLD"
desc = desc & " | CRIT PRESSURE: " & pressure & " bar"
End If
If flow < 5 And flow >= 0 Then
evType = "THRESHOLD"
desc = desc & " | LOW FLOW: " & flow & " L/min"
End If
'Only log if new fault detected and not already logged
If evType <> "" And prevFaultFlag = 0 Then
On Error Resume Next
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;" & _
"Initial Catalog=Softwell_DB;Integrated Security=SSPI;"
If Err.Number = 0 Then
cn.Execute "INSERT INTO EventLog(EventTime,EventType,TagName,TagValue,Description)" & _
" VALUES(GETDATE(),'" & evType & "','REACTOR'," & temp & ",'" & desc & "')"
cn.Close
SmartTags("INT_FaultLogged") = 1
End If
Set cn = Nothing
On Error GoTo 0
ElseIf evType = "" Then
SmartTags("INT_FaultLogged") = 0 'Reset when conditions return to normal
End If
Daily Practice / Viva
Explain code line-by-line, show Runtime working screen, SQL query result and one troubleshooting point for Day 04.
Softwell WinCC VBScript — Handwritten M-Step Manual
Alarms, Recipes and Trends via VBScript
Practical Outcome: Build final VBScript integration: alarm logging, SQL recipe load/save and SQL-based custom trend data.
-- AlarmLog table for custom alarm history
USE Softwell_DB;
CREATE TABLE AlarmLog (
Id INT IDENTITY(1,1) PRIMARY KEY,
AlarmTime DATETIME NOT NULL DEFAULT GETDATE(),
AlarmNo INT,
AlarmClass NVARCHAR(50),
AlarmText NVARCHAR(255),
AlarmState NVARCHAR(20), -- CAME_IN / WENT_OUT / ACKED
TagName NVARCHAR(100),
TagValue FLOAT,
OperatorName NVARCHAR(100),
ShiftName NVARCHAR(20)
);
'Tag change action on alarm bit tag @OM_ALM_1
'Triggered when alarm state bit changes
Dim cn, alarmState, alarmText, stateStr
alarmState = SmartTags("@OM_ALM_1")
alarmText = "High Temperature Reactor 01"
Select Case alarmState
Case 1: stateStr = "CAME_IN"
Case 2: stateStr = "WENT_OUT"
Case 4: stateStr = "ACKED"
Case Else: stateStr = "UNKNOWN"
End Select
On Error Resume Next
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;" & _
"Initial Catalog=Softwell_DB;Integrated Security=SSPI;"
If Err.Number = 0 Then
cn.Execute "INSERT INTO AlarmLog(AlarmTime,AlarmNo,AlarmClass,AlarmText,AlarmState," & _
"TagName,TagValue,OperatorName,ShiftName)" & _
" VALUES(GETDATE(),1,'HIGH_TEMP','" & alarmText & "','" & stateStr & _
"','EXT_Reactor_Temp'," & SmartTags("EXT_Reactor_Temp") & _
",'" & SmartTags("INT_OperatorName") & _
"','" & SmartTags("SYS_CurrentShift") & "')"
cn.Close
End If
Set cn = Nothing
On Error GoTo 0
-- Recipe parameters table
USE Softwell_DB;
CREATE TABLE RecipeData (
Id INT IDENTITY(1,1) PRIMARY KEY,
RecipeName NVARCHAR(100) NOT NULL,
Param_TempSP FLOAT,
Param_PressureSP FLOAT,
Param_FlowSP FLOAT,
Param_MixTime INT,
Param_AgitatorRPM INT,
CreatedBy NVARCHAR(100),
CreatedAt DATETIME DEFAULT GETDATE()
);
'Button action: LoadRecipe
'Reads selected recipe from SQL and writes SP tags
Dim cn, rs, sql, recipeName
recipeName = SmartTags("INT_SelectedRecipe") 'e.g. "BATCH_A"
On Error Resume Next
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;" & _
"Initial Catalog=Softwell_DB;Integrated Security=SSPI;"
sql = "SELECT TOP 1 * FROM RecipeData WHERE RecipeName='" & recipeName & "'"
rs.Open sql, cn
If Not rs.EOF Then
SmartTags("INT_TempSP") = rs("Param_TempSP")
SmartTags("INT_PressureSP") = rs("Param_PressureSP")
SmartTags("INT_FlowSP") = rs("Param_FlowSP")
SmartTags("INT_MixTime") = rs("Param_MixTime")
SmartTags("INT_AgitatorRPM") = rs("Param_AgitatorRPM")
SmartTags("SYS_DBStatus") = "RECIPE LOADED: " & recipeName
Else
SmartTags("SYS_DBStatus") = "ERR: Recipe not found - " & recipeName
End If
rs.Close : cn.Close
Set rs = Nothing : Set cn = Nothing
On Error GoTo 0
Daily Practice / Viva
Explain code line-by-line, show Runtime working screen, SQL query result and one troubleshooting point for Day 05.
