Setting the TwinCAT target platform

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

Setting the TwinCAT target platform 1:

The following code snippet assumes that you already have a DTE instance that connects to a TwinCAT configuration. It checks the currently set target platform and switches it to another platform.

Code snippet (C#):

ITcSysManager7 systemManager7 = pro.Object as ITcSysManager7;
ITcConfigManager configManager = systemManager7.ConfigurationManager as ITcConfigManager;
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)"}
Setting the TwinCAT target platform 2:

Changing the target hardware platform or target device with TwinCAT configuration?

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

The following code snippet shows how to access the Visual Studio Configuration Manager and activate or deactivate individual TwinCAT sub-projects (PLC, C++ ...) for the build process. The snippet assumes that the TwinCAT project currently open is called "TwinCAT Project1" and that this contains a PLC project "Untitled1". It then deactivates 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
  }
}