Setting TwinCAT target platform

This article describes how to set the TwinCAT target platform via Automation Interface code. The target platform determines for which target the TwinCAT configuration should be compiled, e.g. TwinCAT x86 or TwinCAT x64, and is usually set from within a TwinCAT XAE toolbar in Visual Studio.

Setting TwinCAT target platform 1:

The following code snippet assumes that you already have a DTE instance that links to a TwinCAT configuration. It checks for the currently set target platform and re-sets it to another platform.

Code Snippet (C#):

ITcSysManager systemManager = pro.Object;
ITcSysManager7 sysManPlatform = (ITcSysManager7) systemManager;
ITcConfigManager configManager = (ITcConfigManager) sysManPlatform.ConfigurationManager;
if (configManager.ActiveTargetPlatform == "TwinCAT RT (x86)")
configManager.ActiveTargetPlatform = "TwinCAT RT (x64)";
else
configManager.ActiveTargetPlatform = "TwinCAT RT (x86)";

Code Snippet (Powershell):

$configManager = $systemManager.ConfigurationManager
if ($configManager.ActiveTargetPlatform -eq "TwinCAT RT (x86)"){
  $configManager.ActiveTargetPlatform = "TwinCAT RT (x64)"} else {$configManager.ActiveTargetPlatform = "TwinCAT RT (x86)"}

Note

Changing target hardware platform or remote target with TwinCAT configuration

This article describes how to change the target hardware platform. If you want to change the actual remote target to which the TwinCAT configuration should be written, please use the ITcSysManager::SetTargetNetId() method.

The following code snippet demonstrates how to acquire access to the Visual Studio Configuration Manager and enable or disable single TwinCAT sub projects (PLC, C++, …) for the build process. The snippet assumes that the currently opened TwinCAT project is named “TwinCAT Project1” and that it includes a PLC project “Untitled1”. It then disables the PLC project for the build process.

Code Snippet (C#):

EnvDTE.SolutionContexts solutionContexts = solution.SolutionBuild.ActiveConfiguration.SolutionContexts;
foreach (EnvDTE.SolutionContext solutionContext in solutionContexts)
{
switch(solutionContext.ProjectName)
{
case "TwinCAT Project13\\Untitled1\\Untitled1.plcproj":
solutionContext.ShouldBuild = false;
break;
}
}

Code Snippet (Powershell):

$solutionContexts = $sln.SolutionBuild.ActiveConfiguration.SolutionContexts
foreach ($solutionContext in $solutionContexts)
{
  if($solutionContext.ProjectName -eq "TestSolution\\Untitled1\\Untitled1.plcproj")
  {
    $solutionContext.ShouldBuild = $false
  }
}