Makers Blog

How to create a simple console application in C#

Martin [PLCnext Technology Team] 17 March 2021 min. read
2,243 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 (formerly known as ".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 on a PLC. In that case, multiple applications targeting .NET can be run on the PLC, with minimum duplication of common .NET dlls.

As an alternative, it is possible to run a .NET application on a PLC that does not have the .NET 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 application on the PLC. Microsoft refer to this type of application as a Self-Contained Deployment.

This tutorial creates a simple self-contained console application for an AXC F 2152 PLCnext Control device.

This tutorial uses:

  • AXC F 2152 with firmware 2025.6. .NET does not need to be installed on the device.
  • Visual Studio 2022 with the .NET Desktop Development workload. No PLCnext Technology add-ins or SDKs are required in this case.

Procedure

  1. In Visual Studio 2022, create a new project using the template C# Console App. This type of project can run on .NET on Windows, Linux and macOS.

    Choose the latest Long Term Support version of the .NET Framework.

  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. In Solution Explorer, right-click on the C# Project and select "Publish..."

  4. In the Publish dialog, select the following:

    Target: Folder

    Specific Target: Folder

    Location: (the default location is fine)

    ... then press "Finish" and "Close". A publish profile has now been created.

  5. Edit the publish profile by selecting one of the pencil icons next to one of the settings. Change the following settings:

    Deployment mode: Self-contained

    Target runtime: linux-arm

    File publish options: Produce single file

    Save the new settings.

  6. At the top of the window, press the "Publish" button.

  7. When publishing is complete, select the "Navigate" link.

  8. Copy the published file to the PLC using (for example) WinSCP.

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

  10. Check the format of the executable:

    file /opt/plcnext/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.

  11. Make sure that the executable has execute privileges:

    chmod a+x /opt/plcnext/ConsoleApp1
    
  12. Run the application:

    /opt/plcnext/ConsoleApp1
    
    	Hello World!
    

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