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.
Depending on your personal settings in the Cookie consent dialog you may have allowed cookies for marketing purposes which will remain on your device. The data gained through this will be used by YouTube to provide video suggestions and advertisements based on your browsing habits, and may be made available to other parties. 

Creating a PLCnext project

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

Then go on:

  1. Create a new project

    With Visual Studio 2019:

    • 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

    With Visual Studio 2015/2017:

    • In Visual Studio, from the top menu bar, open File → New  → Project... .
    • In the dialog that opens, select Installed → Templates → Visual C# → PLCnext 
      Find it here:Click to show a screenshot

      PLCnext_VS_New_Project.PNG

    • Select the Firmware Library template for  from the list.
    • Select a name and storage location and click OK.

    A new, empty PLCnext library project is created.

  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….
      Hide screenshotClick to show a screenshot

      Visual Studio 2019 - Add new item

    • Select in the left tree Visual C# ItemsPLCnext and then one of the following templates:
      Hide screenshotClick to show a screenshot
      • 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 generic data types (ANY).

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

      Visual Studio 2019 - Add new item

    ⇒ 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 
      Note the "TODO" comment section in the code:Click to show a screenshot
      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

      In the eCLR-Programming-Reference.chm file, the eCLR base class libraries' description is at hand while programming.

      Known issues and constraints of the current version are linked on the start page of this online help file. 

      In addition, the Readme.txt file contains a "cheat sheet" for common procedures in programming, along with a short overview how the IEC 61131-3 data types are matching to .NET and C# data types.

    • Create the entire project by pressing F6.

    ⇒ A *.pcwlx library is being stored in either the Release or the Debug directory of the project.

 

 

 


•  Web browser recommendation: Chrome/Edge 88 or newer, Firefox ESR 90 or neweror Safari • 
• Published/reviewed: 2022-06-09 • Revision 041 •