'TWinCAT: SPS-Wiederherstellung: 'Schaut sich die Profibuskarten an und tauscht sie ggf. aus 'Wird von RestoreTWinCAT aufgerufen Option Explicit Dim wshshell,fso,i,Fnct Dim strPath,strSystemFolder,strWinFolder,strTempFolder '----------------------------- Objekte erzeugen -------------------------------- i = 0 On Error Resume Next Do err.Clear i = i+1 Set wshshell = CreateObject("WScript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") Set Fnct = CreateObject("Install.Functions") WScript.Sleep 1000 Loop Until Err.Number = 0 OR i > 30 If Err.Number <> 0 Then WScript.Quit End If On Error Goto 0 '--------------------------- Standardpfade ------------------------------------- strPath = GetCurrentPath() strSystemFolder = fso.GetSpecialFolder(1) & "\" strWinFolder = fso.GetSpecialFolder(0) & "\" strTempFolder = fso.GetSpecialFolder(2) & "\" '------------------------------ Main ------------------------------------------- 'Konstanten für den Systemmanager const TREEITEMTYPE_DEVICE = 2 const IODEVICETYPE_CP5412A2 = 7 const IODEVICETYPE_FC3100 = 38 const IODEVICETYPE_FC3100_SLAVE = 60 Dim Arguments 'Kommandozeilenparameter Set Arguments = wscript.Arguments 'SystemManagerFile Dim WsmFileName Dim tsm Dim strProject WsmFileName = Arguments(0) 'WsmFileName = "c:\Focke\M700\M700_00200.wsm" If Len(WsmFileName) = 0 Or InStr(WsmFileName,"\") = 0 Then WScript.Quit End If Dim nPosition nPosition = InstrRev(WsmFileName,"\") strProject = Mid(WsmFileName,nPosition+1) strProject = Replace(strProject,".wsm","") Set tsm = CreateObject("TCatSysManager.TcSysManager") 'Wsm Konfiguration On Error Resume Next Dim SysManagerItems Call tsm.OpenConfiguration(WsmFileName) Set SysManagerItems = tsm.LookupTreeItem("TIID") 'Start If err.number <> 0 Then wscript.quit 'Gefundene Geräte Dim xml Dim FoundDevices Set xml = CreateObject("Msxml.DOMDocument") Call xml.loadXml(SysManagerItems.ProduceXml) Set FoundDevices = xml.selectNodes("TreeItem/DeviceGrpDef/FoundDevices/Device") If err.number <> 0 Then wscript.quit On Error Goto 0 'GetLocalProfibusCard Dim nCurrentDevice Dim strLocalProfibusAdapter Dim nFoundDevices nCurrentDevice = 0 strLocalProfibusAdapter = "" nFoundDevices = FoundDevices.length 'Get Local Profibus Card Dim LocalDeviceInfo Dim DeviceInfo Do While nCurrentDevice < nFoundDevices And strLocalProfibusAdapter = "" DeviceInfo = FoundDevices(nCurrentDevice).selectSingleNode("ItemSubType").nodeTypedValue Select Case DeviceInfo Case CStr(IODEVICETYPE_CP5412A2) strLocalProfibusAdapter = "Profibus Master (CP5412)" LocalDeviceInfo = DeviceInfo Case CStr(IODEVICETYPE_FC3100) strLocalProfibusAdapter = "Profibus Master1 (FC3100)" LocalDeviceInfo = DeviceInfo End Select nCurrentDevice = nCurrentDevice +1 Loop 'GetWsmProfibusCard Dim strWsmProfibusAdapter Dim SysManagerDevice Dim WsmDeviceInfo Dim pItemWsm strWsmProfibusAdapter = "" For Each SysManagerDevice In SysManagerItems ' Für alle Devices in der Datei Dim l_CurrentSysManagerDevice Set l_CurrentSysManagerDevice = SysManagerDevice DeviceInfo = l_CurrentSysManagerDevice.ItemSubType If DeviceInfo = IODEVICETYPE_CP5412A2 Or DeviceInfo = IODEVICETYPE_FC3100 Then strWsmProfibusAdapter = l_CurrentSysManagerDevice.PathName WsmDeviceInfo = DeviceInfo End If Next 'Profibuskarte in wsm-Datei <> gefundene Profibuskarte If CStr(WsmDeviceInfo) <> CStr(LocalDeviceInfo) Then Dim LocalItem Dim WsmItem On Error Resume Next Err.Clear Set WsmItem = tsm.LookupTreeItem(strWsmProfibusAdapter) set LocalItem = SysManagerItems.CreateChild(strLocalProfibusAdapter, LocalDeviceInfo) If err.number <> 0 Then wscript.Quit for i = 1 to WsmItem.ChildCount call WsmItem.ExportChild(WsmItem.Child(i).Name,strTempFolder & "box" & i & ".tce") call LocalItem.ImportChild(strTempFolder & "box" & i & ".tce") Call SetNewDpStateLinkCaller(WsmItem.Child(i).Name) next set WsmItem = nothing Dim ChildName ChildName = strWsmProfibusAdapter ChildName = Replace(ChildName,"TIID^","") Set WsmItem = Nothing Set LocalItem = Nothing Set SysManagerDevice = Nothing Set l_CurrentSysManagerDevice = Nothing call SysManagerItems.DeleteChild(ChildName) End If call tsm.SaveConfiguration(WsmFileName) '------------------------------- Funktionen ------------------------------------ Function GetCurrentPath() Dim l_strScriptName Dim l_strTemp l_strScriptName = WScript.ScriptFullName l_strTemp = WScript.ScriptName GetCurrentPath = Left(l_strScriptName, Len(l_strScriptName) - Len(l_strTemp)) End Function Function ExpandPath(p_strEnvironment,p_strDefault) Dim l_strTemp l_strTemp = wshshell.ExpandEnvironmentStrings(p_strEnvironment) If l_strTemp = p_strEnvironment Then l_strTemp = p_strDefault End If ExpandPath = l_strTemp End Function 'Sub Sub SetNewDpStateLinkCaller(p_strBox) Call SetNewDpStateLink(p_strBox,"^Eingänge^DpState") Call SetNewDpStateLink(p_strBox,"^Inputs^DpState") End Sub Sub SetNewDpStateLink(p_strBox,p_strBoxSubElem) 'Profibusadresse der Box herausbekommen Dim xml Dim nAddress Dim CurrentBox Dim BoxInfo ' Auswählen der Box - Bei ungültigen Verknüpfungen On Error Resume Next Err.Clear Set CurrentBox = tsm.LookupTreeItem("TIID^" & strLocalProfibusAdapter & "^" & p_strBox) 'Start If Err.Number <> 0 Then Err.Clear Exit Sub End If On Error Goto 0 Set xml = CreateObject("Msxml.DOMDocument") Call xml.loadXml(CurrentBox.ProduceXml) Set BoxInfo = xml.SelectSingleNode("TreeItem/BoxDef/FieldbusAddress") nAddress = BoxInfo.Text 'Den Namen der Variablen berechnen Dim strVariable Dim strVariableBody strVariableBody = "PROFIBUS^PROFIBUS[" & nAddress & "]^DpState" strVariable = "TIPC^" & strProject & "^TASK1^Inputs^" & strVariableBody 'Verknuepfen Dim strBox strBox = "TIID^" & strLocalProfibusAdapter & "^" & p_strBox & p_strBoxSubElem On Error Resume Next Call tsm.LinkVariables(strVariable,strBox) On Error Goto 0 End sub