param([int]$Samples = 20, [int]$IntervalMs = 500) $ErrorActionPreference = 'Stop' $adsDll = 'C:\Program Files (x86)\Beckhoff\TwinCAT\3.1\Components\Base\v170\TwinCAT.Ads.dll' Add-Type -Path $adsDll $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 } $lags = New-Object System.Collections.Generic.List[int] for ($k = 0; $k -lt $Samples; $k++) { $lag = ReadSym 'GVL_Sync.nMatchLag' ([int]) $bs = ReadSym 'GVL_Sync.bSync' ([bool]) $lags.Add($lag) Write-Host ("{0} lag={1,4} cyc bSync={2}" -f (Get-Date -Format 'HH:mm:ss'), $lag, $bs) if ($k -lt $Samples - 1) { Start-Sleep -Milliseconds $IntervalMs } } $s = $lags | Sort-Object Write-Host ("min={0} p50={1} max={2} mean={3:F2}" -f $s[0], $s[[int]($Samples/2)], $s[$Samples-1], (($lags | Measure-Object -Average).Average)) $c.Dispose()