我需要从 C# 应用程序启动/关闭 TwinCAT 3.0。
正如善意回答的如何从控制台/C#程序启动/关闭TwinCAT系统? https://stackoverflow.com/questions/54077462/how-to-startup-shutdown-twincat-system-from-console-c-sharp-program我可以使用 TwinCAT 自动化接口。
在 TC 2.0 中,可以简单地实例化自动化接口:
var systemManager = new TcSysManager(); // missing method exception:
// no constructor without parameters defined
在 TC 3 中,它给了我上面的运行时错误。
我似乎需要在要使用自动化接口的 PC 上有一个 Visual Studio 实例。具有自动化功能的平板电脑位于机器上,并且未安装 VS。
是否可以使用自动化接口,或者以编程方式启动/关闭 TC 3.0,而无需在计算机上安装 Visual Studio?
谢谢。
下面的答案是启动和停止特定的 PLC 实例。要在配置和运行之间更改整个 TwinCAT 运行时,请连接到系统服务 ADS 端口(端口 10000)并将状态设置为AdsState.Run
or AdsState.Config
.
可以找到所有有效的状态值。可以找到所有端口值.
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();
}
}
Source:
一个 C++ 示例在这里:
据我所知,自动化接口至少需要安装 Visual Studio Shell。当您使用自动化接口时,您可以看到一个实例devenv.exe
在后台启动。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)