ethercat-linux/scripts/tc-activate.ps1

92 lines
2.7 KiB
PowerShell
Raw Permalink Normal View History

#Requires -Version 5.1
<#
.SYNOPSIS
Activate the TwinCAT configuration (download to target + restart).
.DESCRIPTION
Uses ITcSysManager.ActivateConfiguration() the automation interface
method, NOT the localized DTE command name. This avoids the Chinese
locale command-name mismatch that plagues ExecuteCommand("TwinCAT.激活配置").
After activation, calls StartRestartTwinCAT to bring the target to RUN.
.PARAMETER Solution
Path to the .sln file. Default points to the EL6695 Secondary project.
.PARAMETER TargetNetId
AMS NetID of the local TwinCAT target. Default: 10.0.0.106.1.1
.PARAMETER Visible
Show the TwinCAT XAE window (for debugging).
.EXAMPLE
.\tc-activate.ps1
.\tc-activate.ps1 -Visible
#>
[CmdletBinding()]
param(
[string]$Solution = $null,
[string]$TargetNetId = "169.254.176.217.1.1",
[switch]$Visible
)
. "$PSScriptRoot\tc-common.ps1"
if (-not $Solution) { $Solution = $script:DefaultSolution }
New-Log $script:DefaultLogDir
$LogFile = Join-Path $script:DefaultLogDir "activate.log"
Write-Log "=== Activate Configuration ===" $LogFile
Write-Log "Solution: $Solution" $LogFile
Write-Log "TargetNetId: $TargetNetId" $LogFile
try {
$dte = Get-Dte -Visible:$Visible
} catch {
Write-Log "FATAL: Cannot create TcXaeShell.DTE.17.0 — run Register-TcXaeCom.ps1 first" $LogFile
exit 1
}
try {
if (-not (Open-Solution -Dte $dte -Solution $Solution)) {
throw "Solution did not open"
}
$sysMgr = Get-SysManager -Dte $dte
# Set target NetID
try { $sysMgr.SetTargetNetId($TargetNetId) } catch { Write-Log "SetTargetNetId warning: $_" $LogFile }
try { $sysMgr.ConfigurationManager.ActiveTargetPlatform = "TwinCAT RT (x64)" } catch { }
# Save before activate
Write-Log "Saving project..." $LogFile
try { $sysMgr.SaveConfiguration() } catch { Write-Log "Save warning: $_" $LogFile }
# Activate
Write-Log "Calling ActivateConfiguration()..." $LogFile
$sysMgr.ActivateConfiguration()
Write-Log "ActivateConfiguration() returned OK" $LogFile
# Wait for build to settle
$info = Wait-Busy -Dte $dte
Write-Log "Build settled, LastBuildInfo=$info" $LogFile
# Restart TwinCAT on target
Write-Log "Calling StartRestartTwinCAT()..." $LogFile
try {
$sysMgr.StartRestartTwinCAT()
Write-Log "StartRestartTwinCAT() OK" $LogFile
} catch {
Write-Log "StartRestartTwinCAT warning: $_" $LogFile
}
Start-Sleep -Seconds 5
# Save after activate
try { $dte.Solution.SaveAs($Solution) } catch { Write-Log "Save warning: $_" $LogFile }
Write-Log "=== Activate complete ===" $LogFile
}
finally {
try { $dte.Quit() } catch { }
}