--- a/etherlab/etherlab.py Fri Oct 19 16:37:54 2012 +0200
+++ b/etherlab/etherlab.py Thu Oct 25 19:55:02 2012 +0200
@@ -5,6 +5,7 @@
+from POULibrary import POULibrary from ConfigTreeNode import ConfigTreeNode
from PLCControler import UndoBuffer, LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY
from ConfigEditor import NodeEditor, CIA402NodeEditor, ETHERCAT_VENDOR, ETHERCAT_GROUP, ETHERCAT_DEVICE
@@ -56,6 +57,31 @@
#--------------------------------------------------
+# Etherlab Specific Blocks Library +#-------------------------------------------------- +def GetLocalPath(filename): + return os.path.join(os.path.split(__file__)[0], filename) +class EtherlabLibrary(POULibrary): + def GetLibraryPath(self): + return GetLocalPath("pous.xml") + def Generate_C(self, buildpath, varlist, IECCFLAGS): + etherlab_ext_file = open(GetLocalPath("etherlab_ext.c"), 'r') + etherlab_ext_code = etherlab_ext_file.read() + etherlab_ext_file.close() + Gen_etherlabfile_path = os.path.join(buildpath, "etherlab_ext.c") + ethelabfile = open(Gen_etherlabfile_path,'w') + ethelabfile.write(etherlab_ext_code) + runtimefile_path = os.path.join(os.path.split(__file__)[0], "runtime_etherlab.py") + return ((["etherlab_ext"], [(Gen_etherlabfile_path, IECCFLAGS)], True), "", + ("runtime_etherlab.py", file(GetLocalPath("runtime_etherlab.py")))) +#-------------------------------------------------- #--------------------------------------------------
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/etherlab/etherlab_ext.c Thu Oct 25 19:55:02 2012 +0200
@@ -0,0 +1,42 @@
+#include "iec_types_all.h" +extern long AtomicCompareExchange(long* atomicvar,long compared, long exchange); + return AtomicCompareExchange(&SDOLock, FREE, ACQUIRED) == FREE; + AtomicCompareExchange(&SDOLock, ACQUIRED, ANSWERED); + return SDOLock == ANSWERED; + AtomicCompareExchange(&SDOLock, ANSWERED, FREE); +int __init_etherlab_ext() +void __cleanup_etherlab_ext() +void __retrieve_etherlab_ext() +void __publish_etherlab_ext() --- a/etherlab/extension.py Fri Oct 19 16:37:54 2012 +0200
+++ b/etherlab/extension.py Thu Oct 25 19:55:02 2012 +0200
@@ -1,5 +1,12 @@
+def GetEtherLabLibClass(): + from etherlab import EtherlabLibrary +features.libraries.append( + ('Etherlab', GetEtherLabLibClass)) from etherlab import RootClass
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/etherlab/runtime_etherlab.py Thu Oct 25 19:55:02 2012 +0200
@@ -0,0 +1,50 @@
+import subprocess,sys,ctypes +from threading import Thread +SDOAnswered = PLCBinary.SDOAnswered +SDOAnswered.restype = None +SDOAnswered.argtypes = [] +def SDOThreadProc(*params): + if params[0] == "upload": + command = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x" + command = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s" + proc = subprocess.Popen(command % params[1:], stdout=subprocess.PIPE, shell=True) + output = proc.communicate()[0] + if params[0] == "upload": + if params[2] in ["float", "double"]: + elif params[2] in ["string", "octet_string", "unicode_string"]: + hex_value, dec_value = output.split() + if int(hex_value, 16) == int(dec_value): + Result = int(dec_value) +def EthercatSDOUpload(pos, index, subindex, var_type): + SDOThread = Thread(target=SDOThreadProc, args=["upload", pos, var_type, index, subindex]) +def EthercatSDODownload(pos, index, subindex, var_type, value): + SDOThread = Thread(target=SDOThreadProc, args=["download", pos, var_type, index, subindex, value])