69 lines
2.5 KiB
PowerShell
69 lines
2.5 KiB
PowerShell
|
|
#Requires -Version 5.1
|
||
|
|
#Requires -RunAsAdministrator
|
||
|
|
$ErrorActionPreference = 'Continue'
|
||
|
|
|
||
|
|
$LogFile = "C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Secondary\Logs\tc-activate-rm.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 "=== Activate via TcSysManagerRM ==="
|
||
|
|
|
||
|
|
$rm = New-Object -ComObject TcSysManagerRM.1
|
||
|
|
Write-Log "TcSysManagerRM version = $($rm.Version)"
|
||
|
|
$sm = $rm.CreateSysManager15('')
|
||
|
|
Write-Log "SysManager15 created"
|
||
|
|
|
||
|
|
try { $sm.SetTargetNetId('169.254.176.217.1.1'); Write-Log "SetTargetNetId OK" } catch { Write-Log "SetTargetNetId ERR: $_" }
|
||
|
|
|
||
|
|
try {
|
||
|
|
$sm.OpenConfiguration('C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Secondary\TwinCAT_EL6695_Secondary.tsproj')
|
||
|
|
Write-Log "OpenConfiguration(tsproj) OK"
|
||
|
|
} catch { Write-Log "OpenConfiguration(tsproj) ERR: $_" }
|
||
|
|
|
||
|
|
try {
|
||
|
|
$cfg = $sm.ConfigurationManager
|
||
|
|
$cfg.ActiveTargetPlatform = 'TwinCAT RT (x64)'
|
||
|
|
Write-Log "ActiveTargetPlatform set to TwinCAT RT (x64)"
|
||
|
|
} catch { Write-Log "Set platform ERR: $_" }
|
||
|
|
|
||
|
|
try {
|
||
|
|
$sm.ActivateConfiguration()
|
||
|
|
Write-Log "ActivateConfiguration OK"
|
||
|
|
Start-Sleep -Seconds 2
|
||
|
|
$err = $sm.GetLastErrorMessages()
|
||
|
|
if ($err) { Write-Log "ActivateConfiguration messages: $err" }
|
||
|
|
} catch { Write-Log "ActivateConfiguration ERR: $_" }
|
||
|
|
Start-Sleep -Seconds 5
|
||
|
|
|
||
|
|
try {
|
||
|
|
$sm.StartRestartTwinCAT()
|
||
|
|
Write-Log "StartRestartTwinCAT OK"
|
||
|
|
Start-Sleep -Seconds 2
|
||
|
|
$err = $sm.GetLastErrorMessages()
|
||
|
|
if ($err) { Write-Log "StartRestartTwinCAT messages: $err" }
|
||
|
|
} catch { Write-Log "StartRestartTwinCAT ERR: $_" }
|
||
|
|
Start-Sleep -Seconds 15
|
||
|
|
|
||
|
|
# ADS diagnostics
|
||
|
|
$adsDll = 'C:\Program Files (x86)\Beckhoff\TwinCAT\3.1\Components\Base\v170\TwinCAT.Ads.dll'
|
||
|
|
Add-Type -Path $adsDll
|
||
|
|
function Read-State($NetId, $Port, $Label) {
|
||
|
|
$c = New-Object TwinCAT.Ads.TcAdsClient
|
||
|
|
try {
|
||
|
|
$c.Connect($NetId, $Port)
|
||
|
|
$s = $c.ReadState()
|
||
|
|
Write-Log "$Label state = $($s.AdsState) / deviceState = $($s.DeviceState)"
|
||
|
|
} catch { Write-Log "$Label ERR: $($_.Exception.Message)" } finally { $c.Dispose() }
|
||
|
|
}
|
||
|
|
Read-State '169.254.176.217.1.1' 10000 'System'
|
||
|
|
Read-State '169.254.176.217.1.1' 851 'PLC'
|
||
|
|
Read-State '169.254.176.217.2.1' 65535 'ECAT1'
|
||
|
|
|
||
|
|
try { Write-Log "IsTwinCATStarted = $($sm.IsTwinCATStarted())" } catch { Write-Log "IsTwinCATStarted ERR: $_" }
|
||
|
|
|
||
|
|
Write-Log "=== done ==="
|