Makers Blog

How to create a simple PLCnext console application in C#

Martin PLCnext Team 17 March 2021 min. read
752 views 1 comments

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 eCLR section of the PLCnext Info Center.

The Installation 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).

  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 Installation 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.

Note:

The Makers Blog shows applications and user stories of community members that are not tested or reviewed by Phoenix Contact. Use them at your own risk.

Discussion

Please login/register to comment

Login/Register

Leave a Reply

fcorti 24.06.2022

Dear ALL ,I'm trying to do the sample for GRPC but I've no familiarity with VS, If you could add some pictures for the next steps it could be great for beginner like me . I'm blocked to Step 5 the terminal gives me error : C:\Users\itcf01\source\repos\ConsoleApp1>dotnet build .ConsoleApp1.csproj dotnet publish -c RELEASE -r linux-arm .ConsoleApp1.csproj -o MyApp Microsoft (R) Build Engine versione 16.11.2+f32259642 per .NET Copyright (C) Microsoft Corporation. Tutti i diritti sono riservati. MSBUILD : error MSB1008: è possibile specificare un solo progetto. Opzione: dotnet Per la sintassi delle opzioni, digitare "MSBuild -help"

Login / Register to reply
Martin PLCnext Team 24.06.2022

Hi Fabrizio, I am afraid that the "Upgrade" of the PLCnext Community website messed up the formatting of many of the old Makers Blog posts. The commands that must be executed are: dotnet build ConsoleApp1.csproj dotnet publish -c RELEASE -r linux-arm ConsoleApp1.csproj -o MyApp Sorry for the confusion.

Martin PLCnext Team 24.06.2022

The commands that must be executed are:

Martin PLCnext Team 24.06.2022

dotnet build ConsoleApp1.csproj

Martin PLCnext Team 24.06.2022

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

Martin PLCnext Team 24.06.2022

Sorry for the confusion, and for the VERY frustrating formatting that this new Makers Blog area applies to posts and comments.

Newsletter
Never miss a new article
Sign up for the newsletter
Never miss news about PLCnext Technology
Get interesting content via newsletter four times a year
Receive exclusive information before all other users