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
-
In Visual Studio, create a new project using the template C# Console app (.NET Core).
-
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.
-
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>
-
In Solution Explorer, right-click on the Solution and open a Terminal.
-
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, andMyApp
is the name of the output directory, where the application will be published. The application can also be published inDEBUG
mode if required. -
Copy the output directory and all its contents to the PLC using (for example) WinSCP.
-
Open a shell session on the PLC using (for example) PuTTY or ssh.
-
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.
-
Make sure that the executable has execute privileges:
chmod a+x /opt/plcnext/MyApp/ConsoleApp1
-
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.
Leave a Reply
You must be logged in to post a comment.