Skip to content

Instantly share code, notes, and snippets.

@nikvoronin
Last active November 9, 2020 11:21
Show Gist options
  • Save nikvoronin/66c924bf03f5b31960fa47fe35d8bbbc to your computer and use it in GitHub Desktop.
Save nikvoronin/66c924bf03f5b31960fa47fe35d8bbbc to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using TwinCAT.Ads; // v4.4.0.0
namespace AdsReadSystemConfig
{
class Program
{
const string TARGET_AMS = "127.0.0.1.1.1";
const uint READDEVDESCRIPTION_IDX = 0x0000_02bc;
const uint READDEVDESCRIPTION_OFF = 0x0000_0001;
static void Main()
{
var client = new TcAdsClient();
var ams = new AmsAddress(TARGET_AMS, AmsPort.R3_CTRLPROG);
client.Connect(ams);
// length of the plc's xml description
int length = (int)client.ReadAny(READDEVDESCRIPTION_IDX, READDEVDESCRIPTION_OFF, typeof(int));
var adsStream = new AdsStream(length);
client.Read(READDEVDESCRIPTION_IDX, READDEVDESCRIPTION_OFF, adsStream);
string description = Encoding.UTF8.GetString(adsStream.ToArray()).TrimEnd('\0');
var xdoc = XDocument.Parse(description);
var elements = xdoc.Descendants();
Console.WriteLine(xdoc);
Console.WriteLine();
// looking for the name of PLC's OS
var osName = elements.FirstOrDefault(x => x.Name == "OsName")?.Value ?? "<Unknown>";
Console.WriteLine("Press [Enter] to close...");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment