89 lines
2.6 KiB
PowerShell
89 lines
2.6 KiB
PowerShell
|
|
#Requires -Version 5.1
|
||
|
|
[CmdletBinding()]
|
||
|
|
param(
|
||
|
|
[string]$Solution = $null
|
||
|
|
)
|
||
|
|
|
||
|
|
. "$PSScriptRoot\tc-common.ps1"
|
||
|
|
if (-not $Solution) { $Solution = $script:DefaultSolution }
|
||
|
|
|
||
|
|
New-Log $script:DefaultLogDir
|
||
|
|
$LogFile = Join-Path $script:DefaultLogDir "start-tc.log"
|
||
|
|
|
||
|
|
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 "=== Start TwinCAT Runtime via DTE ==="
|
||
|
|
|
||
|
|
try {
|
||
|
|
$dte = New-Object -ComObject TcXaeShell.DTE.17.0 -ErrorAction Stop
|
||
|
|
} catch {
|
||
|
|
Write-Log "FATAL: Cannot create DTE: $_"
|
||
|
|
exit 1
|
||
|
|
}
|
||
|
|
|
||
|
|
$dte.MainWindow.Visible = $true
|
||
|
|
|
||
|
|
try {
|
||
|
|
$dte.Solution.Open($Solution)
|
||
|
|
for ($i = 1; $i -le 120; $i++) {
|
||
|
|
Start-Sleep -Seconds 1
|
||
|
|
if ($dte.Solution.IsOpen) { break }
|
||
|
|
}
|
||
|
|
Write-Log "Solution open: $($dte.Solution.IsOpen)"
|
||
|
|
|
||
|
|
$sysMgr = $null
|
||
|
|
for ($i = 1; $i -le 40; $i++) {
|
||
|
|
try {
|
||
|
|
$sysMgr = $dte.Solution.Projects.Item(1).Object
|
||
|
|
if ($sysMgr) { break }
|
||
|
|
} catch {}
|
||
|
|
Start-Sleep -Seconds 3
|
||
|
|
}
|
||
|
|
Write-Log "SysManager: $(if ($sysMgr) { 'OK' } else { 'null' })"
|
||
|
|
if (-not $sysMgr) { exit 1 }
|
||
|
|
|
||
|
|
# Set target and platform with retry
|
||
|
|
$targetNetId = '169.254.176.217.1.1'
|
||
|
|
$platform = 'TwinCAT RT (x64)'
|
||
|
|
for ($i = 1; $i -le 10; $i++) {
|
||
|
|
try { $sysMgr.SetTargetNetId($targetNetId); Write-Log "SetTargetNetId OK (try $i)"; break } catch { Start-Sleep -Seconds 2 }
|
||
|
|
}
|
||
|
|
for ($i = 1; $i -le 10; $i++) {
|
||
|
|
try { $sysMgr.ConfigurationManager.ActiveTargetPlatform = $platform; Write-Log "Platform OK (try $i)"; break } catch { Start-Sleep -Seconds 2 }
|
||
|
|
}
|
||
|
|
|
||
|
|
Start-Sleep -Seconds 3
|
||
|
|
Write-Log "IsTwinCATStarted before: $($sysMgr.IsTwinCATStarted())"
|
||
|
|
|
||
|
|
Write-Log "Calling StartRestartTwinCAT()..."
|
||
|
|
try {
|
||
|
|
$sysMgr.StartRestartTwinCAT()
|
||
|
|
Write-Log "StartRestartTwinCAT() OK"
|
||
|
|
} catch {
|
||
|
|
Write-Log "StartRestartTwinCAT() error: $_"
|
||
|
|
}
|
||
|
|
Start-Sleep -Seconds 10
|
||
|
|
Write-Log "IsTwinCATStarted after: $($sysMgr.IsTwinCATStarted())"
|
||
|
|
|
||
|
|
# Also try ActivateConfiguration if not started
|
||
|
|
if (-not $sysMgr.IsTwinCATStarted()) {
|
||
|
|
Write-Log "TwinCAT not started. Trying ActivateConfiguration..."
|
||
|
|
try {
|
||
|
|
$sysMgr.ActivateConfiguration()
|
||
|
|
Write-Log "ActivateConfiguration OK"
|
||
|
|
} catch {
|
||
|
|
Write-Log "ActivateConfiguration error: $_"
|
||
|
|
}
|
||
|
|
Start-Sleep -Seconds 10
|
||
|
|
Write-Log "IsTwinCATStarted after activate: $($sysMgr.IsTwinCATStarted())"
|
||
|
|
}
|
||
|
|
|
||
|
|
Write-Log "=== Done ==="
|
||
|
|
} finally {
|
||
|
|
try { $dte.Quit() } catch {}
|
||
|
|
}
|