我需要从C#应用程序启动/关闭TwinCAT 3.0。如如何从控制台/ C#程序启动/关闭TwinCAT系统中所回答的那样?我可以使用TwinCAT自动化界面。在TC 2.0中,可以通过以下方式简单地实例化Automation Interface:
var systemManager = new TcSysManager(); // missing method exception:
// no constructor without parameters defined
在TC 3中,它给了我上面的运行时错误。
似乎我想在PC上使用Visual Studio实例来使用自动化接口。具有自动化功能的平板电脑位于计算机上,并且未安装VS。
是否可以使用自动化接口,或者以编程方式启动/关闭TC 3.0,而无需在计算机上安装Visual Studio?谢谢。
下面的答案用于启动和停止特定的PLC实例。要在Config和Run之间更改整个TwinCAT运行时,请连接到系统服务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
在后台启动的实例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句