Hz
How to create an empty function block in OpenPLC Editor, and write the C code that has been written in Visual Studio into this empty function block, and implement the functions I need, such as: PID algorithm and MPC algorithm?
Reply 0 0
thiagoralves
The best strategy would be to convert your algorithm to structured text, as that’s a native IEC 61131-3 language. You can write C code under pragmas - between { and } characters - but there are a few limitations to this, such as not being able to declare functions or include header files.
Reply 1 0
Hz
Thank you for your answer.I'm a newbie.
Can you tell me where Pragmas is?
If I want to write some complex algorithms into function blocks, I can do it this way
Looking forward to your reply, thank you.
Reply 0 0
Hz
I used the Openplc Import Tool to try to customize the new function block with C according to the documentation

Adding new blocks to OpenPLC Editor’s library

, but it doesn't work anymore, can you tell me where I got it wrong? Thank you again微信图片_20241209230616.png微信图片_20241209230625.png微信图片_20241209230632.png微信图片_20241209230638.png微信图片_20241209230645.png
Reply 0 0
thiagoralves
You don't have to use the import tool unless you want to permanently add your block to the OpenPLC Library. This is quite complex. I suggest you start with just a block you can create on the PLC program itself (gray add button on the left panel). You can create the block in any of the IEC languages. If you want to add C code, you must add it in between { } on a Structured Text block.
 
You can check the Random_Generator_Pragma example from the file menu
Reply 0 0
Hz
Thanks for the reply, I was wondering if I could follow the Random_Generator_Pragma example to add my own c code. Or can you be able to teach me how to write C code under pragma, thanks.
Reply 0 0
thiagoralves
  1. Click on the gray plus icon on the left panel
  2. Select Function block, language ST
  3. Type {
  4. Write your C code
  5. Type }
Reply 0 0
Hz
thank you
Reply 0 0
fg71
You could write C code in Arduino Extension to be used in custom Function Block and you could include headers and you could
declare functions. I have done this with ArduinoRTC (Real Time Clock).
Reply 0 0
Reply