ProjectController.py
changeset 1432 8872223a675b
parent 1430 754fa90c8b27
child 1433 4a45f6642523
equal deleted inserted replaced
1431:df59be5afb08 1432:8872223a675b
   730         Reset variable and program list that are parsed from
   730         Reset variable and program list that are parsed from
   731         CSV file generated by IEC2C compiler.
   731         CSV file generated by IEC2C compiler.
   732         """
   732         """
   733         self._ProgramList = None
   733         self._ProgramList = None
   734         self._VariablesList = None
   734         self._VariablesList = None
       
   735         self._DbgVariablesList = None
   735         self._IECPathToIdx = {}
   736         self._IECPathToIdx = {}
   736         self._Ticktime = 0
   737         self._Ticktime = 0
   737         self.TracedIECPath = []
   738         self.TracedIECPath = []
   738 
   739 
   739     def GetIECProgramsAndVariables(self):
   740     def GetIECProgramsAndVariables(self):
   748                 # describes CSV columns
   749                 # describes CSV columns
   749                 ProgramsListAttributeName = ["num", "C_path", "type"]
   750                 ProgramsListAttributeName = ["num", "C_path", "type"]
   750                 VariablesListAttributeName = ["num", "vartype", "IEC_path", "C_path", "type"]
   751                 VariablesListAttributeName = ["num", "vartype", "IEC_path", "C_path", "type"]
   751                 self._ProgramList = []
   752                 self._ProgramList = []
   752                 self._VariablesList = []
   753                 self._VariablesList = []
       
   754                 self._DbgVariablesList = []
   753                 self._IECPathToIdx = {}
   755                 self._IECPathToIdx = {}
   754 
   756 
   755                 # Separate sections
   757                 # Separate sections
   756                 ListGroup = []
   758                 ListGroup = []
   757                 for line in open(csvfile,'r').xreadlines():
   759                 for line in open(csvfile,'r').xreadlines():
   772                     # Push this dictionnary into result.
   774                     # Push this dictionnary into result.
   773                     self._ProgramList.append(attrs)
   775                     self._ProgramList.append(attrs)
   774 
   776 
   775                 # second section contains all variables
   777                 # second section contains all variables
   776                 config_FBs = {}
   778                 config_FBs = {}
       
   779                 Idx = 0
   777                 for line in ListGroup[1]:
   780                 for line in ListGroup[1]:
   778                     # Split and Maps each field to dictionnary entries
   781                     # Split and Maps each field to dictionnary entries
   779                     attrs = dict(zip(VariablesListAttributeName,line.strip().split(';')))
   782                     attrs = dict(zip(VariablesListAttributeName,line.strip().split(';')))
   780                     # Truncate "C_path" to remove conf an ressources names
   783                     # Truncate "C_path" to remove conf an ressources names
   781                     parts = attrs["C_path"].split(".",2)
   784                     parts = attrs["C_path"].split(".",2)
   788                             attrs["C_path"] = '__'.join(parts[1:])
   791                             attrs["C_path"] = '__'.join(parts[1:])
   789                     else:
   792                     else:
   790                         attrs["C_path"] = '__'.join(parts)
   793                         attrs["C_path"] = '__'.join(parts)
   791                         if attrs["vartype"] == "FB":
   794                         if attrs["vartype"] == "FB":
   792                             config_FBs[tuple(parts)] = attrs["C_path"]
   795                             config_FBs[tuple(parts)] = attrs["C_path"]
   793                     # Push this dictionnary into result.
   796                     if attrs["vartype"] != "FB":
       
   797                         # Push this dictionnary into result.
       
   798                         self._DbgVariablesList.append(attrs)
       
   799                         # Fill in IEC<->C translation dicts
       
   800                         IEC_path=attrs["IEC_path"]
       
   801                         self._IECPathToIdx[IEC_path]=(Idx, attrs["type"])
       
   802                         # Ignores numbers given in CSV file
       
   803                         # Idx=int(attrs["num"])
       
   804                         # Count variables only, ignore FBs
       
   805                         Idx+=1
   794                     self._VariablesList.append(attrs)
   806                     self._VariablesList.append(attrs)
   795                     # Fill in IEC<->C translation dicts
       
   796                     IEC_path=attrs["IEC_path"]
       
   797                     Idx=int(attrs["num"])
       
   798                     self._IECPathToIdx[IEC_path]=(Idx, attrs["type"])
       
   799 
   807 
   800                 # third section contains ticktime
   808                 # third section contains ticktime
   801                 if len(ListGroup) > 2:
   809                 if len(ListGroup) > 2:
   802                     self._Ticktime = int(ListGroup[2][0])
   810                     self._Ticktime = int(ListGroup[2][0])
   803 
   811 
   814         Generate trace/debug code out of PLC variable list
   822         Generate trace/debug code out of PLC variable list
   815         """
   823         """
   816         self.GetIECProgramsAndVariables()
   824         self.GetIECProgramsAndVariables()
   817 
   825 
   818         # prepare debug code
   826         # prepare debug code
       
   827         variable_decl_array = []
       
   828         bofs = 0
       
   829         for v in self._DbgVariablesList :
       
   830             sz = DebugTypesSize.get(v["type"], 0)
       
   831             variable_decl_array += [
       
   832                 "{&(%(C_path)s), "%v+
       
   833                 {"EXT":"%(type)s_P_ENUM",
       
   834                  "IN":"%(type)s_P_ENUM",
       
   835                  "MEM":"%(type)s_O_ENUM",
       
   836                  "OUT":"%(type)s_O_ENUM",
       
   837                  "VAR":"%(type)s_ENUM"}[v["vartype"]]%v +
       
   838                  "}"]
       
   839             bofs += sz
   819         debug_code = targets.GetCode("plc_debug.c") % {
   840         debug_code = targets.GetCode("plc_debug.c") % {
   820            "buffer_size": reduce(lambda x, y: x + y, [DebugTypesSize.get(v["type"], 0) for v in self._VariablesList], 0),
   841            "buffer_size":bofs,
   821            "programs_declarations":
   842            "programs_declarations":
   822                "\n".join(["extern %(type)s %(C_path)s;"%p for p in self._ProgramList]),
   843                "\n".join(["extern %(type)s %(C_path)s;"%p for p in self._ProgramList]),
   823            "extern_variables_declarations":"\n".join([
   844            "extern_variables_declarations":"\n".join([
   824               {"EXT":"extern __IEC_%(type)s_p %(C_path)s;",
   845               {"EXT":"extern __IEC_%(type)s_p %(C_path)s;",
   825                "IN":"extern __IEC_%(type)s_p %(C_path)s;",
   846                "IN":"extern __IEC_%(type)s_p %(C_path)s;",
   826                "MEM":"extern __IEC_%(type)s_p %(C_path)s;",
   847                "MEM":"extern __IEC_%(type)s_p %(C_path)s;",
   827                "OUT":"extern __IEC_%(type)s_p %(C_path)s;",
   848                "OUT":"extern __IEC_%(type)s_p %(C_path)s;",
   828                "VAR":"extern __IEC_%(type)s_t %(C_path)s;",
   849                "VAR":"extern __IEC_%(type)s_t %(C_path)s;",
   829                "FB":"extern %(type)s %(C_path)s;"}[v["vartype"]]%v
   850                "FB":"extern %(type)s %(C_path)s;"}[v["vartype"]]%v
   830                for v in self._VariablesList if v["C_path"].find('.')<0]),
   851                for v in self._VariablesList if v["C_path"].find('.')<0]),
   831            "for_each_variable_do_code":"\n".join([
   852            "variable_decl_array": ",\n".join(variable_decl_array)
   832                {"EXT":"    (*fp)((void*)&(%(C_path)s),%(type)s_P_ENUM);\n",
   853            }
   833                 "IN":"    (*fp)((void*)&(%(C_path)s),%(type)s_P_ENUM);\n",
       
   834                 "MEM":"    (*fp)((void*)&(%(C_path)s),%(type)s_O_ENUM);\n",
       
   835                 "OUT":"    (*fp)((void*)&(%(C_path)s),%(type)s_O_ENUM);\n",
       
   836                 "VAR":"    (*fp)((void*)&(%(C_path)s),%(type)s_ENUM);\n"}[v["vartype"]]%v
       
   837                 for v in self._VariablesList if v["vartype"] != "FB" and v["type"] in DebugTypesSize ]),
       
   838            "find_variable_case_code":"\n".join([
       
   839                "    case %(num)s:\n"%v+
       
   840                "        *varp = (void*)&(%(C_path)s);\n"%v+
       
   841                {"EXT":"        return %(type)s_P_ENUM;\n",
       
   842                 "IN":"        return %(type)s_P_ENUM;\n",
       
   843                 "MEM":"        return %(type)s_O_ENUM;\n",
       
   844                 "OUT":"        return %(type)s_O_ENUM;\n",
       
   845                 "VAR":"        return %(type)s_ENUM;\n"}[v["vartype"]]%v
       
   846                 for v in self._VariablesList if v["vartype"] != "FB" and v["type"] in DebugTypesSize ])}
       
   847 
   854 
   848         return debug_code
   855         return debug_code
   849 
   856 
   850     def Generate_plc_main(self):
   857     def Generate_plc_main(self):
   851         """
   858         """
  1305             IECdebug_data[0].pop(callableobj,None)
  1312             IECdebug_data[0].pop(callableobj,None)
  1306             if len(IECdebug_data[0]) == 0:
  1313             if len(IECdebug_data[0]) == 0:
  1307                 self.IECdebug_datas.pop(IECPath)
  1314                 self.IECdebug_datas.pop(IECPath)
  1308             else:
  1315             else:
  1309                 IECdebug_data[4] = reduce(
  1316                 IECdebug_data[4] = reduce(
  1310                     lambda x, y: x|y, 
  1317                     lambda x, y: x|y,
  1311                     IECdebug_data[0].itervalues(),
  1318                     IECdebug_data[0].itervalues(),
  1312                     False)
  1319                     False)
  1313         self.IECdebug_lock.release()
  1320         self.IECdebug_lock.release()
  1314 
  1321 
  1315         self.ReArmDebugRegisterTimer()
  1322         self.ReArmDebugRegisterTimer()