Beckhoff#Let’s try Open Source Library TcOpen in TwinCAT3!

This article introduces the TwinCAT open source library TcOpen, which is available on the community, and explains how to use it. I will  also cover the concept of open source in general in this tutorial. Thanks to the integration of Beckhoff TwinCAT3 with Visual Studio, it is possible to link TwinCAT with .NET projects, opening up new possibilities. In the future, I will periodically share interesting features of TcOpen. Welcome to the world of open source!

Opensource?

Open source refers to a license that allows software source code to be publicly available for anyone to view, use, modify, and distribute freely. This concept aims to pursue values such as programmer and user freedom, collaboration, transparency, reliability, and security in software development. By making source code publicly available, other developers can reference and improve the code, thus promoting innovation and potentially reducing development costs.

Open source licenses permit free modification and distribution, making it easier for many people to participate in projects and contribute to their growth.

Advantage

There are several benefits to using open source software, including:

  • Freedom of use and modification: Open source software allows anyone to freely use, modify, and redistribute it, making it possible to customize or modify it to suit one’s own application needs.
  • Cost savings: Open source software is generally less expensive than proprietary software that requires licensing fees.
  • Quality improvement: Open source software is developed and improved by a community of developers, allowing for many people to review and examine the code, making it more likely that bugs or defects will be quickly discovered and corrected.
  • Flexibility: Open source software can be freely customized, allowing it to be used in a variety of environments without being dependent on a specific vendor. Additionally, providing an open API makes it easy to integrate with other systems, enhancing its flexibility.

Disadvantage

Of course, there are also some drawbacks to using open source software.

  • No guaranteed support: Open source projects are open to anyone, so the developers of the project are not necessarily responsible for it. Therefore, when using open source software, there is no guarantee of support, meaning it is at your own risk.
  • Security issues: Because open source software is accessible to anyone, malicious individuals can tamper with the software. Additionally, there may be undiscovered security vulnerabilities in open source software, meaning it is at your own risk.
  • Lack of documentation: Since anyone can contribute to an open source project, there is a possibility that different developers may provide varying documentation. Therefore, if the project’s documentation is insufficient, it may be difficult to use the project, meaning it is at your own risk.
  • Quality issues: Because many people contribute to open source software, the quality may not be consistent. Therefore, some software may have quality issues, meaning it is at your own risk.
  • License issues: Some open source software has licenses with usage restrictions. Therefore, if there is a potential violation of the license, it may not be possible to use the software, meaning it is your own responsibility to check the license.

PLCとOpensource?

The concept of open source is intended to promote the sharing, improvement, and evolution of software development, and is generally applied to software development. PLC programs are also a type of software and may be suitable for open source applications.

However, since PLC programs are often used for industrial purposes, safety and reliability are strongly demanded. Therefore, even if it is open source, development that complies with safety standards and protocols suitable for specific industrial applications may be necessary.

In addition, there are license terms associated with the use of open source, and it is necessary to use them in accordance with these terms. There may also be license issues with PLC programs, so they need to be properly managed.

Currently, it may not be practical to publish PLC programs as open source, but they may be able to be operated as open source in the following cases:

  • If it is for educational purposes: Open source licenses can be used if the PLC program is used for educational purposes.
  • If it is a standardized library: PLC programs published as standardized libraries can use open source licenses.

TcOpen?

TcOpen provides TwinCAT 3 code for automation professionals and has grown into a framework that addresses several issues in traditional PLC programming, such as configuration, composition, componentization, and connectivity to IT technologies.

Software development for industrial automation has evolved in a unique way, but TcOpen introduces PLCs to the current software development practices, allowing developers to shift from low-value-added tasks to high-value-added tasks.

Open source provides a robust foundation that allows many people to contribute ideas from all over the world. TcOpen is an object-oriented framework that utilizes OOP extensions defined in IEC 61131-3 ST (ST) and implemented in CoDeSys and TwinCAT3.

https://github.com/TcOpenGroup/TcOpen

Be careful!

TcOpen is currently under development and a stable version has not yet been released. The development team aims to release the first version in 2022 and continue to develop the project by frequently adding small features. TcOpen is currently being used by various companies and individuals.

Prerequisites

Visual Studio 2019

Community Edition v16.8.3+

https://visualstudio.microsoft.com/vs/older-downloads/

TwinCAT 3.1 eXtended Automation Engineering (XAE)

TwinCAT 3.1 4024.17+

https://www.beckhoff.com/english.asp?download/tc3-downloads.htm

.NET Framework 4.8 developer pack

https://dotnet.microsoft.com/download/dotnet-framework/thank-you/net48-developer-pack-offline-installer

.NET6 developer pack (6.0.5)

https://dotnet.microsoft.com/en-us/download/dotnet/6.0

Inxton Vortex Builder extension

https://marketplace.visualstudio.com/items?itemName=Inxton.InxtonVortexBuilderExtensionPre

Import TcOpen Library

Create TwinCAT Project

Launch the Visual Studio>Create a new Project to start your new project.

Choose TwinCAT XAE>Next.

Enter your Project Name and click Create.

Add PLC

Now we can insert the PLC Project by Right Click PLC>Add New Item.

Select the Standard PLC Project Template to create your plc project.

The PLC Project is created and be careful that your PLC project name will relate to the next step with Integrate to TcOpen.

Add New Project

Go to Solution>Right Click>Add<New Project to insert a new solution.

Choose the C# Console App and Next.

The name of your C# project should be in this format:

PLC project name+Connector

In this tutorial, my C# Project should be TcOpenPLCProjectConnector.

Done!Project is created.

Manage NuGet Packages

We can install the TcOpen Packages now by Right click the C# Project>Manage NuGet Packages.

Click the Gear icon in the right hand side to configure your Packages Source.

Add New Packages Sources

Go to NuGet Package Manager>Package Sources>+ Button to insert the new Packages.

Name:A easy-understand name

Source:https://api.nuget.org/v3/index.json

Browse and Install TcOpen

Go back to the NuGet Packages screen and Select nuget.org as your Package source.

Enter the checkbox of Include prerelease and Secarch TcOpen.Group>Install it.

OK.

Error .NETFrameWork,Version=4.7.2?

At first I got an error of .NETFrameWork,Version=4.7.2., it seems that my C# project ‘s.NETFrameWork = Version 4.7.2  is the main problem.

Create again

Create the new project again and configure it as Frame4.8.

Done!You can see the Licsense Acceptance screen in this time.Please accept it.

Output WindowsのところにPackagesがインストールされているとわかります。

The installation is done if you see a Green Check mark on the TcOpen.Group.

Managa Extensions

Vortex Tools must be installed to use TcOpen.Group’s Framework. Click Extensions > Manage Extensions.

Please wait a second..

Search for Inxton and Download the Inxton − Vortex Builder extension.

After downloading, please close Visual Studio once.

Restart Visual Studio

Visual Studio will automatically install the Extensions.

Modify.

Please wait a second..

Done!

Inxton Vortex Builder extension is installed in your environment.

Activate Inxton-Vortex Tools

Next, we need to enable Inxton-Vortex Tools in Visual Studio. Right click on the top of Visual Studio > Enable Inxton-Vortex Tools.

Done!Inxton-Vortex Tools related tools are displayed.

Build the TcOpen Library

We need to build the TcOpen library and import it into the TwinCAT PLC Project.

Click the button in the red frame to build the Library.

Build process is running..

the library of TcOpen.Group is imported as a TwinCAT external library, the information input screen of the library will appear during the build.

Enter the library information and check the box for Minimize Id Changes in TwinCAT Files in General.

Reload your project.

Error

Error is shown again – The attribute “xmlns” in element <ItemGroup> is unrecognized.

The attribute “xmlns” in element <ItemGroup> is unrecognized. plc1Connector C:\Users\root\Source\Repos\TwinCAT Project1\plc1Connector\plc1Connector.csproj 396

Download Packages

Launch your Visual Studio Installation file, and install the .NET 5.0 Runtime.

Create Project Again

Insert the C# Project into your Solution, but C# Class library(.NET Standard or .NET Core) is chosen.

Result

Now your project can be built without any error!

Implmentation1

Let me show you how to use TcOpen into your TwinCAT Project First.

Add

Let’s create a new Function Block in your project that is named SimpleContent.

Extends

Use the Extends Keyword to extend your Function block from TcoCore.TcoContext.

MAIN

Define a SimpleContent Function Block instance in your Main POU.

“There is no implementation for ABSTRACT method ‘main’ defined in function block is not defined while I try to compile my project.

Add ABSTRCT Method

Let’s insert the Main Method in your SimpleContent Function Block. Right Click the SimpleContent > Add > Method.

Enter the Method name with 100% the same of the ABSTRACT Method.In this case, It should be “Main”.

Main Method is inserted in your Function Block.

While you open the Main Method, you can see the comment that automatically generates from TcOpen Library.

Now we can create some sample programs by using the THIS^. keyword to get the _startCycleCount Property to get the current Cycle Count. If the Cycle Count MOD 100=0, the system will trigger the Logger.Push Method.

The Logger.Push Method will save the text message/Time..etc information inside the Instance memory.

Do not forget to call the Run() Method, it is the main method to run the code of SimpleContent Function Block.

Result

As you see , _plcCarret is increasing cyclically – The SimpleContent Function Block is running. 

If you take a look at the _logger._buffer, the logging message/Text/Catalog are saved inside.

Implmentation2

Then I will show you how to connect the C# Project to TwinCAT by using TcOpen library.

ReBuild your project

Now we use the Inxton-Vortex Tools to rebuild our C# project. It is a very important step.

Check Path

Add Tc3Target to your environment. The Variable name is Tc3 Target and the value is your AMS NetID.

And I will show you how to check your AMS NetID.You can see the TwinCAT Runtime icon in the Task bar.

Right Click the icon>Router>Change AMS NetId.

AMS NetID is shown and please fill in this value as the Tc3Target.

Program

Let’s open our C# Project.

we will use Vortex.Adapters.Connector.Tc3.Adapter.Tc3ConnectorAdapter.Create Method to build the connection between C# Project and the TwinCAT Runtime, and call the StartLoggingMessage Method inside MAIN._SimpleContent._logger to log the message.

using System;
using Serilog;

namespace plc1Connector
{
    class Program
    {
        static void Main(string[] args)
        {
            var plc = new plc1.plc1TwinController(Vortex.Adapters.Connector.Tc3.Adapter.Tc3ConnectorAdapter.Create(
                    Environment.GetEnvironmentVariable(“Tc3Target”), 851, true));

            plc.Connector.BuildAndStart();
            TcOpen.Inxton.TcoAppDomain.Current.Builder.SetUpLogger(new TcOpen.Inxton.Logging.SerilogAdapter());
         
            plc.MAIN._SimpleContent._logger.StartLoggingMessages(TcoCore.eMessageCategory.All, 10);
        }
    }
}

Play

Click the plc1Connector icon to start your application.

Result

Done!your C# Project is connected to TwinCAT Runtime.

Implmentation3

Now we can try to display the Logging Message in the console, and end with an Enter button.

Program

Just add these two lines at the end of your project.

Console.WriteLine(“Press enter to exit..”);

Console.ReadLine();

using System;
using Serilog;

namespace plc1Connector
{
    class Program
    {
        static void Main(string[] args)
        {
            var plc = new plc1.plc1TwinController(Vortex.Adapters.Connector.Tc3.Adapter.Tc3ConnectorAdapter.Create(
                    Environment.GetEnvironmentVariable(“Tc3Target”), 851, true));

            plc.Connector.BuildAndStart();
            TcOpen.Inxton.TcoAppDomain.Current.Builder.SetUpLogger(new TcOpen.Inxton.Logging.SerilogAdapter());
         
            plc.MAIN._SimpleContent._logger.StartLoggingMessages(TcoCore.eMessageCategory.All, 10);

            Console.WriteLine(“Press enter to exit..”);
            Console.ReadLine();
        }
    }
}

Result

Done! Logging Message is shown on the Console.

Implmentation4

Finally I will show you how to Show the logging message on the Notepad also.

Install packages

Open the Package Manager to install the Serilog.Sinks.Notepad package.

Program

Add these lines to write the logging messages into the Notepad.

TcOpen.Inxton.TcoAppDomain.Current.Builder.SetUpLogger(new TcOpen.Inxton.Logging.SerilogAdapter(new Serilog.LoggerConfiguration().WriteTo.Console().WriteTo.Notepad()));

using System;
using Serilog;

namespace plc1Connector
{
    class Program
    {
        static void Main(string[] args)
        {
            var plc = new plc1.plc1TwinController(Vortex.Adapters.Connector.Tc3.Adapter.Tc3ConnectorAdapter.Create(
                    Environment.GetEnvironmentVariable(“Tc3Target”), 851, true));

            plc.Connector.BuildAndStart();
            TcOpen.Inxton.TcoAppDomain.Current.Builder.SetUpLogger(new TcOpen.Inxton.Logging.SerilogAdapter(
                new Serilog.LoggerConfiguration().WriteTo.Console().WriteTo.Notepad()
                ));
         
            plc.MAIN._SimpleContent._logger.StartLoggingMessages(TcoCore.eMessageCategory.All, 10);

            Console.WriteLine(“Press enter to exit..”);
            Console.ReadLine();
        }
    }
}

Result

Done!

Footer_Basic

Please Support some devices for my blog

Amazon Gift List

Find ME

Twitter:@3threes2
Email:soup01threes*gmail.com (* to @)
YoutubeChannel:https://www.youtube.com/channel/UCQ3CHGAIXZAbeOC_9mjQiWQ

シェアする

  • このエントリーをはてなブックマークに追加

フォローする