Search code examples
c#twincat

How to startup / shutdown TwinCAT System from console / C# program?


How could I start/restart/shutdoown TwinCAT System runtime from console or C# application? I need a functionality equivalent to the TwinCAT toolbar buttons System Start/Restart on the lower right. Thanks.


Solution

  • This can be accomplished be accomplished using the C# .net ADS library. To change the TwinCAT runtime between Config and Run, connect to the System Service ADS port (port 10000) and set the state to AdsState.Run or AdsState.Config.

    All valid state values can be found here. All the port values can be found here.

    static void Main(string[] args)
        {
            //Create a new instance of class TcAdsClient
            TcAdsClient tcClient = new TcAdsClient();
    
            try
            {
                // Connect to TwinCAT System Service port 10000
                tcClient.Connect(AmsPort.SystemService);
                // Send desired state
                tcClient.WriteControl(new StateInfo(AdsState.Config, tcClient.ReadState().DeviceState));
    
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
            finally
            {
                tcClient.Dispose();
            }
        }