#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 ReadSym($name, $type) { $h = $c.CreateVariableHandle($name) $v = $c.ReadAny($h, $type) $c.DeleteVariableHandle($h) return $v } function ReadBytes($name, $len) { $h = $c.CreateVariableHandle($name) $arr = $c.ReadAny($h, [byte[]], @($len)) $c.DeleteVariableHandle($h) return $arr } $nc = ReadSym 'GVL_Sync.nCycles' ([uint64]) $bs = ReadSym 'GVL_Sync.bSync' ([bool]) $nrs = ReadSym 'GVL_Sync.nReturnedSeq' ([uint64]) $nm = ReadSym 'GVL_Sync.nMismatch' ([int]) $in = ReadBytes 'GVL_Sync.aIn1' 800 $out = ReadBytes 'GVL_Sync.aOut1' 800 $nzIn = ($in | Where-Object { $_ -ne 0 }).Count $nzOut = ($out | Where-Object { $_ -ne 0 }).Count Write-Host ("nCycles={0} bSync={1} nReturnedSeq={2} lag={3} nMismatch={4}" -f $nc, $bs, $nrs, ($nc - $nrs), $nm) Write-Host ("aIn1 nonzero = {0}/800" -f $nzIn) Write-Host ("aOut1 nonzero = {0}/800" -f $nzOut) # per-byte pattern check against nReturnedSeq: expected[i] = (nReturnedSeq + i) mod 256, pattern region $bad = 0 $badIdx = @() for ($i = 16; $i -lt 800; $i++) { if ($i -ge 264 -and $i -le 279) { continue } $exp = [byte](($nrs + $i) % 256) if ($in[$i] -ne $exp) { $bad++; if ($badIdx.Count -lt 8) { $badIdx += $i } } } Write-Host ("pattern[16..799 excl 264..279] mismatches = {0} {1}" -f $bad, $(if ($badIdx.Count) { 'at ' + ($badIdx -join ',') } else { '' })) # show window samples at several offsets foreach ($off in @(0, 16, 128, 256, 288, 512, 768)) { $seg = [BitConverter]::ToString($in[$off..($off + 7)]) Write-Host ("aIn1[{0,3}..{1,3}] = {2}" -f $off, ($off + 7), $seg) } $c.Dispose()