ProjectController.py
branchwxPython4
changeset 3405 fdc12f7d27c8
parent 3396 8c8cb5c9ff38
child 3421 70ef15fca028
--- a/ProjectController.py	Tue Nov 30 18:43:10 2021 +0100
+++ b/ProjectController.py	Sun Jan 16 17:00:58 2022 +0100
@@ -276,6 +276,7 @@
         # copy StatusMethods so that it can be later customized
         self.StatusMethods = [dic.copy() for dic in self.StatusMethods]
         self.DebugToken = None
+        self.LastComplainDebugToken = None
         self.debug_status = PlcStatus.Stopped
 
         self.IECcodeDigest = None
@@ -797,7 +798,12 @@
 
         IECCodeContent += open(self._getIECgeneratedcodepath(), "r").read()
 
-        with open(self._getIECcodepath(), "w") as plc_file:
+        IECcodepath = self._getIECcodepath()
+
+        if not os.path.exists(IECcodepath):
+            self.LastBuiltIECcodeDigest = None
+
+        with open(IECcodepath, "w") as plc_file:
             plc_file.write(IECCodeContent)
 
         hasher = hashlib.md5()
@@ -966,7 +972,7 @@
                 # describes CSV columns
                 ProgramsListAttributeName = ["num", "C_path", "type"]
                 VariablesListAttributeName = [
-                    "num", "vartype", "IEC_path", "C_path", "type", "derived"]
+                    "num", "vartype", "IEC_path", "C_path", "type", "derived", "retain"]
                 self._ProgramList = []
                 self._VariablesList = []
                 self._DbgVariablesList = []
@@ -1047,10 +1053,9 @@
 
         # prepare debug code
         variable_decl_array = []
-        bofs = 0
-        for v in self._DbgVariablesList:
-            sz = DebugTypesSize.get(v["type"], 0)
-            variable_decl_array += [
+        retain_indexes = []
+        for i, v in enumerate(self._DbgVariablesList):
+            variable_decl_array.append(
                 "{&(%(C_path)s), " % v +
                 {
                     "EXT": "%(type)s_P_ENUM",
@@ -1059,10 +1064,12 @@
                     "OUT": "%(type)s_O_ENUM",
                     "VAR": "%(type)s_ENUM"
                 }[v["vartype"]] % v +
-                "}"]
-            bofs += sz
+                "}")
+
+            if v["retain"] == "1":
+                retain_indexes.append("/* "+v["C_path"]+" */ "+str(i))
+
         debug_code = targets.GetCode("plc_debug.c") % {
-            "buffer_size": bofs,
             "programs_declarations": "\n".join(["extern %(type)s %(C_path)s;" %
                                                 p for p in self._ProgramList]),
             "extern_variables_declarations": "\n".join([
@@ -1076,6 +1083,7 @@
                 }[v["vartype"]] % v
                 for v in self._VariablesList if v["C_path"].find('.') < 0]),
             "variable_decl_array": ",\n".join(variable_decl_array),
+            "retain_vardsc_index_array": ",\n".join(retain_indexes),
             "var_access_code": targets.GetCode("var_access.c")
         }
 
@@ -1550,6 +1558,14 @@
                                     else:
                                         values_buffer.append((value, forced))
                             self.DebugTicks.append(debug_tick)
+                        else:
+                            # complain if trace is incomplete, but only once per debug session
+                            if self.LastComplainDebugToken != self.DebugToken :
+                                self.logger.write_warning(
+                                    _("Debug: target couldn't trace all requested variables.\n"))
+                                self.LastComplainDebugToken = self.DebugToken
+
+
 
         buffers, self.DebugValuesBuffers = (self.DebugValuesBuffers,
                                             [list() for dummy in xrange(len(self.TracedIECPath))])
@@ -1558,6 +1574,15 @@
 
         return debug_status, ticks, buffers
 
+    RegisterDebugVariableErrorCodes = { 
+        # TRACE_LIST_OVERFLOW
+        1 : _("Debug: Too many variables traced. Max 1024.\n"),
+        # FORCE_LIST_OVERFLOW
+        2 : _("Debug: Too many variables forced. Max 256.\n"),
+        # FORCE_BUFFER_OVERFLOW
+        3 : _("Debug: Cumulated forced variables size too large. Max 1KB.\n")
+    }
+
     def RegisterDebugVarToConnector(self):
         Idxs = []
         self.TracedIECPath = []
@@ -1591,7 +1616,14 @@
                 IdxsT = zip(*Idxs)
                 self.TracedIECPath = IdxsT[3]
                 self.TracedIECTypes = IdxsT[1]
-                self.DebugToken = self._connector.SetTraceVariablesList(zip(*IdxsT[0:3]))
+                res = self._connector.SetTraceVariablesList(zip(*IdxsT[0:3]))
+                if res is not None and res > 0:
+                    self.DebugToken = res
+                else:
+                    self.DebugToken = None
+                    self.logger.write_warning(
+                        self.RegisterDebugVariableErrorCodes.get(
+                            -res, _("Debug: Unknown error")))
             else:
                 self.TracedIECPath = []
                 self._connector.SetTraceVariablesList([])