#Requires -Version 5.1 #Requires -RunAsAdministrator $ErrorActionPreference = 'Continue' $LogFile = "C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Secondary\Logs\ads-readstate.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 ReadState 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-State($NetId, $Port, $Label) { $c = New-Object TwinCAT.Ads.TcAdsClient try { $c.Connect($NetId, $Port) $state = $c.ReadState() Write-Log "$Label state = $($state.AdsState) / deviceState = $($state.DeviceState)" } catch { Write-Log "$Label ERR: $($_.Exception.Message)" } finally { $c.Dispose() } } Read-State '169.254.176.217.1.1' 10000 'System(1.1,10000)' Read-State '169.254.176.217.1.1' 65535 'System(1.1,65535)' Read-State '169.254.176.217.2.1' 65535 'ECAT1(2.1,65535)' Read-State '169.254.176.217.2.1' 2 'ECAT1(2.1,2)' Read-State '169.254.176.217.1.1' 851 'PLC(1.1,851)' Write-Log "=== done ==="