#Requires -Version 5.1 #Requires -RunAsAdministrator $ErrorActionPreference = 'Continue' $LogFile = "C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Secondary\Logs\ads-probe.log" New-Item -ItemType Directory -Force -Path (Split-Path $LogFile) | Out-Null function Write-Log($Message) { $line = "[{0}] {1}" -f (Get-Date -Format 'HH:mm:ss'), $Message Write-Host $line Add-Content -Path $LogFile -Value $line -Encoding UTF8 } Write-Log "=== ADS probe ===" $adsDll = 'C:\Program Files (x86)\Beckhoff\TwinCAT\3.1\Components\Base\v170\TwinCAT.Ads.dll' try { Add-Type -Path $adsDll -ErrorAction Stop; Write-Log "TwinCAT.Ads loaded" } catch { Write-Log "FATAL: cannot load ADS dll: $_"; exit 1 } function Read-Ads($NetId, $Port, $IGrp, $IOff, $Len, $Label) { $c = New-Object TwinCAT.Ads.TcAdsClient try { $c.Connect($NetId, $Port) $buf = New-Object byte[] $Len $n = $c.Read([uint32]$IGrp, [uint32]$IOff, $buf, 0, $Len) $hex = [BitConverter]::ToString($buf, 0, $n) Write-Log "$Label OK n=$n : $hex" } catch { Write-Log "$Label ERR: $($_.Exception.Message)" } finally { $c.Dispose() } } # Try various system/EtherCAT ports and index groups based on project history Read-Ads '169.254.176.217.1.1' 10000 0 0 4 'SystemState(1.1,10000,0,0,4)' Read-Ads '169.254.176.217.1.1' 65535 0 0 4 'SystemState(1.1,65535,0,0,4)' Read-Ads '169.254.176.217.2.1' 65535 0x9 0 32 'ECATMaster(2.1,65535,0x9,0,32)' Read-Ads '169.254.176.217.2.1' 2 0x9 0 32 'ECATMaster(2.1,2,0x9,0,32)' Read-Ads '169.254.176.217.2.1' 10000 0x9 0 32 'ECATMaster(2.1,10000,0x9,0,32)' # Try PLC port 851 (if PLC runtime is up) Read-Ads '169.254.176.217.1.1' 851 0 0 4 'PLC(1.1,851,0,0,4)' Write-Log "=== ADS probe complete ==="