ethercat-linux/scripts/tc-online-info.ps1

175 lines
5.2 KiB
PowerShell
Raw Normal View History

#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 "online-info.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 "=== TwinCAT EtherCAT Online Info ==="
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
$targetNetId = '169.254.176.217.1.1'
$platform = 'TwinCAT RT (x64)'
try {
$sysMgr.SetTargetNetId($targetNetId)
Write-Log "SetTargetNetId $targetNetId OK"
} catch {
Write-Log "SetTargetNetId error: $_"
}
try {
$sysMgr.ConfigurationManager.ActiveTargetPlatform = $platform
Write-Log "ActiveTargetPlatform set to $platform"
} catch {
Write-Log "Set platform error: $_"
}
Start-Sleep -Seconds 5
try {
$cfg = $sysMgr.ConfigurationManager.ActiveConfiguration
Write-Log "Active Target NetId: $($cfg.TargetNetId)"
Write-Log "Active Platform: $($sysMgr.ConfigurationManager.ActiveTargetPlatform)"
} catch {
Write-Log "Get active config error: $_"
}
# Activate configuration
Write-Log "Activating configuration..."
try {
$sysMgr.ActivateConfiguration()
Write-Log "ActivateConfiguration OK"
} catch {
Write-Log "ActivateConfiguration error: $_"
}
Start-Sleep -Seconds 5
# Find EtherCAT master
$io = $null
for ($i = 1; $i -le 10; $i++) {
try {
$io = $sysMgr.LookupTreeItem('TIID')
if ($io) { break }
} catch {}
Start-Sleep -Seconds 2
}
if (-not $io) {
Write-Log "FATAL: Cannot find TIID"
exit 1
}
$ecat = $null
for ($j = 1; $j -le [int]$io.ChildCount; $j++) {
try {
$ch = $io.Child($j)
if ([int]$ch.ItemSubType -eq 111) {
$ecat = $ch
break
}
} catch {}
}
if (-not $ecat) {
Write-Log "FATAL: No EtherCAT device under TIID"
exit 1
}
Write-Log "EtherCAT master: $($ecat.Name)"
# Try to read Online XML via ProduceXml(true)
Write-Log "Reading master Online XML (ProduceXml true)..."
try {
$xml = $ecat.ProduceXml($true)
$xmlFile = Join-Path $script:DefaultLogDir 'online_master.xml'
[System.IO.File]::WriteAllText($xmlFile, $xml, [System.Text.Encoding]::UTF8)
Write-Log "Online XML saved: $xmlFile ($($xml.Length) bytes)"
} catch {
Write-Log "ProduceXml(true) error: $_"
}
# Try to read offline XML too
Write-Log "Reading master Offline XML (ProduceXml false)..."
try {
$xml = $ecat.ProduceXml($false)
$xmlFile = Join-Path $script:DefaultLogDir 'offline_master.xml'
[System.IO.File]::WriteAllText($xmlFile, $xml, [System.Text.Encoding]::UTF8)
Write-Log "Offline XML saved: $xmlFile ($($xml.Length) bytes)"
} catch {
Write-Log "ProduceXml(false) error: $_"
}
# Try to enumerate child boxes
Write-Log "Enumerating configured boxes under EtherCAT master..."
for ($j = 1; $j -le [int]$ecat.ChildCount; $j++) {
try {
$b = $ecat.Child($j)
Write-Log " Box[$j]: name='$($b.Name)' subtype=$($b.ItemSubType)"
} catch {}
}
# Try to find Online tab via different paths
$onlinePaths = @(
'TIID^设备 1876033536 (EtherCAT)^Online',
'TIID^设备 1876033536 (EtherCAT)^I/O online',
'TIID^设备 1876033536 (EtherCAT)^Online view',
'TIID^设备 1876033536 (EtherCAT)^Status'
)
foreach ($p in $onlinePaths) {
try {
$node = $sysMgr.LookupTreeItem($p)
if ($node) {
Write-Log "Found online node at path: $p"
try {
$xml = $node.ProduceXml($false)
$xmlFile = Join-Path $script:DefaultLogDir ("online_node_" + ($p -replace '[^a-zA-Z0-9]', '_') + ".xml")
[System.IO.File]::WriteAllText($xmlFile, $xml, [System.Text.Encoding]::UTF8)
Write-Log " XML saved: $xmlFile ($($xml.Length) bytes)"
} catch {}
}
} catch {
Write-Log "Path $p not found: $($_.Exception.Message)"
}
}
Write-Log "=== Online info capture complete ==="
} finally {
try { $dte.Quit() } catch {}
}