ethercat-linux/scripts/tc-dte-restart-system.ps1

51 lines
1.9 KiB
PowerShell
Raw Normal View History

#Requires -Version 5.1
#Requires -RunAsAdministrator
$ErrorActionPreference = 'Continue'
$LogFile = "C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Secondary\Logs\dte-restart-system.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 "=== Restart TwinCAT System via DTE ==="
$dte = New-Object -ComObject TcXaeShell.DTE.17.0
$dte.MainWindow.Visible = $true
$dte.Solution.Open('C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Secondary\TwinCAT_EL6695_Secondary.sln')
for ($i=1; $i -le 120; $i++) { Start-Sleep -Seconds 1; if ($dte.Solution.IsOpen) { break } }
Write-Log "Solution open: $($dte.Solution.IsOpen)"
$cmdName = 'TwinCAT.重启TwinCAT系统'
try {
$cmd = $dte.Commands.Item($cmdName)
Write-Log "Command found: avail=$($cmd.IsAvailable) enabled=$($cmd.Enabled)"
if ($cmd.IsAvailable) {
$dte.ExecuteCommand($cmdName)
Write-Log "ExecuteCommand($cmdName) OK"
} else {
Write-Log "Command not available/enabled"
}
} catch { Write-Log "ERR: $($_.Exception.Message)" }
Start-Sleep -Seconds 10
# 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) / deviceState = $($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'
try { $dte.Quit() } catch {}
Write-Log "=== done ==="