Defining Custom Arrays in Native Function Blocks – A Practical Guide
Why This Post?
In fact, this post was created because when I was developing the Native Function Block, I found that it was impossible to use an array of custom structures. Or to put it another way, it's a pity that the official GitHub of PLCnext doesn't have tutorials of this type. If you use the C# tutorial to create a custom array in Native Function Block, you will find that you can't compile it successfully, or setting the array values in PLCnext Engineer has no response. The example in the case just happens to be related to the database, that's all.
Demo description
- The following example uses the Native Function Block to create a user-defined Array interface with PLCncli toolchain
- The Array consists of 21 elements, each of which is a structure. The structure contains two IecString80, and an enumeration, as shown in the following diagram.
https://github.com/Leeable/UserDefineArray_NativeFB/tree/Master
Requirements
- Make sure that the ToolChain version is 23.0 or higher. This example uses the 23.6 ToolChain for demonstration purposes.
- Familiar with the basic process of establishing a Native Function Block.
- Visual Studio 2019 or 2022 with ToolChain add-in plugins
$ plcncli --version
plcncli 23.6.0.1450 (23.6.0.1450)
STEP1
- Create a new project using Visual Studio, select "Shared Native Function Block" project type, and name the project "ArrayTest."
- Then, within the ArrayTest project in Visual Studio, create a new Native Function Block named "ArrayCplus."
STEP2
-
Copy those code to ArrayCplus.cs
-
Because there are some display issues when directly including the source code here, such as & and >, I provided a link instead.
-
for example, the & will be encode as
&in this html.....
ArrayCplus.cs
STEP3
Open the terminal in the ArrayTestCpp folder and enter the following.
$ plcncli generate code
After generating the code, locate the ArrayTest-cli32.h and ArrayTest-cli64.h files in the src directory, and find the places where #error is indicated.
We need to remove the comments and improve the code. Click to navigate to InArrayFB-cli.cpp and modify the implementation.
IntArrayFB-cli.cpp
STEP4
In ArrayCplus.cpp, enter the following code. Contents containing strings must be initialized in the process rather than in the Init region; initializing them in the Init region will not work, resulting in the inability to write values to sName and sAttribute through Engineer.
ArrayCplus-cli.cpp
STEP5
Open the terminal in the ArrayTestCpp folder and enter the following.
$ plcncli build all
$ plcncli deploy
STEP6
Open the PLCnext Engineer and load the ArrayTest.pcwlx add an variable to the ArrayTest named arr
Now it can work
Questions
- Although this method works in practice, why is it necessary to initialize content containing IecString or User-defined String in the void PInvoke ArrayTest::ArrayCplus::Process() instead of in the void PInvoke ArrayTest::ArrayCplus::Init()? it doesn't work if i put the code in the void PInvoke ArrayTest::ArrayCplus::Init().
- It's clear that there is no corresponding tutorial on userdefine arrays for Native Function Blocks on GitHub. Can somebody supplement and improve it? (the way that Csharp Userdefine array is not work in the native function block)
- One last point that also confuses me is why, regardless of whether it's IecString, User-defined String, or IecStringEx*, if they contain strings, they must be in the form of 【InOut】in CSharp code when used as a Native Function Block?
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.
Industrial Technology
- How 5G Is Revolutionizing Smart Factories
- Exploring 6G: The Future of Ultra-Fast Connectivity
- Driving Smarter Food Safety: A Strategic Partnership
- Top 7 Smart Warehouse Technologies You Should Adopt Now
- Navigating the Food & Beverage Industry: Challenges & Opportunities for 2024
- PCB Slots: Reliable, Efficient Component Fixing for Precise Electronics Assembly
- U.S. Manufacturing Reshoring: Current Trends and Impact
- Revolutionary Tech Eliminates Counterfeit Products from Supply Chains Worldwide
- Integrating ISO 9001 into Manufacturing Strategy: Driving Quality and Growth
- Bevel Protractor Explained: How It Measures Angles and Why It Matters