⑤ Event Handlers

This section controls what happens when the engineer clicks a button. There are two levels of event handling:

  1. DialogEvents — immediate response to button clicks (runs in the event loop)
  2. CommandEvent — long-running operations triggered by DialogEvents via a "pending command" number
Why two levels?
VBScript running inside Siemens Starter cannot run multiple threads. If a long operation (like downloading parameters) was run directly inside a button click handler, the UI would freeze and the window would become unresponsive. Instead, the click handler just sets PendingCommand = n, and the main event loop picks it up on the next tick.

Event Flow

Engineer clicks button (e.g. "Config Drives") │ ▼ HTML onclick → VBScript DialogEvents_4() → DialogEvents(4) │ │ Sets PendingCommand = 101 │ Shows "Please Wait" status │ ▼ EventHandler() main loop detects PendingCommand ≠ 0 │ ▼ CommandEvent(101) │ │ Goes offline (GoOnline(False)) │ Calls ConfigureAllDrives() │ Saves project │ Shows "Config Drives - Done" in green │ ▼ PendingCommand = 0 → loop continues waiting for next click

EventHandler

Sub EventHandler( ThisIEDocument )
What it does:
This is the main loop of the program. It runs continuously until the engineer closes the HTML window. On every loop iteration (every ~1 second), it checks if a command is pending. If so, it processes it and clears the pending command. It also checks if the window has been closed (error reading ReadyState) and exits if so.
Sub EventHandler(ThisIEDocument)
    On Error Resume Next
    While (Not(boClose))             ' Keep looping until the window is closed (boClose = True)

        APP.Time(1)                   ' Wait 1 second (keeps the script responsive while idle)

        If PendingCommand <> 0 Then   ' Was a button pressed? Is there a command waiting to run?
            CommandEvent(PendingCommand)  ' Run the command (see table below)
            PendingCommand=0            ' Clear the pending flag once done
        End If

        Err.Clear
        iDummy = ThisIEDocument.ReadyState   ' Try to read the window state
        boClose = (Err.Number <> 0)          ' If this gives an error, the window was closed
        Err.Clear
    Wend

    APP.Workbench.Visible = True   ' Bring Starter's workbench back into focus after window closes
    ThisIEDocument.Quit             ' Close the IE window cleanly
End Sub

CommandEvent — Long-Running Commands

Private Sub CommandEvent( Number )
Command NumberTriggered byWhat it does
100Step 3 buttonGo offline → ConfigureAllCUs() → Save project
101Step 4 buttonGo offline → ConfigureAllDrives() → Save project
200Step 5 buttonGo online → check no drives are enabled → PROJ.Download()
300Step 8 buttonGo online → PostIDAllDrives()Ram2RomAllDevices()
400Step 9 buttonGo online → Ram2RomAllDevices() → Upload all devices → Save project
500Step 10 button(S53 new) ExportHMIFile() — export machine data for HMI
600Step 11 buttonGo online → Ram2RomAllDevices() only (RAM → ROM without upload)
Private Sub CommandEvent(Number)
    Select Case Number

        Case 100                     ' STEP 3: Config Infeed and CUs
            PendingCommand=0
            StatusLineEcho 3,"Blue","Config CU/Infeed - Going Offline"
            GoOnline(False)           ' Go offline before writing parameters
            StatusLineEcho 3,"Blue","Config CU/Infeed - Configuring"
            ConfigureAllCUs            ' Run the CU configuration (see file 03)
            StatusLineEcho 3,"Blue","Config CU/Infeed - Saving"
            APP.Workbench.WBProject.Save()   ' Save the Starter project file
            StatusLineEcho 3,"Green","Config CU/Infeed - Done"
            updatestatusdisplay        ' Refresh button states

        Case 200                     ' STEP 5: Download
            PendingCommand=0
            If GoOnline(True)=True Then     ' Must be online to download
                If AnyDeviceEnabled Then       ' SAFETY CHECK: No drives powered? If drives are spinning,
                    StatusLineEcho 5,"DarkRed","Drives Enabled - Activate G-stop"
                    ' ↑ ABORT — don't download with drives running (dangerous)
                Else
                    StatusLineEcho 5,"Blue","Downloading, please wait"
                    PROJ.Download()    ' Send all parameters to all drives (can take several minutes)
                    StatusLineEcho 5,"Green","Download, complete"
                End If
            Else
                StatusLineEcho 5,"DarkRed","Download, error - Unable to go Online"
            End If

        ' (Cases 100, 300, 400, 500, 600 follow the same pattern)
    End Select
End Sub

DialogEvents — Button Click Handler

Private Sub DialogEvents( Number )
What it does:
Receives button click events from the HTML page. Short operations run immediately here; long ones just set PendingCommand for CommandEvent to pick up.
Event NumberButton / SourceAction
1Step 1: Identify DrivesGo online, set ShowDriveIdent=True, call UpdateStatusDisplay to show the drive table
2Step 2: Edit Machine DataSet ShowMachineData=True, load and display current machine data form
3Step 3Set PendingCommand=100 (Config CUs)
4Step 4Set PendingCommand=101 (Config Drives)
5Step 5Set PendingCommand=200 (Download)
6Step 6Show "exit script and carry-out Safety Integrated config" message
7Step 7Show "exit script and carry-out Motor Identification" message
8Step 8Set PendingCommand=300 (Post ID Config)
9Step 9Set PendingCommand=400 (RAM→ROM + Upload)
10Step 10Set PendingCommand=500 (Export HMI file)
1001"Save" in Machine Data formCall SaveMachineData, hide machine data form
1002"Cancel" in Machine Data formHide machine data form without saving
2001"Accept" in Drive IdentCheck no duplicates, rename CUs and drives, go offline, save project
2002"Cancel" in Drive IdentHide drive ident table
How button clicks connect to DialogEvents:
In the HTML builder (file 04), each button gets assigned an onclick handler like:
.GetElementById("Button_1").OnClick = GetRef("DialogEvents_1")
GetRef("DialogEvents_1") creates a reference to the VBScript sub DialogEvents_1() which is defined as:
Private Sub DialogEvents_1() : DialogEvents(1) : End Sub
So clicking the HTML button calls DialogEvents_1()DialogEvents(1).

DialogID — Drive Flash Identification

Sub DialogID( ThisToNumber )
What it does:
Makes a specific drive module "flash" its LED so the engineer can identify which physical drive module corresponds to which row in the identification table. Clicking the same "- Identify -" button again turns the flashing off (toggle).
Sub DialogID(ThisToNumber)
    Index=1
    For Each adevice In PROJ.Devices
        For Each ato In adevice.TOs
            If (aTO.ExternalTypeName="SINAMICS_SERVO_SL" Or ATO.ExternalTypeName="SINAMICS_VECTOR_SVC") Then
                If index=ThisTONumber Then   ' Found the drive we want to identify
                    If ato.Parameters(124,0)=0 Then   ' p124 = LED control. 0 = off, 1 = flash
                        SetParam aTO, 124, 0, 1   ' Turn LED FLASHING ON
                        ' Change button colour to gold/blue = "flashing active"
                        MainPage.Document.GetElementbyid("Button_"&ThisToNumber+100).style.backgroundColor="gold"
                        MainPage.Document.GetElementbyid("Button_"&ThisToNumber+100).style.Color="blue"
                    Else
                        SetParam aTO, 124, 0, 0   ' Turn LED FLASHING OFF
                        ' Change button back to blue/gold = "not flashing"
                        MainPage.Document.GetElementbyid("Button_"&ThisToNumber+100).style.backgroundColor="blue"
                        MainPage.Document.GetElementbyid("Button_"&ThisToNumber+100).style.Color="gold"
                    End If
                End If
                index=index+1
            End If
        Next
    Next
End Sub

UpdateStatusDisplay

Sub UpdateStatusDisplay
What it does:
This is called after almost every action to refresh the entire UI state. It:

Button Enable/Disable Logic

ButtonEnabled when...
Step 0 (Auto Reset)Always disabled (reminder only)
Step 1 (Identify)Drives exist in project (autoconfig was run)
Step 2 (Machine Data)All drives in project are identified
Step 3 (Config CU)Drives identified + machine data complete + data valid
Step 4 (Config Drives)Same as Step 3
Step 5 (Download)Same as Step 3 + ALL drives have ConfigStatus ≥ 1
Steps 6-9Always enabled (manual steps or upload)
Sub UpdateStatusDisplay
    ' ── Show/hide Machine Data form ──────────────────────────────────────────────
    If ShowMachineData Then
        MainPage.Document.GetElementbyid("MachineData").style.Display = "inline"
    Else
        MainPage.Document.GetElementbyid("MachineData").style.Display = "none"
        MainPage.Document.GetElementbyid("main_table").style.Display = "inline"
    End If

    ' ── Show/hide Drive Ident table rows ─────────────────────────────────────────
    '   Loop through all devices and TOs, fill in the ID table with live data
    If ShowDriveIdent Then
        Index=1
        For Each adevice In PROJ.Devices
            For Each ato In adevice.TOs
                If (aTO.ExternalTypeName="SINAMICS_SERVO_SL" ...) Then
                    MainPage.Document.GetElementbyid("DriveIdentLine_"&Index).style.Display = "inline"
                    MainPage.Document.GetElementbyid("ID1_"&Index).innerHTML = adevice.Name  ' CU name
                    MainPage.Document.GetElementbyid("ID2_"&Index).innerHTML = ato.Name       ' Drive name
                    MainPage.Document.GetElementbyid("ID3_"&Index).innerHTML = ato.Parameters(121,0)  ' Drive number (p121)
                    MainPage.Document.GetElementbyid("ID4_"&Index).innerHTML = ato.Parameters(207,0)&" Amps"  ' Rated current
                    MainPage.Document.GetElementbyid("ID5_"&Index).innerHTML = ato.Parameters(307,0)&" kW"    ' Rated power
                    MainPage.Document.GetElementbyid("ID6_"&Index).innerHTML = ato.Parameters(311,0)&" rpm"   ' Rated speed
                    Index=Index+1
                End If
            Next
        Next
    End If

    ' ── Check machine data validity ──────────────────────────────────────────────
    '   MachineDataComplete = all required fields are filled
    '   MachineDataValid    = no conflicting values
    MachineDataComplete=True
    If (MachineData_SupplyVoltage < 350 Or MachineData_SupplyVoltage > 500) Then
        MachineDataComplete=False   ' Voltage must be selected (380-480V range)
    End If
    ' ... (similar checks for each drive: speed, torque, direction, motor type, encoder)

    ' ── Set button disabled states based on above checks ─────────────────────────
    MainPage.Document.GetElementbyid("Button_0").disabled=True  ' Step 0 always greyed out
    MainPage.Document.GetElementbyid("Button_1").disabled = _
        ShowMachineData Or Not DrivesExistInProject      ' Grey out if no drives autoconfig'd
    MainPage.Document.GetElementbyid("Button_2").disabled = _
        ShowMachineData Or Not AllDrivesInProjectIdentified  ' Grey out if drives not identified
    MainPage.Document.GetElementbyid("Button_5").disabled = _
        Not (DrivesExistInProject And AllDrivesInProjectIdentified _
        And MachineDataComplete And MachineDataValid And LowestConfigStatus>=1)
    ' ↑ Download button only active when ALL prerequisites are met (p2901 ≥ 1 = configured)
End Sub

Continue to ⑥ Machine Data.