0

I´m looking for a way to activate the configuration and update the boot project via C#. My Twincat 3 project is compiled and all necessary file are in the /_Boot folder. Next step is a C# programm (actually unit tests) that loads and executes the project on my PLC.

So far I have read through Beckhoff Information System, but couldn´t find any hint.

downforme
  • 371
  • 2
  • 15

2 Answers2

4

You need the Twincat Automation Interface API in order to activate your configuration and start the PLC.

An example from the official documentation:

        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();
                }
            }
        }

There are also many other things you can do with this api, for example generate code for your PLC..

You can find the documentation here:

Automation Interface pdf

Filippo Boido
  • 1,136
  • 7
  • 11
  • Thanks for your answer. But in my context (unit tests) I only have the compiled output in /_Boot (zipped, as resource) available. – downforme Jul 11 '19 at 21:12
  • Let me understand better, you don't have the source code of the project you want to test and upload? – Filippo Boido Jul 11 '19 at 21:58
  • I´m not testing the PLC project, but a C# project talking to the PLC. So no, I don´t have the twincat solution available, only it´s compiled output – downforme Jul 12 '19 at 06:02
2

If you only have the _Boot folder at your disposal, you just have to copy the content of _Boot\TwinCAT RT(x64)\Plc to your target boot folder C:\TwinCAT\3.1\Boot\Plc and start the PLC via ADS-Command.

The PLC will boot with the replaced compiled project.

Here an example from the official ADS-Documentation for starting the 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();
    }
}
Filippo Boido
  • 1,136
  • 7
  • 11
  • ok thanks. thats basically the same process we use now with twincat 2, where I copy the .wpb file via net command. I guess there is no way to have the SysManager do the copying? – downforme Jul 12 '19 at 07:24
  • 1
    The SysManager in Twincat 3 is coupled with visual studio and without the Twincat solution you won't be able to make use of the Automation Interface API. The only way is to copy your boot files for example with the c# File.Copy Method to your target boot folder. – Filippo Boido Jul 12 '19 at 07:57