Store PLCnext CommunityPLCnext on LinkedInPLCnext on Instagram  PLCnext on YouTube Github PLCnext CommunityStore PLCnext Community

 

 How to create a Blog Entry

How to create a simple PLCnext console application in C#

This tutorial shows how to create a simple "Hello World" console application for a PLCnext Control device. The application is written in C# and targets .NET Core.

Note that this tutorial is intended for C# applications that run entirely outside the PLCnext Runtime. For C# applications that target the eCLR, please refer to the relevant section of the PLCnext Info Center.

A previous tutorial showed how to install .NET Core on a PLC. In that case, multiple applications targeting .NET Core can be run on the PLC, with minimum duplication of common .NET dlls.

As an alternative, it is possible to run a .NET Core application on a PLC that does not have the .NET Core framework installed. In this case, all the .NET dependencies required for the application are deployed with the application. This may be suitable for projects that require only one .NET Core application on the PLC.

This tutorial addresses the second type of application.

This tutorial uses:

  • AXC F 2152 with firmware 2021.0.3. .NET Core does not need to be installed.
  • Visual Studio 2019. No PLCnext-specific add-ins or SDKs are required in this case.

Procedure

  1. In Visual Studio, create a new project using the template C# Console App (.NET Core).

    New Project

  2. Add a file to the project root directory called runtimeconfig.template.json, containing the following:

    {
       "configProperties": {
           "System.Globalization.Invariant": true
       }
    }

    The reason for this property setting is described in the "Background Information" section of the previous tutorial.

  3. Add the following line to the PropertyGroup section of the project File (.csproj):

    <RuntimeIdentifiers>linux-arm</RuntimeIdentifiers>

    This tells the compiler to target a Linux 32 bit platform.

    The .csproj file should then look something like this:

    <Project sdk="Microsoft.NET.Sdk">
    
     <PropertyGroup>
       <OutputType>Exe</OutputType>
       <TargetFramework>netcoreapp3.1</TargetFramework>
       <RuntimeIdentifiers>linux-arm</RuntimeIdentifiers>
     </PropertyGroup>
    
    </Project>
  4. In Solution Explorer, right-click on the Solution and open a Terminal.

  5. In the terminal, execute the following commands:

    dotnet build .\ConsoleApp1.csproj
    dotnet publish -c RELEASE -r linux-arm .\ConsoleApp1.csproj -o MyApp

    ... where ConsoleApp1 is the name of the solution, and MyApp is the name of the output directory, where the application will be published. The application can also be published in DEBUG mode if required.

  6. Copy the output directory and all its contents to the PLC using (for example) WinSCP.

  7. Open a shell session on the PLC using (for example) PuTTY or ssh.

  8. Check the format of the executable:

    $ file /opt/plcnext/MyApp/ConsoleApp1
    ConsoleApp1: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, BuildID[sha1]=887a06cd9735de5da4b686517f69d68779571cec, stripped

    This confirms that the application has been built for the correct platform.

  9. Make sure that the executable has execute privileges:

    $ chmod a+x /opt/plcnext/MyApp/ConsoleApp1
  10. Run the application:

    $ /opt/plcnext/MyApp/ConsoleApp1
    Hello World!

Questions/comments?

If you have any questions or comments, please ask them in the PLCnext Community Forum.