没有Visual Studio的TwinCAT 3.0自动化接口?

时间:2019-01-08 15:05:07

标签: c# twincat

我需要从C#应用程序启动/关闭TwinCAT 3.0。 诚如How to startup / shutdown TwinCAT System from console / C# program?所言,我可以使用TwinCAT自动化界面。 在TC 2.0中,可以通过以下方式简单地实例化Automation Interface:

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

在TC 3中,它给了我以上运行时错误。

似乎我想在PC上使用Visual Studio实例来使用自动化接口。具有自动化功能的平板电脑在计算机上,没有安装VS。

是否可以在不将Visual Studio安装在计算机上的情况下使用自动化接口,或者以编程方式启动/关闭TC 3.0? 谢谢。

2 个答案:

答案 0 :(得分:2)

下面的答案用于启动和停止特定的PLC实例。要在配置和运行之间更改整个TwinCAT运行时,请连接到系统服务ADS端口(端口10000),并将状态设置为AdsState.RunAdsState.Config

可以找到所有有效状态值here。可以在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();
        }
    }

要以编程方式启动或停止TwinCAT运行时,可以使用ADS命令将AdsState更改为“运行”或“停止”。 Beckhoff为此提供了C#和C ++库。一个C#示例:

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

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

一个C ++示例在这里:https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_adssamples_c/html/TcAdsDll_API_CPP_Sample06.htm&id=


据我所知,自动化接口至少需要安装Visual Studio Shell。使用自动化接口时,您会看到在后台启动的devenv.exe实例。

答案 1 :(得分:1)

几乎正确,端口必须是AmsPort.SystemService(10000)
然后
要从配置中重新启动PLC,请使用AdsState.Reset(.Run将不起作用)
要将PLC设置为ConfigMode,请使用AdsState.Reconfig(.Config将不起作用)

.Devstate:可以为0或任何其他值。

要检查PLC是否处于RunMode或Config等。(某些vb.net代码)

 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)