Execute an ADS broadcast search

To trigger a TwinCAT Broadcast search and find unknown remote ADS devices, the ConsumeXml() and ProduceXml() methods from the ITcSmTreeItem interface may be used.

General broadcast search

Code Snippet (C#):

string xmlString = "<TreeItem><RoutePrj><TargetList><BroadcastSearch>true</BroadcastSearch></TargetList></RoutePrj></TreeItem>";
ITcSmTreeItem routes = sysMan.LookupTreeItem("TIRR");
routes.ConsumeXml(xmlString);
string result = routes.ProduceXml();

Code Snippet (Powershell):

$xmlString = "<TreeItem><RoutePrj><TargetList><BroadcastSearch>true</BroadcastSearch></TargetList></RoutePrj></TreeItem>"
$routes = $systemManager.LookupTreeItem("TIRR")
$routes.ConsumeXml($xmlString)
$result = $routes.ProduceXml()

The variable “result” now contains an XML representation of all found ADS devices on the network. To select a specific device from that list, regular .NET mechanisms for XML handling may be used.

Code Snippet (C#):

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(result);
string amsNetId = xmlDocument.SelectSingleNode("//TreeItem/RoutePrj/TargetList/Target/IpAddr[text()=\"" + ipAddress + "\"]/../NetId").InnerText;
string name = xmlDocument.SelectSingleNode("//TreeItem/RoutePrj/TargetList/Target/IpAddr[text()=\"" + ipAddress + "\"]/../Name").InnerText;

Code Snippet (Powershell):

$xmlDocument = [xml]$result
$localAmsNetId = $xmlDocument.TreeItem.RoutePrj.Target

This information might then be used to add a route to that ADS device, as described in a separate documentation article.

Direct broadcast search

Requires at least TwinCAT 3.1 Build 4020.10 or higher.

To execute a broadcast search with a given hostname or IP address, the following XML structures can be used in ConsumeXml().

XML - Search for Hostname:

<TreeItem>
  <RoutePrj>
    <TargetList>
      <Search>CX-12345</Search>
    </TargetList>
  </RoutePrj>
</TreeItem>

XML - Search for IP address:

<TreeItem>
  <RoutePrj>
    <TargetList>
      <Search>172.17.60.153</Search>
    </TargetList>
  </RoutePrj>
</TreeItem>

A subsequent ProduceXml() will return the found host as follows:

XML - Found host:

<TreeItem>
  <RoutePrj>
    <TargetList>
      <Target>
        <Name>CX-12345</Name>
        <NetId>172.17.60.153.1.1</NetId>
        <IpAddr>172.17.60.153</IpAddr>
        <Version>3.1.4020</Version>
        <OS>Windows 7</OS>
      </Target>
    </TargetList>
  </RoutePrj>
</TreeItem>