6.50: Changing driver mode via VBA

6.50: Changing driver mode via VBA

With zenon 6.50 it is possible to access the driver config via VBA for some drivers (by Nov. 2009: "S7TCP32;IEC870;LOGIX32")
If you are in the situation that you have an integration project with many sub projects including many drivers, it will take some time to change the driver mode i.e. from "Hardware" to "Simulation counting" if some tests should be done.
Remedy:
the VBA Module basChangeDriverMod_650 loops through all projekts within the workspace and changes the desired driver mode
HowToDo:
- open VBA-IDE for the editor (ALT+F11)
- create a new Module (name it i.e. basChangeDriverMod_650)
- copy&paste following code into this new Module:
VBA Code:

'the DynProperty for switching between the different driver mode is called "GenDriverMode"
'GenDriverMode=0 Hardware
'GenDriverMode=1 Simulation counting
'GenDriverMode=2 Simulation static
'GenDriverMode=3 Simulation programmed
'FYI: Multi-User projects are not considered
Public Function ChangeDriverMod_650()
'with release of zenon 6.50 following drivers support the access to the driver configuartion:
Const strDriverTypes As String = "S7TCP32;IEC870;LOGIX32"
'if new drivers support this interface add the driver .exe name to the string above with a ";" in between

'declarations
Dim arrGenDriverMode(3) As String
Dim iGenDriverMode As Integer
Dim strDriverMode As String
Dim arrDriverTypes
Dim iDriverTypes As Integer
Dim obProjects As Projects
Dim obProject As Project
Dim iProject As Integer
Dim obDrivers As Drivers
Dim obDriver As Driver
Dim iDriver As Integer
Dim iDriverChanged As Integer
Dim arrMessage
Dim strMessage As String
Dim iMessage As Integer
Dim myFSO As FileSystemObject
Dim myTextStream As TextStream
Const strFileName As String = "C:\DriverModeChange.txt"

arrGenDriverMode(0) = "Hardware"
arrGenDriverMode(1) = "Simulation counting"
arrGenDriverMode(2) = "Simulation static"
arrGenDriverMode(3) = "Simulation programmed"
'display input box (in order to enter the desired driver mode)
strDriverMode = InputBox("0 = " & arrGenDriverMode(0) _
& vbLf & "1 = " & arrGenDriverMode(1) _
& vbLf & "2 = " & arrGenDriverMode(2) _
& vbLf & "3 = " & arrGenDriverMode(3) _
& vbLf & vbLf & "DO NOT INTERRUPT THIS WIZARD ABNORMALY!" _
& vbLf & vbLf & "Enter driver mode: " _
, "Driver Mode Configurator for " & strDriverTypes, "2")

'check if something has been entered
If strDriverMode = "" Then
Exit Function
End If

'check if the input is a numerical value
If Not IsNumeric(strDriverMode) Then
MsgBox "Invalid Mode entered!", vbCritical, "Driver Mode Configurator"
Exit Function
End If

'check if the numerical is between the mode range (0 -> "arrGenDriverMode(x)")
If (CInt(strDriverMode) < 0) Or (CInt(strDriverMode) > UBound(arrGenDriverMode)) Then
MsgBox "Invalid Mode entered!", vbCritical, "Driver Mode Configurator"
Exit Function
End If

'get the list of defined drivers (as defined with strDriverTypes)
arrDriverTypes = Split(strDriverTypes, ";")

'get all projects loaded in the workspace
Set obProjects = MyWorkspace.Application.Projects
For iProject = 0 To obProjects.Count - 1
'create a object from the project
Set obProject = obProjects.Item(iProject)
If Not obProject Is Nothing Then
'check if the project is a global project
If obProject.IsGlobal Then
Debug.Print obProject.Name & " is a Global project"
'check if the project is a Mulit-User project
ElseIf obProject.DynProperties("MultiUser") = True Then
Debug.Print obProject.Name & " is a Multi-User project!"
Else
'so all other projects are valid for checking
'get all drivers of the project
Set obDrivers = obProject.Drivers
For iDriver = 0 To obDrivers.Count - 1
'create a object from the driver
Set obDriver = obDrivers.Item(iDriver)
If Not obDriver Is Nothing Then
'check if the driver supports the configuration via VBA (defined via "Const strDriverTypes")
For iDriverTypes = 0 To UBound(arrDriverTypes)
If obDriver.Name = arrDriverTypes(iDriverTypes) Then
'open the driver config
Call obDriver.OpenConfig
'only change the desired driver mode if it differs
If obDriver.DynProperties("GenDriverMode") <> CInt(strDriverMode) Then
obDriver.DynProperties("GenDriverMode") = CInt(strDriverMode)
'create a log message
strMessage = strMessage & "Project: """ & obProject.Name & """ - Driver: """ & obDriver.Identification & """ - New Mode: """ & arrGenDriverMode(strDriverMode) & """" & ";"
'increment the counter (changed drivers)
iDriverChanged = iDriverChanged + 1
End If
'close the driver config
Call obDriver.CloseConfig(True)
End If
Next iDriverTypes
End If
'destroy the driver object
Set obDriver = Nothing
Next iDriver
'destroy the driver collection object
Set obDrivers = Nothing
End If
End If
'destroy the project object
Set obProject = Nothing
Next iProject
'destroy the project collection object
Set obProjects = Nothing

'if nothing has been changed -> leave the function
If iDriverChanged = 0 Then
MsgBox "No driver modes have been changed!", vbInformation, "Driver Mode Configurator"
Exit Function
End If

'write the driver mode changes into the defined text file
Set myFSO = New FileSystemObject
Set myTextStream = myFSO.OpenTextFile(strFileName, ForWriting, True)

myTextStream.WriteLine (iDriverChanged & " drivers have been changed")
'split up the log string
arrMessage = Split(strMessage, ";")
For iMessage = 0 To UBound(arrMessage)
'write each single line to the textfile
myTextStream.WriteLine arrMessage(iMessage)
Next iMessage

'close the stream
myTextStream.Close
'destroy the stream object
Set myTextStream = Nothing

'show the written text file in notepad
Call Shell("C:\Windows\system32\notepad.exe " & strFileName & "", 1)

'delete the text file
myFSO.DeleteFile (strFileName)
'destroy the fso
Set myFSO = Nothing
End Function


- copy following VBA Code at the end fo MyWorkspace:
Public Sub DriverMode_Wizard()
ChangeDriverMod_650
End Sub
- save ZenWorkspace.vba
- choose at the menu bar "VBA Macros" in the editor the VBA-Macro "DriverMode_Wizard"
- after launching the Wizard choose the desired driver mode and press "OK"

Regards,
Herbert

This is a migrated post! Originally posted on 03.12.2009 by user herberto. Please be aware that information can be outdated.

    Disclaimer

    This document governs the use of our Community Forum. By registering and using the platform, you accept these conditions.

    The COPA-DATA Community Forum serves to encourage the exchange of information and experience about the zenon software between forum users respectively zenon users.

    Please mind that any published information on the Community Forum is the subjective opinion and view based on the experience and the level of knowledge of the author. COPA-DATA does not overtake any responsibility for the content and the accuracy of the shared information.

    Users of the Community Forum are encouraged to share only well-founded experiences and to point out any risks associated with the implementation of proposed solutions to problems. COPA-DATA at its absolute discretion, reserves the right to moderate the forum. In this connection COPA-DATA may remove any information containing false facts, potentially dangerous solutions, bad language or content that may insult, degrade or discriminate others. COPA-DATA may block a non-complying user from forum access if the user violated this provision.

    COPA-DATA reserves the right to change this document from time to time at own discretion.


    Ing. Punzenberger COPA-DATA GmbH
    Karolingerstraße 7b · 5020 Salzburg · Austria
    www.copadata.com