TwinCAT 3.0 Automation Interface without Visual Studio?

与世无争的帅哥 提交于 2019-12-11 12:06:00

问题


I need to startup/shutdown TwinCAT 3.0 from a C# application. As kindly answered in How to startup / shutdown TwinCAT System from console / C# program? I can use TwinCAT Automation Interface. While in TC 2.0 was possible to simply instantiate Automation Interface with:

var systemManager = new TcSysManager(); // missing method exception: 
                                        //  no constructor without parameters defined

In TC 3 it gives me the above runtime error.

It seems that I need a Visual Studio instance on the PC where I want to use Automation Interface. The panel PC with the automation is on a machine and does not has VS Installed.

Is it possible to use Automation Interface or, alternatively, programmatically startup/shutdown TC 3.0 without having Visual Studio installed on the machine? Thanks.


回答1:


The answer below is for starting and stopping a specific PLC instance. To change the entire 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();
        }
    }

To Start or Stop the TwinCAT runtime programmatically, you can use ADS commands to change AdsState to Run or Stop. Beckhoff provides C# and C++ libraries for this. A C# example:

static void Main(string[] args)
{
    //Create a new instance of class TcAdsClient
    TcAdsClient tcClient = new TcAdsClient();

    try
    {
        // Connect to local PLC - Runtime 1 - TwinCAT 3 Port=851
        tcClient.Connect(851);

                Console.WriteLine(" PLC Run\t[R]");
                Console.WriteLine(" PLC Stop\t[S]");
                Console.WriteLine("\r\nPlease choose \"Run\" or \"Stop\" and confirm with enter..");
                string sInput = Console.ReadLine().ToLower();

        //Process user input and apply chosen state
        do{
            switch (sInput)
            {
                case "r": tcClient.WriteControl(new StateInfo(AdsState.Run, tcClient.ReadState().DeviceState)); break;
                case "s": tcClient.WriteControl(new StateInfo(AdsState.Stop, tcClient.ReadState().DeviceState)); break;
                default: Console.WriteLine("Please choose \"Run\" or \"Stop\" and confirm with enter.."); sInput = Console.ReadLine().ToLower(); break;
            }
        } while (sInput != "r" && sInput != "s");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        Console.ReadKey();
    }
    finally
    {
        tcClient.Dispose();
    }
}

Source: https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_adssamples_c/html/TcAdsDll_API_CPP_Sample06.htm&id=

A C++ example is here: https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_adssamples_c/html/TcAdsDll_API_CPP_Sample06.htm&id=


To my knowledge, Automation Interface requires at least Visual Studio Shell to be installed. When you use Automation Interface, you can see an instance of devenv.exe that gets launched in the background.




回答2:


Almost right, Port must be AmsPort.SystemService (10000)
THEN
To Restart PLC from config use AdsState.Reset (.Run will not work)
To Set PLC in ConfigMode use AdsState.Reconfig (.Config wil not work)

.Devstate: Could be 0 or what ever.

To Check if PLC is in RunMode or Config etc.. (some vb.net code)

 Dim tc As New TcAdsClient
 Dim AdsStateInfo as StateInfo
 Try
     tc.Connect("", AmsPort.SystemService) '(AmsPort.SystemService=10000)
     AdsStateInfo = tc.ReadState
  Catch ex As Exception
     AdsStateInfo.AdsState = TwinCAT.Ads.AdsState.Error
     AdsStateInfo.DeviceState = 7 ' Error7 if not found
 End Try
 MsgBox("AdsState: "+ AdsStateInfo.AdsState.ToString)


来源:https://stackoverflow.com/questions/54094565/twincat-3-0-automation-interface-without-visual-studio

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