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

94 lines
3.7 KiB
PowerShell
Raw Normal View History

#Requires -RunAsAdministrator
#Requires -Version 5.1
[CmdletBinding()]
param()
$ErrorActionPreference = 'Continue'
. "$PSScriptRoot\tc-common.ps1"
$Solution = "C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Primary\TwinCATProject1.sln"
$LogDir = "C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Primary\Logs"
New-Log $LogDir
$LogFile = Join-Path $LogDir "diag-online.log"
function Log($m) { Write-Log $m $LogFile }
Log "=== Online diagnostic (Primary) ==="
$dte = Get-Dte
if (-not (Open-Solution -Dte $dte -Solution $Solution)) { Log "FATAL: solution not open"; exit 1 }
$sysMgr = Get-SysManager -Dte $dte
if (-not $sysMgr) { Log "FATAL: no SysManager"; exit 1 }
try { $sysMgr.SetTargetNetId('169.254.176.217.1.1'); Log "TargetNetId set" } catch { Log "SetTargetNetId: $_" }
Start-Sleep -Seconds 3
try { Log "IsTwinCATStarted=$($sysMgr.IsTwinCATStarted) CurrentMode=$($sysMgr.CurrentMode)" } catch { Log "state read: $_" }
# --- EtherCAT master online XML ---
try {
$io = $sysMgr.LookupTreeItem('TIID')
$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 ($ecat) {
Log "Master: $($ecat.Name)"
$xml = $ecat.ProduceXml($true)
$xmlFile = Join-Path $LogDir 'diag_master_online.xml'
[System.IO.File]::WriteAllText($xmlFile, $xml, [System.Text.Encoding]::UTF8)
Log "Online XML saved: $xmlFile ($($xml.Length) bytes)"
for ($j = 1; $j -le [int]$ecat.ChildCount; $j++) {
try {
$b = $ecat.Child($j)
$sx = $b.ProduceXml($true)
$bx = Join-Path $LogDir ("diag_box_{0}.xml" -f $j)
[System.IO.File]::WriteAllText($bx, $sx, [System.Text.Encoding]::UTF8)
Log (" Box[{0}] {1} -> {2} ({3} bytes)" -f $j, $b.Name, $bx, $sx.Length)
} catch { Log " Box[$j] read err: $_" }
}
} else { Log "No EtherCAT master found" }
} catch { Log "Master XML: $_" }
# --- PLC variables via ADS (port 851) ---
$adsDll = 'C:\Program Files (x86)\Beckhoff\TwinCAT\3.1\Components\Base\v170\TwinCAT.Ads.dll'
try {
Add-Type -Path $adsDll -ErrorAction Stop
$c = New-Object TwinCAT.Ads.TcAdsClient
$c.Connect('169.254.176.217.1.1', 851)
function Read-Sym($name, $type) {
try {
$h = $script:c.CreateVariableHandle($name)
$v = $script:c.ReadAny($h, $type)
$script:c.DeleteVariableHandle($h)
return $v
} catch { return "ERR: $($_.Exception.Message)" }
}
Log "nCycles = $(Read-Sym 'GVL_Sync.nCycles' ([uint32]))"
Log "bSync = $(Read-Sym 'GVL_Sync.bSync' ([bool]))"
Log "nReturnedSeq = $(Read-Sym 'GVL_Sync.nReturnedSeq' ([uint32]))"
Log "nProcessedSeq = $(Read-Sym 'GVL_Sync.nProcessedSeq' ([uint32]))"
Log "bProcessingOk = $(Read-Sym 'GVL_Sync.bProcessingOk' ([bool]))"
try {
$h = $c.CreateVariableHandle('GVL_Sync.aIn1')
$arr = $c.ReadAny($h, [byte[]], @(1024))
$c.DeleteVariableHandle($h)
Log ("aIn1[0..15] = " + ([BitConverter]::ToString($arr[0..15])))
$nz = ($arr | Where-Object { $_ -ne 0 }).Count
Log "aIn1 nonzero bytes = $nz / 1024"
} catch { Log "aIn1 read: $_" }
try {
$h = $c.CreateVariableHandle('GVL_Sync.aOut1')
$arr = $c.ReadAny($h, [byte[]], @(1024))
$c.DeleteVariableHandle($h)
Log ("aOut1[0..15] = " + ([BitConverter]::ToString($arr[0..15])))
$nz = ($arr | Where-Object { $_ -ne 0 }).Count
Log "aOut1 nonzero bytes = $nz / 1024"
} catch { Log "aOut1 read: $_" }
$c.Dispose()
} catch { Log "ADS: $_" }
Log "=== Done ==="