ethercat-linux/scripts/tc-ads-start-runtime.ps1

42 lines
1.5 KiB
PowerShell
Raw Permalink Normal View History

#Requires -Version 5.1
#Requires -RunAsAdministrator
$ErrorActionPreference = 'Continue'
$LogFile = "C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Secondary\Logs\tc-start-runtime.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 "=== Attempt to start TwinCAT runtime via ADS WriteControl ==="
$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 }
$netId = '169.254.176.217.1.1'
$port = 10000
$c = New-Object TwinCAT.Ads.TcAdsClient
try {
$c.Connect($netId, $port)
Write-Log "Connected to $netId : $port"
$before = $c.ReadState()
Write-Log "Before: AdsState = $($before.AdsState), DeviceState = $($before.DeviceState)"
Write-Log "Writing AdsState.Run..."
$stateInfo = New-Object TwinCAT.Ads.StateInfo([TwinCAT.Ads.AdsState]::Run, 0)
$c.WriteControl($stateInfo)
Write-Log "WriteControl Run OK"
Start-Sleep -Seconds 10
$after = $c.ReadState()
Write-Log "After: AdsState = $($after.AdsState), DeviceState = $($after.DeviceState)"
} catch {
Write-Log "ERR: $($_.Exception.Message)"
} finally { $c.Dispose() }
Write-Log "=== done ==="