sy9452
Hi everyone,
I am currently working on a custom Python Function Block in OpenPLC. I have defined my input and output variables in the interface table (as seen in the attached screenshot), but I am having trouble understanding the standard way to interact with these variables within the script.

[203128%2F9667550%2Fmceclip0]
 
Specifically:
  1. Get/Read: What is the recommended way to read the current value of an input pin (e.g., IN1, IN2)?
  2. Set/Write: How should I assign a value to an output pin (e.g., OUTPUT) so that it is correctly passed back to the PLC runtime?
In my current code, I am simply using:
 
def block_loop():    OUTPUT = IN1 + IN2
However, I want to ensure I'm not missing any specific API calls or "get/set" methods required by the OpenPLC Python bridge to ensure thread safety or proper data mapping.
Any guidance or examples of the "best practice" for accessing these variables would be greatly appreciated!
Thanks in advance.
Reply 0 0
thiagoralves
There are no API calls. We did everything under the hood so your code can be as simple as possible! That's exactly all you need. OUTPUT = IN1 + IN2 is sufficient to both read IN1 and IN2 and set OUTPUT.
 
The only good practice for Python (and this is related to Python, not OpenPLC in general) is to declare the variables you want to write as global. That's because Python usually creates variables on the fly during execution. So if you simply do OUTPUT = IN1 + IN2, it may create a new local variable called OUTPUT instead of using the global scope OUTPUT variable. Just adding a global OUTPUT statement at the beginning of every function is all you need to make sure Python will use the global scope OUTPUT instead of creating a new local scope OUTPUT for that statement.
Reply 0 0
sy9452
Hi again,
Thank you for the clarification! I have updated my code to include the global declaration as suggested, but unfortunately, the output variable still doesn't seem to update in the PLC runtime.
As shown in my attached screenshots:

[203128%2F9667619%2Fmceclip0]

[203128%2F9667620%2Fmceclip0]
Code: I am using global output1, input1, input2 inside block_loop() and performing output1 = input1 + input2.
Debugger: My inputs input1 and input2 are receiving values (1 and 2), but output1 remains 0.
Comparison: For testing, I placed a standard ADD block right below my Python block using the same inputs, and it works perfectly (outputting 3).
Is there any other configuration I might be missing?
Thank you for your help!
Reply 0 0
thiagoralves
Check your PLC Logs to make sure your python block is running. Since python blocks are asynchronous, something broken on the Python code won't prevent the PLC code to run. It will just stop updating variables on the Python side.
Reply 0 0
sy9452
Hi,
I checked the logs and found the following error when starting my Python function block:
[05-02-26 23:11:36] [INFO]: [Python loader] Random shared memory location: /shmPeD5WxIEWSoz
[05-02-26 23:11:36] [INFO]: [Python loader] Started Python function block: ADD1.py (PID 1423)
[05-02-26 23:11:36] [INFO]: [Python] Error on shared memory: [Errno 13] Permission denied: '//shmPeD5WxIEWSoz_in'
It seems like the Python process does not have the necessary permissions to access the shared memory location created by the OpenPLC loader.
I am running both the OpenPLC Runtime and Editor on a Windows 11 machine inside a VMware Workstation virtual machine. I have already tried:
Running both applications with Administrator privileges.but the error persists.
Is it possible that running OpenPLC inside a virtual machine is causing this shared memory conflict?
Any advice would be greatly appreciated. Thanks!
Reply 0 0
thiagoralves
Ah, that's good information. What version of the runtime are you running? Did you install the latest one from Autonomylogic.com? If not, please install the latest version and let me know if the issue continues to happen.
Reply 0 0
sy9452
Hi,
Thank you for the quick response. Yes, I am using the latest versions downloaded from Autonomylogic.com.
Specifically, I have installed:
OpenPLC Editor: v4.1.2 (OpenPLC.Editor_4.1.2.exe)
OpenPLC Runtime: v4.0.6 (OpenPLC_Runtime_4.0.6_Setup.exe)
Reply 0 0
thiagoralves
Perfect. Let us investigate that further. It seems to be a specific problem on Windows. It works fine on the runtime running on Linux if you want to try that.
Reply 0 0
sy9452
Hi,
Thanks for the suggestion! I switched to Linux, and I can confirm that the single Python Function Block now works perfectly. However, I have encountered two new issues when trying to reuse the same Python block multiple times in my main program.
 

Issue 1: Case-insensitive naming conflict between Type and Instance (Image 1) When I used the instance name ADD1 for my Python block type add1, the compilation failed with several ST (Structured Text) errors:

  • [06-02-26 11:01:39] [INFO]: ST file generated at: D:\AICC\build\OpenPLC Runtime v4\src\ program.st
    [06-02-26 11:01:39] [INFO]: Static files copied successfully.
    [06-02-26 11:01:39] [ERROR]: iec2c process exited with code 1
    [06-02-26 11:01:39] [ERROR]: D:\AICC\build\OpenPLC Runtime v4\src\program.st:4-5..4-15: error: invalid variable(s) declaration.
    [06-02-26 11:01:39] [ERROR]: 4 | ADD1 : add1;
    [06-02-26 11:01:39] [ERROR]: | ^~~~~~~~~~~
    [06-02-26 11:01:39] [ERROR]: D:\AICC\build\OpenPLC Runtime v4\src\program.st:28-3..28-13: error: invalid variable before ':=' in ST assignment statement.
    [06-02-26 11:01:39] [ERROR]: 28 | ADD1(input1 := var1, input2 := var2);
    [06-02-26 11:01:39] [ERROR]: | ^~~~~~~~~~~
    [06-02-26 11:01:39] [ERROR]: D:\AICC\build\OpenPLC Runtime v4\src\program.st:28-21..28-22: error: ';' missing at the end of statement in ST statement.
    [06-02-26 11:01:39] [ERROR]: 28 | ADD1(input1 := var1, input2 := var2);
    [06-02-26 11:01:39] [ERROR]: | ^~
    [06-02-26 11:01:39] [ERROR]: D:\AICC\build\OpenPLC Runtime v4\src\program.st:28-22..28-29: error: invalid variable before ':=' in ST assignment statement.
    [06-02-26 11:01:39] [ERROR]: 28 | ADD1(input1 := var1, input2 := var2);
    [06-02-26 11:01:39] [ERROR]: | ^~~~~~~~
    [06-02-26 11:01:39] [ERROR]: D:\AICC\build\OpenPLC Runtime v4\src\program.st:28-37..28-38: error: ';' missing at the end of statement in ST statement.
    [06-02-26 11:01:39] [ERROR]: 28 | ADD1(input1 := var1, input2 := var2);
    [06-02-26 11:01:39] [ERROR]: | ^~
    [06-02-26 11:01:39] [ERROR]: D:\AICC\build\OpenPLC Runtime v4\src\program.st:28-38..28-38: error: invalid statement in ST statement.
    [06-02-26 11:01:39] [ERROR]: 28 | ADD1(input1 := var1, input2 := var2);
    [06-02-26 11:01:39] [ERROR]: | ^
    [06-02-26 11:01:39] [ERROR]: D:\AICC\build\OpenPLC Runtime v4\src\program.st:29-11..29-14: error: invalid expression after ':=' in ST assignment statement.
    [06-02-26 11:01:39] [ERROR]: 29 | var5 := ADD1.output1;
    [06-02-26 11:01:39] [ERROR]: | ^~~~
    [06-02-26 11:01:39] [ERROR]: D:\AICC\build\OpenPLC Runtime v4\src\program.st:29-14..29-15: error: ';' missing at the end of statement in ST statement.
    [06-02-26 11:01:39] [ERROR]: 29 | var5 := ADD1.output1;
    [06-02-26 11:01:39] [ERROR]: | ^~
    [06-02-26 11:01:39] [ERROR]: D:\AICC\build\OpenPLC Runtime v4\src\program.st:29-15..29-22: error: invalid statement in ST statement.
    [06-02-26 11:01:39] [ERROR]: 29 | var5 := ADD1.output1;
    [06-02-26 11:01:39] [ERROR]: | ^~~~~~~~
    [06-02-26 11:01:39] [ERROR]:
    [06-02-26 11:01:39] [ERROR]: 9 error(s) found. Bailing out!
    [06-02-26 11:01:39] [ERROR]: Stopping debug compilation process.
  • error: invalid variable before ':=' in ST assignment statement.
It seems the compiler does not distinguish between the type name and the instance name (case-insensitivity). When I renamed the instance from ADD1 to  ADD2, the project compiled and downloaded successfully. Is this naming restriction by design for Python blocks?
[203128%2F9668562%2Fmceclip0]

Issue 2: Multiple instances share/overwrite the same output (Image 2) After fixing the naming issue, I tried to reuse the block twice (instances ADD0 and ADD2) with the same inputs (var1=1, var2=2).

  • Expected: Both var3 and var4 should be 3.
  • Actual: ADD0.output1 (var3) is 0, while ADD2.output1 (var4) is 3.
    [203128%2F9668563%2Fmceclip0]
It looks like the outputs of multiple instances are overwriting each other, or perhaps they are all mapping to the same global variable in the background. Does this mean Python blocks in OpenPLC are currently limited to a single instance per project, or is this a bug in how shared memory handles multiple instances?
I have attached screenshots of my logic and the debugger for reference. Thank you for your help!
Reply 0 0
thiagoralves
  1. IEC 61131-3 languages are case insensitive. The compiler makes no distinction between ADD1, Add1, aDd1, etc. Although Python is case sensitive, the runtime is not to follow IEC.
  2. You can only have one instance of a Python block per project. You can create multiple Python blocks (even with the same code if you want), but each block can have only one instance.
Reply 0 0
sy9452
Thank you for the clarification! This helps me understand the design logic of Python blocks in OpenPLC. I will adjust my project accordingly.
Reply 1 0
MO
Is it possible in OpenPLC Editor to output an array from a Python function block as an output variable so that the function block array can be used in an ST program?
Reply 0 0
thiagoralves
No. Currently only standard base types are supported
Reply 0 0
Reply