Creating a C# function block library
With Visual Studio®, you can program functions, function blocks, function containers and programs in C# which you can subsequently import using the PLCnext Technology engineering software, PLCnext Engineer and use on a PLCnext Technology device.
Tutorial video
Note: Tutorial videos are embedded from the Phoenix Contact Technical Support YouTube channel. With playing an embedded YouTube video in this platform, you accept the YouTube Terms & Conditions.
Even showing the video previews on this page depends on your personal settings in the Cookie consent dialog. To watch YouTube videos, you have to allow cookies for marketing purposes which will remain on your device. The data gained through these cookies will be used by YouTube to provide video suggestions and advertisements based on your browsing habits, and may be made available to other parties.
04m:06s | HDTV 720p | English | English |
Creating a PLCnext project
Before proceeding, make sure to have the workbench prepared properly.
Then go on:
- Create a new project
- On the Visual Studio start-up screen select Create a new project,
or alternatively in the Visual Studio workbench, from the top menu bar, open File → New → Project…. - Type in the search plcnext and double-click the Firmware Library entry appearing in the list
Hide screenshotClick to show a screenshot - Type in your Project Name, select a storage location and click Create.
Hide screenshotClick to show a screenshot
- On the Visual Studio start-up screen select Create a new project,
- Enable the System namespace
See the System namespace topic for this step.
- Add new code sheets
With the extension, different code sheet templates are available. All templates are very basic but working examples. They can be compiled and tested right away.
- To add a new template right click in the Solution Explorer on your desired project.
- Select Add → New Item….
- Select in the left tree Visual C# Items → PLCnext and then one of the following templates:
- Function
Template for a C# function to be used in IEC 61131-3 codeHide value settingsShow return value settings
Select a return value in the popped-up dialog:
Return Type:
For information on the data types in C# programming for PLCnext Technology and their correlations to data types in IEC 61131 or C++ programming, you can pop up the table of supported data types in an overlay window.
Kind of Return Value:
By Reference is recommended for complex data types and is mandatory for strings, arrays and structures. Examples are available in our GitHub C# Example Repository. - Function Block
Template for a C# function block to be used in IEC 61131-3 code.
- Function Container
Template for a C# function container to define several IEC functions in on class.
- Program
Template for a C# program to be used like an IEC 61131-3 program.
↪ A default code sheet for the selected type of element is created.
- Function
- Create the code
- In the template code sheet, create the code for the function, function block, or function container
- Observe the comments in the template, and the Known Issues and Constraints section of the online help
Here you find the online help file:Click to show where to find thatIn Visual Studio, open your C# project and expand the EclrFirmwareLibrary branch in the Solution Explorer in the upper right corner of the screen.
- Create the entire project by pressing F6.
↪ A *.pcwlx library is being stored in either the Release or the Debug directory of the project.
- In the template code sheet, create the code for the function, function block, or function container
Proceed with importing the library into PLCnext Engineer to use your C# function or function block in an automation project.