etherlab/EthercatMaster.py
changeset 2643 b98d9e08231f
parent 2456 7373e3048167
parent 2641 c9deff128c37
child 2703 32ffdb32b14e
equal deleted inserted replaced
2640:1b4b335e19ea 2643:b98d9e08231f
   241 
   241 
   242     def __init__(self):
   242     def __init__(self):
   243         config_filepath = self.ConfigFileName()
   243         config_filepath = self.ConfigFileName()
   244         config_is_saved = False
   244         config_is_saved = False
   245         self.Config = None
   245         self.Config = None
   246         if os.path.isfile(config_filepath):
   246         # if os.path.isfile(config_filepath):
   247             config_xmlfile = open(config_filepath, 'r')
   247         #     config_xmlfile = open(config_filepath, 'r')
   248             try:
   248         #     try:
   249                 self.Config, error = \
   249         #         self.Config, error = \
   250                     EtherCATConfigParser.LoadXMLString(config_xmlfile.read())
   250         #             EtherCATConfigParser.LoadXMLString(config_xmlfile.read())
   251                 if error is None:
   251         #         if error is None:
   252                     config_is_saved = True
   252         #             config_is_saved = True
   253             except Exception as e:
   253         #     except Exception as e:
   254                 error = str(e)
   254         #         error = str(e)
   255             config_xmlfile.close()
   255         #     config_xmlfile.close()
   256 
   256 
   257             if error is not None:
   257         #     if error is not None:
   258                 self.GetCTRoot().logger.write_error(
   258         #         self.GetCTRoot().logger.write_error(
   259                     _("Couldn't load %s network configuration file.") % self.CTNName())
   259         #             _("Couldn't load %s network configuration file.") % self.CTNName())
   260 
   260 
   261         if self.Config is None:
   261         if self.Config is None:
   262             self.Config = EtherCATConfigParser.CreateElement("EtherCATConfig")
   262             self.Config = EtherCATConfigParser.CreateElement("EtherCATConfig")
   263 
   263 
   264         process_filepath = self.ProcessVariablesFileName()
   264         process_filepath = self.ProcessVariablesFileName()
   280                     _("Couldn't load %s network process variables file.") % self.CTNName())
   280                     _("Couldn't load %s network process variables file.") % self.CTNName())
   281 
   281 
   282         if self.ProcessVariables is None:
   282         if self.ProcessVariables is None:
   283             self.ProcessVariables = ProcessVariablesParser.CreateElement("ProcessVariables")
   283             self.ProcessVariables = ProcessVariablesParser.CreateElement("ProcessVariables")
   284 
   284 
   285         if config_is_saved and process_is_saved:
   285         #if config_is_saved and process_is_saved:
       
   286         if process_is_saved:
   286             self.CreateBuffer(True)
   287             self.CreateBuffer(True)
   287         else:
   288         else:
   288             self.CreateBuffer(False)
   289             self.CreateBuffer(False)
   289             self.OnCTNSave()
   290             self.OnCTNSave()
       
   291 
       
   292         if os.path.isfile(config_filepath):
       
   293             config_xmlfile = open(config_filepath, 'r')
       
   294             try:
       
   295                 self.Config, error = \
       
   296                     EtherCATConfigParser.LoadXMLString(config_xmlfile.read())
       
   297                 if error is None:
       
   298                     config_is_saved = True
       
   299             except Exception, e:
       
   300                 error = e.message
       
   301             config_xmlfile.close()
       
   302             
       
   303             if error is not None:
       
   304                 self.GetCTRoot().logger.write_error(
       
   305                     _("Couldn't load %s network configuration file.") % CTNName)
   290 
   306 
   291         # ----------- call ethercat mng. function --------------
   307         # ----------- call ethercat mng. function --------------
   292         self.CommonMethod = _CommonSlave(self)
   308         self.CommonMethod = _CommonSlave(self)
   293 
   309 
   294     def GetIconName(self):
   310     def GetIconName(self):
   304                                            self.GetSlaveTypesLibrary())
   320                                            self.GetSlaveTypesLibrary())
   305         if dialog.ShowModal() == wx.ID_OK:
   321         if dialog.ShowModal() == wx.ID_OK:
   306             type_infos = dialog.GetValueInfos()
   322             type_infos = dialog.GetValueInfos()
   307             device, _module_extra_params = self.GetModuleInfos(type_infos)
   323             device, _module_extra_params = self.GetModuleInfos(type_infos)
   308             if device is not None:
   324             if device is not None:
   309                 if HAS_MCL and _EthercatCIA402SlaveCTN.NODE_PROFILE in device.GetProfileNumbers():
   325                 if HAS_MCL and str(_EthercatCIA402SlaveCTN.NODE_PROFILE) in device.GetProfileNumbers():
   310                     ConfNodeType = "EthercatCIA402Slave"
   326                     ConfNodeType = "EthercatCIA402Slave"
   311                 else:
   327                 else:
   312                     ConfNodeType = "EthercatSlave"
   328                     ConfNodeType = "EthercatSlave"
   313                 new_child = self.CTNAddChild("%s_0" % ConfNodeType, ConfNodeType)
   329                 new_child = self.CTNAddChild("%s_0" % ConfNodeType, ConfNodeType)
   314                 new_child.SetParamsAttribute("SlaveParams.Type", type_infos)
   330                 new_child.SetParamsAttribute("SlaveParams.Type", type_infos)
   575                               "sync_managers": device.GetSyncManagers(),
   591                               "sync_managers": device.GetSyncManagers(),
   576                               "entries": self.GetSlaveVariables(device)})
   592                               "entries": self.GetSlaveVariables(device)})
   577                 return infos
   593                 return infos
   578         return None
   594         return None
   579 
   595 
   580     def GetSlaveVariables(self, slave_pos=None, limits=None, device=None):
   596     def GetSlaveVariables(self, slave_pos=None, limits=None, device=None, module=None):
       
   597         # add jblee
       
   598         files = os.listdir(self.CTNPath())
       
   599         moduleNames = []
       
   600         modulePos = 1
       
   601         for file in files:
       
   602             filepath = os.path.join(self.CTNPath(), file)
       
   603             if os.path.isdir(filepath):
       
   604                 MDPFilePath = os.path.join(filepath, "DataForMDP.txt")
       
   605                 CheckConfNodePath = os.path.join(filepath, "baseconfnode.xml")
       
   606 
       
   607                 try :
       
   608                     moduleDataFile = open(MDPFilePath, 'r')
       
   609                     confNodeFile = open(CheckConfNodePath, 'r')
       
   610 
       
   611                     lines = moduleDataFile.readlines()
       
   612                     checklines = confNodeFile.readlines()
       
   613 
       
   614                     moduleDataFile.close()
       
   615                     confNodeFile.close()
       
   616 
       
   617                     module_info = self.GetModuleEntryList()
       
   618                     # checklines(ex) : <BaseParams xmlns:xsd="http://www.w3.org/2001/XMLSchema" IEC_Channel="0" Name="EthercatSlave_0"/>
       
   619                     # checklines[1].split() : [<BaseParams, xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       
   620                     #                           IEC_Channel="0", Name="EthercatSlave_0"/>]
       
   621                     # checklines[1].split()[2] : IEC_Channel="0"
       
   622                     # checklines[1].split()[2].split("\"") = [IEC_Channel=, 0, ]
       
   623                     pos_check = int(checklines[1].split()[2].split("\"")[1])
       
   624 
       
   625                     if slave_pos != pos_check:
       
   626                         continue
       
   627 
       
   628                     for line in lines:
       
   629                         if line == "\n":
       
   630                             continue
       
   631                         # module_name : ST-1214, ST-2314, ...
       
   632                         # if user add module => ST-1214 3EA, ST-2314 3EA
       
   633                         # each result_module_name : 
       
   634                         #    (ST-1214, Module 1), (ST-1214, Module 2), (ST-1214, Module 3)
       
   635                         #    (ST-2314, Module 4), (ST-2314, Module 5), (ST-2314, Module 6)  
       
   636                         module_name = line.split()[0]
       
   637                         result_module_name = module_name + ", Module %d" % modulePos
       
   638                         moduleNames.append(result_module_name)
       
   639                         modulePos += 1
       
   640                 except :
       
   641                     pass
       
   642 
   581         if device is None and slave_pos is not None:
   643         if device is None and slave_pos is not None:
   582             slave = self.GetSlave(slave_pos)
   644             slave = self.GetSlave(slave_pos)
   583             if slave is not None:
   645             if slave is not None:
   584                 type_infos = slave.getType()
   646                 type_infos = slave.getType()
   585                 device, _module_extra_params = self.GetModuleInfos(type_infos)
   647                 device, _module_extra_params = self.GetModuleInfos(type_infos)
   586         if device is not None:
   648         if device is not None:
       
   649             # Test OD
   587             entries = device.GetEntriesList(limits)
   650             entries = device.GetEntriesList(limits)
       
   651             #entries = self.CTNParent.GetEntriesList()
   588             entries_list = entries.items()
   652             entries_list = entries.items()
   589             entries_list.sort()
   653             entries_list.sort()
   590             entries = []
   654             entries = []
   591             current_index = None
   655             current_index = None
   592             current_entry = {}
   656             current_entry = {}
   600                     entries.append(entry)
   664                     entries.append(entry)
   601                 elif current_entry:
   665                 elif current_entry:
   602                     current_entry["children"].append(entry)
   666                     current_entry["children"].append(entry)
   603                 else:
   667                 else:
   604                     entries.append(entry)
   668                     entries.append(entry)
       
   669 
       
   670             increment = self.CTNParent.GetModuleIncrement()[0]
       
   671             count = 1
       
   672             #print module_info
       
   673             # moduleNameAndPos : (ST-1214, Module 1), (ST-1214, Module 2), ... ,
       
   674             # moduleNameAndPos.split(",") : ["ST-1214", " Module 1"]
       
   675             # moduleNameAndPos.split(",")[0] : "ST-1214"
       
   676             for moduleNameAndPos in moduleNames:
       
   677                 moduleName = moduleNameAndPos.split(",")[0]
       
   678                 modulePosName = moduleNameAndPos.split(",")[1]
       
   679                 idx_increment = int(increment) * count
       
   680                 
       
   681                 for MDP_entry in module_info.get(moduleName):
       
   682                     LocalMDPEntry = []
       
   683                     #print MDP_entry
       
   684                     local_idx = MDP_entry["Index"]
       
   685                     if ExtractHexDecValue(local_idx) == 0: #and local_idx[0] == "#":
       
   686                         temp_index = ExtractHexDecValue(local_idx)
       
   687                     else :
       
   688                         temp_index = ExtractHexDecValue(MDP_entry["Index"]) + idx_increment
       
   689                     #temp_index = ExtractHexDecValue(MDP_entry["Index"]) + idx_increment
       
   690                     entry_index = hex(temp_index)
       
   691                     entry_subidx = MDP_entry["SubIndex"]
       
   692                     entry_name = MDP_entry["Name"] + ", " + " " + \
       
   693                                  moduleName + " - " + modulePosName
       
   694                     entry_type = MDP_entry["Type"]
       
   695                     entry_bitsize = MDP_entry["BitSize"]
       
   696                     entry_access = MDP_entry["Access"]
       
   697                     mapping_type = MDP_entry["PDOMapping"]
       
   698 
       
   699                     LocalMDPEntry.append({
       
   700                         "Index": entry_index,
       
   701                         "SubIndex": entry_subidx,
       
   702                         "Name": entry_name,
       
   703                         "Type": entry_type,
       
   704                         "BitSize": entry_bitsize,
       
   705                         "Access": entry_access, 
       
   706                         "PDOMapping": mapping_type,
       
   707                         "children": ""})
       
   708                     entries.append(LocalMDPEntry[0])
       
   709                 count += 1
       
   710             
       
   711             #print entries
   605             return entries
   712             return entries
   606         return []
   713         return []
   607 
   714 
   608     def GetSlaveVariableDataType(self, slave_pos, index, subindex):
   715     def GetSlaveVariableDataType(self, slave_pos, index, subindex):
   609         slave = self.GetSlave(slave_pos)
   716         slave = self.GetSlave(slave_pos)
   632         return entries
   739         return entries
   633 
   740 
   634     def GetModuleInfos(self, type_infos):
   741     def GetModuleInfos(self, type_infos):
   635         return self.CTNParent.GetModuleInfos(type_infos)
   742         return self.CTNParent.GetModuleInfos(type_infos)
   636 
   743 
       
   744     # add jblee
       
   745     def GetModuleEntryList(self):
       
   746         return self.CTNParent.GetModuleEntryList()
       
   747     
   637     def GetSlaveTypesLibrary(self, profile_filter=None):
   748     def GetSlaveTypesLibrary(self, profile_filter=None):
   638         return self.CTNParent.GetModulesLibrary(profile_filter)
   749         return self.CTNParent.GetModulesLibrary(profile_filter)
   639 
   750 
   640     def GetLibraryVendors(self):
   751     def GetLibraryVendors(self):
   641         return self.CTNParent.GetVendors()
   752         return self.CTNParent.GetVendors()
   642 
   753 
   643     def GetDeviceLocationTree(self, slave_pos, current_location, device_name):
   754     def GetDeviceLocationTree(self, slave_pos, current_location, device_name):
   644         slave = self.GetSlave(slave_pos)
   755         slave = self.GetSlave(slave_pos)
   645         vars = []
   756         vars = []
       
   757 
       
   758         # add jblee
       
   759         files = os.listdir(self.CTNPath())
       
   760         moduleNames = []
       
   761         modulePos = 1
       
   762         for file in files:
       
   763             filepath = os.path.join(self.CTNPath(), file)
       
   764             if os.path.isdir(filepath):
       
   765                 MDPFilePath = os.path.join(filepath, "DataForMDP.txt")
       
   766                 CheckConfNodePath = os.path.join(filepath, "baseconfnode.xml")
       
   767 
       
   768                 try :
       
   769                     moduleDataFile = open(MDPFilePath, 'r')
       
   770                     confNodeFile = open(CheckConfNodePath, 'r')
       
   771 
       
   772                     lines = moduleDataFile.readlines()
       
   773                     checklines = confNodeFile.readlines()
       
   774 
       
   775                     moduleDataFile.close()
       
   776                     confNodeFile.close()
       
   777 
       
   778                     module_info = self.GetModuleEntryList()
       
   779                     # checklines(ex) : <BaseParams xmlns:xsd="http://www.w3.org/2001/XMLSchema" IEC_Channel="0" Name="EthercatSlave_0"/>
       
   780                     # checklines[1].split() : [<BaseParams, xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       
   781                     #                           IEC_Channel="0", Name="EthercatSlave_0"/>]
       
   782                     # checklines[1].split()[2] : IEC_Channel="0"
       
   783                     # checklines[1].split()[2].split("\"") = [IEC_Channel=, 0, ]
       
   784                     pos_check = int(checklines[1].split()[2].split("\"")[1])
       
   785 
       
   786                     if slave_pos != pos_check:
       
   787                         continue
       
   788 
       
   789                     for line in lines:
       
   790                         if line == "\n":
       
   791                             continue
       
   792                         # module_name : ST-1214, ST-2314, ...
       
   793                         # if user add module => ST-1214 3EA, ST-2314 3EA
       
   794                         # each result_module_name : 
       
   795                         #    (ST-1214, Module 1), (ST-1214, Module 2), (ST-1214, Module 3)
       
   796                         #    (ST-2314, Module 4), (ST-2314, Module 5), (ST-2314, Module 6)  
       
   797                         module_name = line.split()[0]
       
   798                         result_module_name = module_name + ", Module %d" % modulePos
       
   799                         moduleNames.append(result_module_name)
       
   800                         modulePos += 1
       
   801                 except :
       
   802                     pass
       
   803 
   646         if slave is not None:
   804         if slave is not None:
   647             type_infos = slave.getType()
   805             type_infos = slave.getType()
   648 
   806 
   649             device, _module_extra_params = self.GetModuleInfos(type_infos)
   807             device, _module_extra_params = self.GetModuleInfos(type_infos)
   650             if device is not None:
   808             if device is not None:
   679                                                                                         (index, subindex)))),
   837                                                                                         (index, subindex)))),
   680                                 "description": "",
   838                                 "description": "",
   681                                 "children": [],
   839                                 "children": [],
   682                             })
   840                             })
   683 
   841 
       
   842                 # add jblee for MDP
       
   843                 if not entries :
       
   844                     increment = self.CTNParent.GetModuleIncrement()[0]
       
   845                     count = 1
       
   846                     for moduleNameAndPos in moduleNames:
       
   847                         moduleName = moduleNameAndPos.split(",")[0]
       
   848                         idx_increment = int(increment) * count
       
   849                         for MDP_entry in module_info.get(moduleName):
       
   850                             local_idx = MDP_entry["Index"]
       
   851                             if ExtractHexDecValue(local_idx) != 0 and local_idx[0] == "#":
       
   852                                 index = ExtractHexDecValue(local_idx) + idx_increment
       
   853                             else :
       
   854                                 index = ExtractHexDecValue(MDP_entry["Index"])
       
   855                             subindex = int(MDP_entry["SubIndex"])
       
   856                             var_class = VARCLASSCONVERSION.get(MDP_entry["PDOMapping"], None)
       
   857                             if var_class is not None:
       
   858                                 if var_class == LOCATION_VAR_INPUT:
       
   859                                     var_dir = "%I"
       
   860                                 else:
       
   861                                     var_dir = "%Q"
       
   862                             var_size = self.GetSizeOfType(MDP_entry["Type"])
       
   863                             result_name = MDP_entry["Name"] + ", " + moduleNameAndPos
       
   864                             vars.append({"name": "0x%4.4x-0x%2.2x: %s" % (index, subindex, result_name),
       
   865                                          "type": var_class,
       
   866                                          "size": var_size,
       
   867                                          "IEC_type": MDP_entry["Type"],
       
   868                                          "var_name": "%s_%4.4x_%2.2x" % ("_".join(moduleName.split()), index, subindex),
       
   869                                          "location": "%s%s%s"%(var_dir, var_size, ".".join(map(str, current_location + 
       
   870                                                                                                     (index, subindex)))),
       
   871                                          "description": "",
       
   872                                          "children": []})
       
   873                         count += 1
       
   874 
   684         return vars
   875         return vars
   685 
   876 
   686     def CTNTestModified(self):
   877     def CTNTestModified(self):
   687         return self.ChangesToSave or not self.ModelIsSaved()
   878         return self.ChangesToSave or not self.ModelIsSaved()
   688 
   879 
   689     def OnCTNSave(self, from_project_path=None):
   880     def OnCTNSave(self, from_project_path=None):
   690         config_filepath = self.ConfigFileName()
   881         config_filepath = self.ConfigFileName()
   691 
       
   692         config_xmlfile = open(config_filepath, "w")
   882         config_xmlfile = open(config_filepath, "w")
   693         config_xmlfile.write(etree.tostring(
   883         config_xmlfile.write(etree.tostring(
   694             self.Config,
   884             self.Config,
   695             pretty_print=True,
   885             pretty_print=True,
   696             xml_declaration=True,
   886             xml_declaration=True,