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

断了今生、忘了曾经 提交于 2019-12-23 05:47:14

问题


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.


回答1:


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();
        }
    }



回答2:


You can use the TwinCAT automation interface, which is accessible by linking the automation interface DLL from a C#/.NET-program.

Specifically, to start/restart TwinCAT you use the ITcSysManager::StartRestartTwinCAT - method



来源:https://stackoverflow.com/questions/54077462/how-to-startup-shutdown-twincat-system-from-console-c-sharp-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!