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

cookie

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.

Check and change your cookie consent

Creating a PLCnext project

Before proceeding, make sure to have the workbench prepared properly.

Then go on:

  1. 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 FileNewProject….
    •  Type in the search plcnext and double-click the Firmware Library entry appearing in the list
      Hide screenshotClick to show a screenshot

      Visual Studio 2019 - Create a new project

    • Type in your Project Name, select a storage location and click Create.
      Hide screenshotClick to show a screenshot

      Visual Studio 2019 - Configure your new project

  2. Enable the System namespace

    See the System namespace topic for this step.

  3. 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 AddNew Item….
      Visual Studio 2019 - Add new item
    • Select in the left tree Visual C# ItemsPLCnext and then one of the following templates:
      Visual Studio 2019 - Add new item
      • Function
        Template for a C# function to be used in IEC 61131-3 code

        Hide value settingsShow return value settings

        Select a return value in the popped-up dialog:
        Visual Studio 2019 - Function Wizard
        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.

  4. Create the code
    • In the template code sheet, create the code for the function, function block, or function container 
      FB template dialog box
    • 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 that

      In Visual Studio, open your C# project and expand the EclrFirmwareLibrary branch in the Solution Explorer in the upper right corner of the screen.

      PLCnext_VS_SolutionExplorer_CHM_Readme.png

    • Create the entire project by pressing F6.
      ↪ A *.pcwlx library is being stored in either the Release or the Debug directory of the project.

 

Proceed with importing the library into PLCnext Engineer to use your C# function or function block in an automation project.

 

 


• Published/reviewed: 2024-12-19  ☃  Revision 076 •