#Requires -RunAsAdministrator #Requires -Version 5.1 $ErrorActionPreference = 'Continue' $adsDll = 'C:\Program Files (x86)\Beckhoff\TwinCAT\3.1\Components\Base\v170\TwinCAT.Ads.dll' 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 = $c.CreateVariableHandle($name) $v = $c.ReadAny($h, $type) $c.DeleteVariableHandle($h) return $v } catch { return "ERR: $($_.Exception.Message)" } } function Read-Bytes($name) { try { $h = $c.CreateVariableHandle($name) $arr = $c.ReadAny($h, [byte[]], @(8)) $c.DeleteVariableHandle($h) return $arr } catch { return $null } } for ($i = 1; $i -le 8; $i++) { $nc = Read-Sym 'GVL_Sync.nCycles' ([uint32]) $bs = Read-Sym 'GVL_Sync.bSync' ([bool]) $lag = Read-Sym 'GVL_Sync.nMatchLag' ([int]) $nmis = Read-Sym 'GVL_Sync.nMismatch' ([int]) $out = Read-Bytes 'GVL_Sync.aOut1' $in = Read-Bytes 'GVL_Sync.aIn1' $outH = if ($out) { [BitConverter]::ToString($out) } else { 'ERR' } $inH = if ($in) { [BitConverter]::ToString($in) } else { 'ERR' } # per-byte verify against expected transform for current cycle (lag unknown here) $xorH = if ($in) { [BitConverter]::ToString([byte[]]($in | ForEach-Object { $_ -bxor 0x5A })) } else { 'ERR' } Write-Host ("[{0}] nCyc={1} bSync={2} lag={3} mism={4}" -f $i, $nc, $bs, $lag, $nmis) Write-Host (" aOut1(sent) = {0}" -f $outH) Write-Host (" aIn1(recv) = {0} aIn1^5A = {1}" -f $inH, $xorH) Start-Sleep -Seconds 3 } $c.Dispose()