下面的答案是用于启动和停止特定的 PLC 实例。要在 Config 和 Run 之间更改整个 TwinCAT 运行时,请连接到 System Service ADS 端口(端口 10000)并将状态设置为AdsState.Run
或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++ 库。AC# 示例:
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_net/185266187.html&id=6127144084214800894
这里有一个 C++ 示例:https ://infosys.beckhoff.com/english.php?content=../content/1033/tc3_adsdll2/9007199379562763.html&id=8444596171373533137
据我所知,自动化接口至少需要安装 Visual Studio Shell。devenv.exe
当您使用自动化接口时,您可以看到在后台启动的实例。