#Requires -Version 5.1 <# .SYNOPSIS Scan the EtherCAT bus and list discovered devices. .DESCRIPTION Opens the solution, switches to CONFIG mode (so scanning is possible), then polls the EtherCAT device's ProduceXml for FoundDevices/FoundBoxes. .PARAMETER Solution Path to the .sln file. .EXAMPLE .\tc-scan.ps1 #> [CmdletBinding()] param( [string]$Solution = $null, [string]$TargetNetId = "10.0.0.106.1.1" ) . "$PSScriptRoot\tc-common.ps1" if (-not $Solution) { $Solution = $script:DefaultSolution } New-Log $script:DefaultLogDir $LogFile = Join-Path $script:DefaultLogDir "scan.log" Write-Log "=== EtherCAT Scan ===" $LogFile $dte = Get-Dte -Visible:$true try { if (-not (Open-Solution -Dte $dte -Solution $Solution)) { throw "Solution did not open" } $sysMgr = Get-SysManager -Dte $dte try { $sysMgr.SetTargetNetId($TargetNetId) } catch { } # Activate to go online (needed for scanning) Write-Log "Activating config to go online..." $LogFile try { $sysMgr.ActivateConfiguration() } catch { Write-Log "Activate warning: $_" $LogFile } Start-Sleep -Seconds 10 # Find EtherCAT device $io = Find-TreeItem -SysMgr $sysMgr -Paths @("TIID") if (-not $io) { throw "TIID not found" } # Enumerate children to find EtherCAT device foreach ($child in $io) { $xml = $null try { $xml = $child.ProduceXml($false) } catch { } if ($xml -and $xml -match "(\d+)") { $sub = [int]$Matches[1] if ($sub -in @(90, 91, 92) -or $child.Name -like "*EtherCAT*") { Write-Log "EtherCAT device: $($child.Name) SubType=$sub" $LogFile # Poll for boxes for ($poll = 0; $poll -lt 10; $poll++) { Start-Sleep -Seconds 2 $devXml = $child.ProduceXml($false) if ($devXml -match "FoundBoxes") { Write-Log "FoundBoxes detected" $LogFile break } } # List boxes foreach ($box in $child) { Write-Log " Box: $($box.Name) Type=$($box.ItemType)" $LogFile } # Save XML $outFile = Join-Path $script:DefaultLogDir "scan_device.xml" [System.IO.File]::WriteAllText($outFile, $child.ProduceXml($true)) Write-Log "Device XML saved: $outFile" $LogFile break } } } Write-Log "=== Scan complete ===" $LogFile } finally { try { $dte.Quit() } catch { } }