我正在寻找一种激活配置并通过C#更新启动项目的方法。 我的Twincat 3项目已编译,所有必需的文件都在/ _Boot文件夹中。 下一步是一个C#程序(实际上是单元测试),该程序将在我的PLC上加载并执行项目。
到目前为止,我已经阅读了Beckhoff信息系统,但是找不到任何提示。
答案 0 :(得分:2)
您需要Twincat自动化接口API才能激活您的配置并启动PLC。
官方文档中的示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE100;
using System.IO;
using TCatSysManagerLib;
namespace ActivatePreviousConfiguration{
class Program
{
static void Main(string[] args)
{
Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
EnvDTE.DTE dte = (EnvDTE.DTE)System.Activator.CreateInstance(t);
dte.SuppressUI = false;
dte.MainWindow.Visible = true;
EnvDTE.Solution sol = dte.Solution;
sol.Open(@"C:\Temp\SolutionFolder\MySolution1\MySolution1.sln");
EnvDTE.Project pro = sol.Projects.Item(1);
ITcSysManager sysMan = pro.Object;
sysMan.ActivateConfiguration();
sysMan.StartRestartTwinCAT();
}
}
}
使用此api还可以执行许多其他操作,例如为PLC生成代码。
您可以在此处找到文档:
答案 1 :(得分:2)
如果只有_Boot文件夹可供使用,则只需将_Boot \ TwinCAT RT(x64)\ Plc的内容复制到目标引导文件夹C:\ TwinCAT \ 3.1 \ Boot \ Plc并启动PLC通过ADS-Command。
PLC将使用替换后的已编译项目启动。
以下是官方ADS文档中启动plc的示例:
static void Main(string[] args)
{
//Create a new instance of class TcAdsClient
TcAdsClient tcClient = new TcAdsClient();
try
{
// Connect to local PLC - Runtime 1 - TwinCAT2 Port=801, TwinCAT3 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();
}
}