using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using TCatSysManagerLib;
using System.Reflection;
using System.IO;
namespace download_test
{
class Program
{
private static void PlcControlLoginDownload(string TCatPlcCtrl_exe_path, string project_file_path, string command_file_path)
{
Process process = new Process();
process.StartInfo.FileName = TCatPlcCtrl_exe_path;
process.StartInfo.Arguments = string.Format("{0} /cmd {1}", project_file_path, command_file_path);
process.Start();
bool ok = process.WaitForExit(5000);
Debug.Assert(ok);
}
private static void SystemManagerActivateConfiguration(string _tsmPath)
{
try
{
ITcSysManager _sysManager = new TcSysManager();
_sysManager.OpenConfiguration(_tsmPath);
System.Threading.Thread.Sleep(2000); // nicht zu schnell sonst wird der SystemManager nicht fertig (COM-Exception)
_sysManager.ActivateConfiguration();
_sysManager.StartRestartTwinCAT();
System.Threading.Thread.Sleep(10000); // nicht zu schnell sonst wird der SystemManager nicht fertig
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Debug.Assert(false);
}
}
private static void TestRun(string tsmPath, string TCatPlcCtrl_exe_path, string project_file_path, int rounds)
{
string command_file_path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\commands.txt";
string[] lines = { "query off ok", "online login", "online run", "file quit" };
string content = string.Join("\r\n", lines) + "\r\n";
File.WriteAllText(command_file_path, content);
for (int i = 0; i < rounds; ++i)
{
SystemManagerActivateConfiguration(tsmPath);
PlcControlLoginDownload(TCatPlcCtrl_exe_path, project_file_path, command_file_path);
}
}
static void Main(string[] args)
{
string tsmPath = @"D:\projects\beckhoff\testprojekt\hw.tsm";
string TCatPlcCtrl_exe_path = @"C:\TwinCAT\Plc\TCatPlcCtrl.exe";
string project_file_path = @"D:\projects\beckhoff\testprojekt\sw.pro";
TestRun(tsmPath, TCatPlcCtrl_exe_path, project_file_path, 10);
}
}
}