etherlab/EthercatCFileGenerator.py
changeset 2378 7aa47c09f8f5
parent 2377 88a9d64560d3
child 2379 015b724c30a5
equal deleted inserted replaced
2377:88a9d64560d3 2378:7aa47c09f8f5
    70 
    70 
    71 
    71 
    72 def ConfigureVariable(entry_infos, str_completion):
    72 def ConfigureVariable(entry_infos, str_completion):
    73     entry_infos["data_type"] = DATATYPECONVERSION.get(entry_infos["var_type"], None)
    73     entry_infos["data_type"] = DATATYPECONVERSION.get(entry_infos["var_type"], None)
    74     if entry_infos["data_type"] is None:
    74     if entry_infos["data_type"] is None:
    75         raise ValueError, _("Type of location \"%s\" not yet supported!") % entry_infos["var_name"]
    75         msg = _("Type of location \"%s\" not yet supported!") % entry_infos["var_name"]
       
    76         raise ValueError(msg)
    76 
    77 
    77     if not entry_infos.get("no_decl", False):
    78     if not entry_infos.get("no_decl", False):
    78         if "real_var" in entry_infos:
    79         if "real_var" in entry_infos:
    79             str_completion["located_variables_declaration"].append(
    80             str_completion["located_variables_declaration"].append(
    80                 "IEC_%(var_type)s %(real_var)s;" % entry_infos)
    81                 "IEC_%(var_type)s %(real_var)s;" % entry_infos)
   163             if name != entry_infos["infos"][2]:
   164             if name != entry_infos["infos"][2]:
   164                 if dir == "I":
   165                 if dir == "I":
   165                     entry_infos["infos"][4].append(name)
   166                     entry_infos["infos"][4].append(name)
   166                     return entry_infos["infos"][2]
   167                     return entry_infos["infos"][2]
   167                 else:
   168                 else:
   168                     raise ValueError, _("Output variables can't be defined with different locations (%s and %s)") % (entry_infos["infos"][2], name)
   169                     msg = _("Output variables can't be defined with different locations (%s and %s)") \
       
   170                           % (entry_infos["infos"][2], name)
       
   171                     raise ValueError(msg)
   169         else:
   172         else:
   170             raise ValueError, _("Definition conflict for location \"%s\"") % name
   173             raise ValueError(_("Definition conflict for location \"%s\"") % name)
   171 
   174 
   172     def GenerateCFile(self, filepath, location_str, master_number):
   175     def GenerateCFile(self, filepath, location_str, master_number):
   173 
   176 
   174         # Extract etherlab master code template
   177         # Extract etherlab master code template
   175         plc_etherlab_filepath = os.path.join(os.path.split(__file__)[0], "plc_etherlab.c")
   178         plc_etherlab_filepath = os.path.join(os.path.split(__file__)[0], "plc_etherlab.c")
   215             slave_pos = (slave_alias, alias[slave_alias])
   218             slave_pos = (slave_alias, alias[slave_alias])
   216 
   219 
   217             # Extract slave device informations
   220             # Extract slave device informations
   218             device, module_extra_params = self.Controler.GetModuleInfos(type_infos)
   221             device, module_extra_params = self.Controler.GetModuleInfos(type_infos)
   219             if device is None:
   222             if device is None:
   220                 raise ValueError, _("No informations found for device %s!") % (type_infos["device_type"])
   223                 msg = _("No informations found for device %s!") \
       
   224                       % (type_infos["device_type"])
       
   225                 raise ValueError(msg)
   221 
   226 
   222             # Extract slaves variables to be mapped
   227             # Extract slaves variables to be mapped
   223             slave_variables = self.UsedVariables.get(slave_idx, {})
   228             slave_variables = self.UsedVariables.get(slave_idx, {})
   224 
   229 
   225             # Extract slave device object dictionary entries
   230             # Extract slave device object dictionary entries
   391                             entry_type = entry.getDataType().getcontent()
   396                             entry_type = entry.getDataType().getcontent()
   392                             if entry_infos["var_type"] != entry_type:
   397                             if entry_infos["var_type"] != entry_type:
   393                                 message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
   398                                 message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
   394                                 if (self.Controler.GetSizeOfType(entry_infos["var_type"]) !=
   399                                 if (self.Controler.GetSizeOfType(entry_infos["var_type"]) !=
   395                                     self.Controler.GetSizeOfType(entry_type)):
   400                                     self.Controler.GetSizeOfType(entry_type)):
   396                                     raise ValueError, message
   401                                     raise ValueError(message)
   397                                 else:
   402                                 else:
   398                                     self.Controler.GetCTRoot().logger.write_warning(_("Warning: ") + message + "\n")
   403                                     self.Controler.GetCTRoot().logger.write_warning(_("Warning: ") + message + "\n")
   399 
   404 
   400                             if (entry_infos["dir"] == "I" and pdo_type != "Inputs" or
   405                             if (entry_infos["dir"] == "I" and pdo_type != "Inputs" or
   401                                 entry_infos["dir"] == "Q" and pdo_type != "Outputs"):
   406                                 entry_infos["dir"] == "Q" and pdo_type != "Outputs"):
   402                                 raise ValueError, _("Wrong direction for location \"%s\"!") % entry_infos["var_name"]
   407                                 raise ValueError(_("Wrong direction for location \"%s\"!") % entry_infos["var_name"])
   403 
   408 
   404                             ConfigureVariable(entry_infos, str_completion)
   409                             ConfigureVariable(entry_infos, str_completion)
   405 
   410 
   406                         elif pdo_type == "Outputs" and entry.getDataType() is not None and device_coe is not None:
   411                         elif pdo_type == "Outputs" and entry.getDataType() is not None and device_coe is not None:
   407                             data_type = entry.getDataType().getcontent()
   412                             data_type = entry.getDataType().getcontent()
   426                         if sm is None:
   431                         if sm is None:
   427                             for sm_idx, sync_manager in enumerate(sync_managers):
   432                             for sm_idx, sync_manager in enumerate(sync_managers):
   428                                 if sync_manager["name"] == pdo_type:
   433                                 if sync_manager["name"] == pdo_type:
   429                                     sm = sm_idx
   434                                     sm = sm_idx
   430                         if sm is None:
   435                         if sm is None:
   431                             raise ValueError, _("No sync manager available for %s pdo!") % pdo_type
   436                             raise ValueError(_("No sync manager available for %s pdo!") % pdo_type)
   432 
   437 
   433                         sync_managers[sm]["pdos_number"] += 1
   438                         sync_managers[sm]["pdos_number"] += 1
   434                         sync_managers[sm]["pdos"].append(
   439                         sync_managers[sm]["pdos"].append(
   435                             {"slave": slave_idx,
   440                             {"slave": slave_idx,
   436                              "index": pdo_index,
   441                              "index": pdo_index,
   458                     for (index, subindex), entry_declaration in slave_variables.iteritems():
   463                     for (index, subindex), entry_declaration in slave_variables.iteritems():
   459 
   464 
   460                         if not entry_declaration["mapped"]:
   465                         if not entry_declaration["mapped"]:
   461                             entry = device_entries.get((index, subindex), None)
   466                             entry = device_entries.get((index, subindex), None)
   462                             if entry is None:
   467                             if entry is None:
   463                                 raise ValueError, _("Unknown entry index 0x%4.4x, subindex 0x%2.2x for device %s") % \
   468                                 msg = _("Unknown entry index 0x%4.4x, subindex 0x%2.2x for device %s") \
   464                                                  (index, subindex, type_infos["device_type"])
   469                                       % (index, subindex, type_infos["device_type"])
       
   470                                 raise ValueError(msg)
   465 
   471 
   466                             entry_infos = {
   472                             entry_infos = {
   467                                 "index": index,
   473                                 "index": index,
   468                                 "subindex": subindex,
   474                                 "subindex": subindex,
   469                                 "name": entry["Name"],
   475                                 "name": entry["Name"],
   477 
   483 
   478                             if entry_infos["var_type"] != entry["Type"]:
   484                             if entry_infos["var_type"] != entry["Type"]:
   479                                 message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
   485                                 message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
   480                                 if (self.Controler.GetSizeOfType(entry_infos["var_type"]) !=
   486                                 if (self.Controler.GetSizeOfType(entry_infos["var_type"]) !=
   481                                     self.Controler.GetSizeOfType(entry["Type"])):
   487                                     self.Controler.GetSizeOfType(entry["Type"])):
   482                                     raise ValueError, message
   488                                     raise ValueError(message)
   483                                 else:
   489                                 else:
   484                                     self.Controler.GetCTRoot().logger.write_warning(message + "\n")
   490                                     self.Controler.GetCTRoot().logger.write_warning(message + "\n")
   485 
   491 
   486                             if entry_infos["dir"] == "I" and entry["PDOMapping"] in ["T", "RT"]:
   492                             if entry_infos["dir"] == "I" and entry["PDOMapping"] in ["T", "RT"]:
   487                                 pdo_type = "Inputs"
   493                                 pdo_type = "Inputs"
   488                             elif entry_infos["dir"] == "Q" and entry["PDOMapping"] in ["R", "RT"]:
   494                             elif entry_infos["dir"] == "Q" and entry["PDOMapping"] in ["R", "RT"]:
   489                                 pdo_type = "Outputs"
   495                                 pdo_type = "Outputs"
   490                             else:
   496                             else:
   491                                 raise ValueError, _("Wrong direction for location \"%s\"!") % entry_infos["var_name"]
   497                                 msg = _("Wrong direction for location \"%s\"!") \
       
   498                                       % entry_infos["var_name"]
       
   499                                 raise ValueError(msg)
   492 
   500 
   493                             if pdo_type not in dynamic_pdos:
   501                             if pdo_type not in dynamic_pdos:
   494                                 raise ValueError, _("No Sync manager defined for %s!") % pdo_type
   502                                 msg = _("No Sync manager defined for %s!") % pdo_type
       
   503                                 raise ValueError(msg)
   495 
   504 
   496                             ConfigureVariable(entry_infos, str_completion)
   505                             ConfigureVariable(entry_infos, str_completion)
   497 
   506 
   498                             if len(dynamic_pdos[pdo_type]["pdos"]) > 0:
   507                             if len(dynamic_pdos[pdo_type]["pdos"]) > 0:
   499                                 pdo = dynamic_pdos[pdo_type]["pdos"][0]
   508                                 pdo = dynamic_pdos[pdo_type]["pdos"][0]
   500                             elif module_extra_params["add_pdo"]:
   509                             elif module_extra_params["add_pdo"]:
   501                                 while dynamic_pdos[pdo_type]["current_index"] in pdos_index:
   510                                 while dynamic_pdos[pdo_type]["current_index"] in pdos_index:
   502                                     dynamic_pdos[pdo_type]["current_index"] += 1
   511                                     dynamic_pdos[pdo_type]["current_index"] += 1
   503                                 if dynamic_pdos[pdo_type]["current_index"] >= dynamic_pdos[pdo_type]["max_index"]:
   512                                 if dynamic_pdos[pdo_type]["current_index"] >= dynamic_pdos[pdo_type]["max_index"]:
   504                                     raise ValueError, _("No more free PDO index available for %s!") % pdo_type
   513                                     raise ValueError(_("No more free PDO index available for %s!") % pdo_type)
   505                                 pdos_index.append(dynamic_pdos[pdo_type]["current_index"])
   514                                 pdos_index.append(dynamic_pdos[pdo_type]["current_index"])
   506 
   515 
   507                                 dynamic_pdos_number += 1
   516                                 dynamic_pdos_number += 1
   508                                 pdo = {"slave": slave_idx,
   517                                 pdo = {"slave": slave_idx,
   509                                        "index": dynamic_pdos[pdo_type]["current_index"],
   518                                        "index": dynamic_pdos[pdo_type]["current_index"],