--- a/py_ext/PythonFileCTNMixin.py Tue Sep 15 13:57:06 2020 +0200
+++ b/py_ext/PythonFileCTNMixin.py Tue Oct 20 00:23:52 2020 +0200
@@ -95,15 +95,24 @@
getattr(self.CodeFile, section).getanyText() + "\n" + \
self.PostSectionsTexts.get(section, "")
+ def CTNGlobalInstances(self): + variables = self.CodeFileVariables(self.CodeFile) + ret = [(variable.getname(), + for variable in variables] + location_str = "_".join(map(str, self.GetCurrentLocation())) + ret.append(("On_"+location_str+"_Change", "python_poll", "")) def CTNGenerate_C(self, buildpath, locations):
# location string for that CTN
location_str = "_".join(map(str, self.GetCurrentLocation()))
configname = self.GetCTRoot().GetProjectConfigNames()[0]
- return '"' + var.getonchange() + \
- "('" + var.getname() + "')\"" \
- if var.getonchange() else '""'
+ return [onchangecall.strip() + "('" + var.getname() + "')" + for onchangecall in var.getonchange().split(',')] return repr(var.getonchange()) \
@@ -124,6 +133,9 @@
self.CodeFile.variables.variable)
+ onchange_var_count = len([None for varinfo in varinfos if varinfo["onchange"]]) # python side PLC global variables access stub
globalstubs = "\n".join([
@@ -144,6 +156,16 @@
""" % varinfo for varinfo in varinfos])
+ on_change_func_body = "\n".join([""" + """.join(varinfo['onchangecode'])+""" + errors.append("%(name)s: "+str(e)) +""" % varinfo for varinfo in varinfos if varinfo["onchange"]]) # Runtime calls (start, stop, init, and cleanup)
for section in self.SECTIONS_NAMES:
@@ -163,6 +185,9 @@
"globalstubs": globalstubs,
"globalsection": globalsection,
+ "location_str": location_str, + "on_change_func_body":on_change_func_body, + "onchange_var_count": onchange_var_count @@ -174,6 +199,11 @@
## Code for PLC global variable access
from runtime.typemapping import TypeTranslator
+_PySafeGetChanges_%(pyextname)s = PLCBinary.PySafeGetChanges_%(location_str)s +_PySafeGetChanges_%(pyextname)s.restype = ctypes.POINTER(ctypes.c_int * %(onchange_var_count)d) +_PySafeGetChanges_%(pyextname)s.argtypes = None _%(pyextname)sGlobalsDesc = []
__ext_name__ = "%(pyextname)s"
PLCGlobalsDesc.append(( "%(pyextname)s" , _%(pyextname)sGlobalsDesc ))
@@ -185,6 +215,16 @@
## Beremiz python runtime calls
+def On_%(pyextname)s_Change(): + changesP = _PySafeGetChanges_%(pyextname)s() + raise Exception("PySafeGetChanges returned NULL!") + changes = iter(changesP.contents) + raise Exception("Exception in %(pyextname)s OnChange call:\\\\n" + "\\\\n".join(errors)) @@ -220,7 +260,7 @@
-PYTHON_POLL* __%(name)s_notifier;
+int __%(name)s_rbuffer_written = 0; @@ -244,15 +284,21 @@
IEC_%(IECtype)s tmp = __GET_VAR(%(configname)s__%(uppername)s);
if(NE_%(IECtype)s(1, NULL, __%(name)s_rbuffer, tmp)){
__%(name)s_rbuffer = tmp;
- PYTHON_POLL_body__(__%(name)s_notifier);
+ /* mark variable as changed */ + __%(name)s_rbuffer_written = 1; AtomicCompareExchange((long*)&__%(name)s_rlock, 1, 0);
- varinitonchangefmt = """\
- __%(name)s_notifier = __GET_GLOBAL_ON%(uppername)sCHANGE();
- __SET_VAR(__%(name)s_notifier->,TRIG,,__BOOL_LITERAL(TRUE));
- __SET_VAR(__%(name)s_notifier->,CODE,,__STRING_LITERAL(%(onchangelen)d,%(onchangecode)s));
+ varcollectchangefmt = """\ + while(AtomicCompareExchange(&__%(name)s_wlock, 0, 1)); + pysafe_changes[change_index++] = __%(name)s_rbuffer_written; + /* mark variable as unchanged */ + __%(name)s_rbuffer_written = 0; + AtomicCompareExchange((long*)&__%(name)s_wlock, 1, 0); vardec = "\n".join([(vardecfmt + vardeconchangefmt
if varinfo["onchange"] else vardecfmt) % varinfo
@@ -261,16 +307,20 @@
varpub = "\n".join([(varpubonchangefmt if varinfo["onchange"] else
for varinfo in varinfos])
- varinit = "\n".join([varinitonchangefmt %
- dict(onchangelen=len(varinfo["onchangecode"]), **varinfo)
+ varcollectchange = "\n".join([varcollectchangefmt % varinfo for varinfo in varinfos if varinfo["onchange"]])
+ pysafe_pypoll_code = "On_"+pyextname+"_Change()"
"location_str": location_str,
+ "pysafe_pypoll_code": '"'+pysafe_pypoll_code+'"', + "pysafe_pypoll_code_len": len(pysafe_pypoll_code), + "varcollectchange": varcollectchange, + "onchange_var_count": onchange_var_count # TODO : use config name obtained from model instead of default
@@ -286,12 +336,17 @@
+PYTHON_POLL* __%(location_str)s_notifier; /* User variables reference */
/* Beremiz confnode functions */
int __init_%(location_str)s(int argc,char **argv){
+ __%(location_str)s_notifier = __GET_GLOBAL_ON_%(location_str)s_CHANGE(); + __SET_VAR(__%(location_str)s_notifier->,TRIG,,__BOOL_LITERAL(TRUE)); + __SET_VAR(__%(location_str)s_notifier->,CODE,,__STRING_LITERAL(%(pysafe_pypoll_code_len)d,%(pysafe_pypoll_code)s)); @@ -303,8 +358,21 @@
void __publish_%(location_str)s(void){
+ // call python part if there was at least a change + PYTHON_POLL_body__(__%(location_str)s_notifier); +static int pysafe_changes[%(onchange_var_count)d]; +void* PySafeGetChanges_%(location_str)s(void){ + return (void*)&pysafe_changes[0]; Gen_PyCfile_path = os.path.join(buildpath, "PyCFile_%s.c" % location_str)
--- a/tests/python/plc.xml Tue Sep 15 13:57:06 2020 +0200
+++ b/tests/python/plc.xml Tue Oct 20 00:23:52 2020 +0200
@@ -1,7 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<project xmlns="http://www.plcopen.org/xml/tc6_0201" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xsi:schemaLocation="http://www.plcopen.org/xml/tc6_0201">
<fileHeader companyName="" productName="Beremiz" productVersion="0.0" creationDateTime="2008-12-14T16:21:19" contentDescription="This example shows many features in Beremiz: 1. How to implement python extensions. 2. How to implement basic C extension. 3. How to use C code in IEC POUs. 4. How to call C functions from python code. 5. How to avoid race conditions between IEC, C and python code. 6. How to convert betweet different IEC types. "/>
- <contentHeader name="Beremiz Python Support Tests" modificationDateTime="2020-06-17T13:19:14">
+ <contentHeader name="Beremiz Python Support Tests" modificationDateTime="2020-10-19T23:53:08"> <pageSize x="1024" y="1024"/>
--- a/tests/python/py_ext_0@py_ext/pyfile.xml Tue Sep 15 13:57:06 2020 +0200
+++ b/tests/python/py_ext_0@py_ext/pyfile.xml Tue Oct 20 00:23:52 2020 +0200
@@ -2,7 +2,7 @@
<PyFile xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<variable name="SomeVarName" type="DINT" onchange="MyFunc"/>
- <variable name="Grumpf" type="STRING" initial="'mhoo'" onchange="MyFunc"/>
+ <variable name="Grumpf" type="STRING" initial="'mhoo'" onchange="MyFunc, MyOtherFunc"/> @@ -12,6 +12,10 @@