How to manage routes

Routes can be added using ConsumeXml on the route node. It is not possible to delete items directly via the Automation Interface. An alternative is to edit StaticRoutes.xml directly. The project's target Net ID can be easily set and retrieved via ITcSysManager2.

How to manage routes 1:

Only unencrypted routes are created using this method; ADS Secure / encrypted routes are not supported.

Requirements

TwinCAT 3 is installed. To delete a route, you need write access to:
C:\TwinCAT\3.1\TargetStaticRoutes.xml

Adding a route using an IP address

C#:

string xmlString = "<TreeItem><RoutePrj><TargetList><BroadcastSearch>true</BroadcastSearch></TargetList><AddRoute><RemoteName>RouteName</RemoteName><RemoteNetId>10.1.128.217.1.1</RemoteNetId><RemoteIpAddr>10.1.128.217</RemoteIpAddr><UserName>Administrator</UserName><Password>1</Password><NoEncryption></NoEncryption></AddRoute></RoutePrj></TreeItem>";
ITcSmTreeItem routes = systemManager.LookupTreeItem("TIRR");
routes.ConsumeXml(xmlString);

PowerShell:

$xmlString ="<TreeItem><ItemName>Route Settings</ItemName><PathName>TIRR</PathName><RoutePrj><TargetList><BroadcastSearch>true</BroadcastSearch></TargetList><AddRoute><RemoteName>RouteName</RemoteName><RemoteNetId>10.1.128.217.1.1</RemoteNetId><RemoteIpAddr>10.1.128.217</RemoteIpAddr><UserName>Administrator</UserName><Password>1</Password><NoEncryption></NoEncryption></AddRoute></RoutePrj></TreeItem>"
$routes = $systemManager.LookupTreeItem("TIRR")
$routes.ConsumeXml($xmlString)

Adding a route using a hostname

<TreeItem>
  <ItemName>Route Settings</ItemName>
  <PathName>TIRR</PathName>
  <RoutePrj>
    <TargetList>
      <BroadcastSearch>true</BroadcastSearch>
    </TargetList>
    <AddRoute>
      <RemoteName>RouteName</RemoteName>
      <RemoteNetId>1.2.3.4.5.6</RemoteNetId>
      <RemoteHostName>CX-12345</RemoteHostName>
      <UserName>userName</UserName>
      <Password>password</Password>
      <NoEncryption></NoEncryption>
      <LocalName>LocalName</LocalName>
    </AddRoute>
  </RoutePrj>
</TreeItem>

Adding a project route

<TreeItem>
  <ItemName>Route Settings</ItemName>
  <PathName>TIRR</PathName>
  <RoutePrj>
    <TargetList>
      <BroadcastSearch>true</BroadcastSearch>
    </TargetList>
    <AddProjectRoute>
      <Name>RouteName</Name>
      <NetId>1.2.3.4.5.6</NetId>
      <IpAddr>1.2.3.4</IpAddr>
    </AddProjectRoute>
  </RoutePrj>
</TreeItem>
How to manage routes 2:

The XML structure for regular and project routes can be used simultaneously.

Setting and reading Net ID

// Net-ID setzen (ITcSysManager2)
ITcSysManager2 sysMan2 = (ITcSysManager2)sysManager;

sysMan2.SetTargetNetId("5.23.233.156.1.1");  // Auf Remote-System
sysMan2.SetTargetNetId("");                   // Zurück auf lokales System

string currentNetId = sysMan2.GetTargetNetId();

Deleting a route from StaticRoutes.xml

var xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(@"C:\TwinCAT\3.1\Target\StaticRoutes.xml");

string routeToRemove =
    "<Route>" +
        "<Name>CX-17E99C</Name>" +
        "<Address>CX-17E99C</Address>" +
        "<NetId>5.23.233.156.1.1</NetId>" +
        "<Type>TCP_IP</Type>" +
    "</Route>";

string xmlString = xmlDoc.InnerXml;
xmlString = xmlString.Replace(routeToRemove, "");
xmlDoc.InnerXml = xmlString;
xmlDoc.Save(@"C:\TwinCAT\3.1\Target\StaticRoutes.xml");
How to manage routes 3:

SetTargetNetId("") resets the project to the local AMS Net ID. GetTargetNetId() returns the Net ID currently set in the project. Changes to the StaticRoutes.xml take effect only after the TwinCAT service is restarted.