49 lines
2.1 KiB
PowerShell
49 lines
2.1 KiB
PowerShell
|
|
#Requires -Version 5.1
|
||
|
|
#Requires -RunAsAdministrator
|
||
|
|
$ErrorActionPreference = 'Continue'
|
||
|
|
|
||
|
|
$LogFile = "C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Secondary\Logs\tc-activate-rm-errors.log"
|
||
|
|
New-Item -ItemType Directory -Force -Path (Split-Path $LogFile) | Out-Null
|
||
|
|
function Write-Log($Message) {
|
||
|
|
$line = "[{0}] {1}" -f (Get-Date -Format 'HH:mm:ss'), $Message
|
||
|
|
Write-Host $line
|
||
|
|
Add-Content -Path $LogFile -Value $line -Encoding UTF8
|
||
|
|
}
|
||
|
|
|
||
|
|
Write-Log "=== Activate via TcSysManagerRM with error capture ==="
|
||
|
|
$rm = New-Object -ComObject TcSysManagerRM.1
|
||
|
|
$sm = $rm.CreateSysManager15('')
|
||
|
|
$sm.SetTargetNetId('169.254.176.217.1.1')
|
||
|
|
$sm.OpenConfiguration('C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Secondary\TwinCAT_EL6695_Secondary.tsproj')
|
||
|
|
Write-Log "OpenConfiguration OK"
|
||
|
|
|
||
|
|
try {
|
||
|
|
$cfg = $sm.ConfigurationManager
|
||
|
|
Write-Log "ConfigurationManager = $cfg"
|
||
|
|
Write-Log "ActiveTargetPlatform = $($cfg.ActiveTargetPlatform)"
|
||
|
|
Write-Log "ActiveConfiguration = $($cfg.ActiveConfiguration)"
|
||
|
|
$cfg.ActiveTargetPlatform = 'TwinCAT RT (x64)'
|
||
|
|
Write-Log "Set ActiveTargetPlatform OK"
|
||
|
|
} catch { Write-Log "ConfigManager ERR: $_" }
|
||
|
|
|
||
|
|
try { $sm.ActivateConfiguration(); Write-Log "ActivateConfiguration OK" } catch { Write-Log "ActivateConfiguration ERR: $_" }
|
||
|
|
Write-Log "Errors after Activate: $($sm.GetLastErrorMessages())"
|
||
|
|
Start-Sleep -Seconds 5
|
||
|
|
|
||
|
|
try { $sm.StartRestartTwinCAT(); Write-Log "StartRestartTwinCAT OK" } catch { Write-Log "StartRestartTwinCAT ERR: $_" }
|
||
|
|
Write-Log "Errors after StartRestart: $($sm.GetLastErrorMessages())"
|
||
|
|
Start-Sleep -Seconds 15
|
||
|
|
|
||
|
|
# ADS diagnostics
|
||
|
|
$adsDll = 'C:\Program Files (x86)\Beckhoff\TwinCAT\3.1\Components\Base\v170\TwinCAT.Ads.dll'
|
||
|
|
Add-Type -Path $adsDll
|
||
|
|
function Read-State($NetId, $Port, $Label) {
|
||
|
|
$c = New-Object TwinCAT.Ads.TcAdsClient
|
||
|
|
try { $c.Connect($NetId, $Port); $s = $c.ReadState(); Write-Log "$Label state = $($s.AdsState) / $($s.DeviceState)" } catch { Write-Log "$Label ERR: $($_.Exception.Message)" } finally { $c.Dispose() }
|
||
|
|
}
|
||
|
|
Read-State '169.254.176.217.1.1' 10000 'System'
|
||
|
|
Read-State '169.254.176.217.1.1' 851 'PLC'
|
||
|
|
Read-State '169.254.176.217.2.1' 65535 'ECAT1'
|
||
|
|
|
||
|
|
Write-Log "=== done ==="
|