andrej@2354: #!/usr/bin/env python andrej@2354: # -*- coding: utf-8 -*- andrej@2354: andrej@2354: # This file is part of Beremiz andrej@2354: # andrej@2354: # Copyright (C) 2013: Real-Time & Embedded Systems (RTES) Lab., University of Seoul andrej@2354: # andrej@2354: # See COPYING file for copyrights details. andrej@2354: andrej@2405: from __future__ import absolute_import andrej@2354: import os andrej@2390: import string andrej@2390: from xml.dom import minidom andrej@2354: andrej@2354: import wx andrej@2354: import wx.grid andrej@2354: import wx.gizmos andrej@2354: import wx.lib.buttons andrej@2354: andrej@2354: # -------------------------------------------------------------------- andrej@2354: from controls import CustomGrid, CustomTable andrej@2416: from runtime import PlcStatus andrej@2354: # -------------------------------------------------------------------- andrej@2354: andrej@2355: # ------------ for register management --------------- andrej@2390: andrej@2375: from util.TranslationCatalogs import NoTranslate andrej@2356: # ------------------------------------------------------------- andrej@2354: andrej@2360: andrej@2354: # ----------------------------- For Sync Manager Table ----------------------------------- andrej@2354: def GetSyncManagersTableColnames(): andrej@2354: """ andrej@2354: Returns column names of SyncManager Table in Slave state panel. andrej@2354: """ andrej@2375: _ = NoTranslate andrej@2354: return ["#", _("Name"), _("Start Address"), _("Default Size"), _("Control Byte"), _("Enable")] andrej@2354: andrej@2360: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # Sync Managers Table andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class SyncManagersTable(CustomTable): andrej@2355: def GetValue(self, row, col): andrej@2354: if row < self.GetNumberRows(): andrej@2354: if col == 0: andrej@2354: return row andrej@2354: return self.data[row].get(self.GetColLabelValue(col, False), "") andrej@2354: andrej@2360: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # EtherCAT Management Treebook andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class EtherCATManagementTreebook(wx.Treebook): andrej@2354: def __init__(self, parent, controler, node_editor): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent wx.ScrolledWindow object andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: @param node_editor: Reference to Beremiz frame andrej@2354: """ andrej@2354: wx.Treebook.__init__(self, parent, -1, size=wx.DefaultSize, style=wx.BK_DEFAULT) andrej@2354: self.parent = parent andrej@2354: self.Controler = controler andrej@2354: self.NodeEditor = node_editor andrej@2355: andrej@2354: self.EtherCATManagementClassObject = {} andrej@2355: andrej@2354: # fill EtherCAT Management Treebook andrej@2379: panels = [ andrej@2354: ("Slave State", SlaveStatePanelClass, []), andrej@2354: ("SDO Management", SDOPanelClass, []), andrej@2354: ("PDO Monitoring", PDOPanelClass, []), andrej@2355: ("ESC Management", EEPROMAccessPanel, [ andrej@2379: ("Smart View", SlaveSiiSmartView), andrej@2379: ("Hex View", HexView)]), andrej@2379: ("Register Access", RegisterAccessPanel, []) andrej@2379: ] andrej@2379: for pname, pclass, subs in panels: andrej@2379: self.AddPage(pclass(self, self.Controler), pname) andrej@2379: for spname, spclass in subs: andrej@2379: self.AddSubPage(spclass(self, self.Controler), spname) andrej@2354: andrej@2360: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For SlaveState Panel andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class SlaveStatePanelClass(wx.Panel): andrej@2354: def __init__(self, parent, controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent EtherCATManagementTreebook class andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2366: wx.Panel.__init__(self, parent, -1, (0, 0), size=wx.DefaultSize, style=wx.SUNKEN_BORDER) andrej@2354: self.Controler = controler andrej@2354: self.parent = parent andrej@2355: andrej@2354: # initialize SlaveStatePanel UI dictionaries andrej@2354: self.StaticBoxDic = {} andrej@2354: self.StaticTextDic = {} andrej@2354: self.TextCtrlDic = {} andrej@2354: self.ButtonDic = {} andrej@2355: andrej@2354: # iniitalize BoxSizer and FlexGridSizer andrej@2354: self.SizerDic = { andrej@2375: "SlaveState_main_sizer": wx.BoxSizer(wx.VERTICAL), andrej@2375: "SlaveState_inner_main_sizer": wx.FlexGridSizer(cols=1, hgap=50, rows=3, vgap=10), andrej@2375: "SlaveInfosDetailsInnerSizer": wx.FlexGridSizer(cols=4, hgap=10, rows=2, vgap=10), andrej@2375: "SyncManagerInnerSizer": wx.FlexGridSizer(cols=1, hgap=5, rows=1, vgap=5), andrej@2375: "SlaveState_sizer": wx.FlexGridSizer(cols=1, hgap=10, rows=2, vgap=10), andrej@2375: "SlaveState_up_sizer": wx.FlexGridSizer(cols=4, hgap=10, rows=2, vgap=10), andrej@2375: "SlaveState_down_sizer": wx.FlexGridSizer(cols=2, hgap=10, rows=1, vgap=10)} andrej@2355: andrej@2354: # initialize StaticBox and StaticBoxSizer andrej@2354: for box_name, box_label in [ andrej@2354: ("SlaveInfosDetailsBox", "Slave Informations"), andrej@2354: ("SyncManagerBox", "Sync Manager"), andrej@2354: ("SlaveStateBox", "Slave State Transition && Monitoring")]: andrej@2354: self.StaticBoxDic[box_name] = wx.StaticBox(self, label=_(box_label)) andrej@2355: self.SizerDic[box_name] = wx.StaticBoxSizer(self.StaticBoxDic[box_name]) andrej@2355: andrej@2354: for statictext_name, statictext_label, textctrl_name in [ andrej@2354: ("VendorLabel", "Vendor:", "vendor"), andrej@2354: ("ProductcodeLabel", "Product code:", "product_code"), andrej@2354: ("RevisionnumberLabel", "Slave Count:", "revision_number"), andrej@2354: ("PhysicsLabel", "Physics:", "physics")]: andrej@2354: self.StaticTextDic[statictext_name] = wx.StaticText(self, label=_(statictext_label)) andrej@2354: self.TextCtrlDic[textctrl_name] = wx.TextCtrl(self, size=wx.Size(130, 24), style=wx.TE_READONLY) andrej@2355: self.SizerDic["SlaveInfosDetailsInnerSizer"].AddMany([self.StaticTextDic[statictext_name], andrej@2381: self.TextCtrlDic[textctrl_name]]) andrej@2355: andrej@2354: self.SizerDic["SlaveInfosDetailsBox"].AddSizer(self.SizerDic["SlaveInfosDetailsInnerSizer"]) andrej@2355: andrej@2363: self.SyncManagersGrid = CustomGrid(self, size=wx.Size(605, 155), style=wx.VSCROLL) andrej@2355: andrej@2355: self.SizerDic["SyncManagerInnerSizer"].Add(self.SyncManagersGrid) andrej@2354: self.SizerDic["SyncManagerBox"].Add(self.SizerDic["SyncManagerInnerSizer"]) andrej@2355: andrej@2375: buttons = [ andrej@2375: ("InitButton", 0, "INIT", "State Transition to \"Init\" State", self.OnButtonClick, []), andrej@2375: ("PreOPButton", 1, "PREOP", "State Transition to \"PreOP\" State", self.OnButtonClick, [ andrej@2375: ("TargetStateLabel", "Target State:", "TargetState")]), andrej@2375: ("SafeOPButton", 2, "SAFEOP", "State Transition to \"SafeOP\" State", self.OnButtonClick, []), andrej@2375: ("OPButton", 3, "OP", "State Transition to \"OP\" State", self.OnButtonClick, [ andrej@2375: ("CurrentStateLabel", "Current State:", "CurrentState")]) andrej@2375: ] andrej@2375: for button_name, button_id, button_label, button_tooltipstring, event_method, sub_item in buttons: andrej@2363: self.ButtonDic[button_name] = wx.Button(self, id=button_id, label=_(button_label)) andrej@2354: self.ButtonDic[button_name].Bind(wx.EVT_BUTTON, event_method) andrej@2354: self.ButtonDic[button_name].SetToolTipString(button_tooltipstring) andrej@2354: self.SizerDic["SlaveState_up_sizer"].Add(self.ButtonDic[button_name]) andrej@2375: for statictext_name, statictext_label, textctrl_name in sub_item: andrej@2354: self.StaticTextDic[statictext_name] = wx.StaticText(self, label=_(statictext_label)) andrej@2354: self.TextCtrlDic[textctrl_name] = wx.TextCtrl(self, size=wx.DefaultSize, style=wx.TE_READONLY) andrej@2355: self.SizerDic["SlaveState_up_sizer"].AddMany([self.StaticTextDic[statictext_name], andrej@2380: self.TextCtrlDic[textctrl_name]]) andrej@2355: andrej@2354: for button_name, button_label, button_tooltipstring, event_method in [ andrej@2354: ("StartTimerButton", "Start State Monitoring", "Slave State Update Restart", self.StartTimer), andrej@2354: ("StopTimerButton", "Stop State Monitoring", "Slave State Update Stop", self.CurrentStateThreadStop)]: andrej@2354: self.ButtonDic[button_name] = wx.Button(self, label=_(button_label)) andrej@2354: self.ButtonDic[button_name].Bind(wx.EVT_BUTTON, event_method) andrej@2354: self.ButtonDic[button_name].SetToolTipString(button_tooltipstring) andrej@2355: self.SizerDic["SlaveState_down_sizer"].Add(self.ButtonDic[button_name]) andrej@2355: andrej@2355: self.SizerDic["SlaveState_sizer"].AddMany([self.SizerDic["SlaveState_up_sizer"], andrej@2381: self.SizerDic["SlaveState_down_sizer"]]) andrej@2355: andrej@2354: self.SizerDic["SlaveStateBox"].Add(self.SizerDic["SlaveState_sizer"]) andrej@2355: andrej@2354: self.SizerDic["SlaveState_inner_main_sizer"].AddMany([ andrej@2354: self.SizerDic["SlaveInfosDetailsBox"], self.SizerDic["SyncManagerBox"], andrej@2354: self.SizerDic["SlaveStateBox"]]) andrej@2355: andrej@2354: self.SizerDic["SlaveState_main_sizer"].Add(self.SizerDic["SlaveState_inner_main_sizer"]) andrej@2355: andrej@2354: self.SetSizer(self.SizerDic["SlaveState_main_sizer"]) andrej@2355: andrej@2354: # register a timer for periodic exectuion of slave state update (period: 1000 ms) andrej@2354: self.Bind(wx.EVT_TIMER, self.GetCurrentState) andrej@2355: andrej@2354: self.CreateSyncManagerTable() andrej@2355: andrej@2354: self.Centre() andrej@2355: andrej@2354: def CreateSyncManagerTable(self): andrej@2354: """ andrej@2354: Create grid for "SyncManager" andrej@2354: """ andrej@2355: # declare Table object andrej@2354: self.SyncManagersTable = SyncManagersTable(self, [], GetSyncManagersTableColnames()) andrej@2354: self.SyncManagersGrid.SetTable(self.SyncManagersTable) andrej@2354: # set grid alignment attr. (CENTER) andrej@2355: self.SyncManagersGridColAlignements = [wx.ALIGN_CENTRE, wx.ALIGN_CENTRE, wx.ALIGN_CENTRE, andrej@2354: wx.ALIGN_CENTRE, wx.ALIGN_CENTRE, wx.ALIGN_CENTRE] andrej@2354: # set grid size andrej@2354: self.SyncManagersGridColSizes = [40, 150, 100, 100, 100, 100] andrej@2354: self.SyncManagersGrid.SetRowLabelSize(0) andrej@2354: for col in range(self.SyncManagersTable.GetNumberCols()): andrej@2354: attr = wx.grid.GridCellAttr() andrej@2354: attr.SetAlignment(self.SyncManagersGridColAlignements[col], wx.ALIGN_CENTRE) andrej@2354: self.SyncManagersGrid.SetColAttr(col, attr) andrej@2354: self.SyncManagersGrid.SetColMinimalWidth(col, self.SyncManagersGridColSizes[col]) andrej@2355: self.SyncManagersGrid.AutoSizeColumn(col, False) andrej@2355: andrej@2354: self.RefreshSlaveInfos() andrej@2355: andrej@2354: def RefreshSlaveInfos(self): andrej@2354: """ andrej@2354: Fill data in "Slave Information" and "SyncManager" andrej@2354: """ andrej@2354: slave_infos = self.Controler.GetSlaveInfos() andrej@2354: sync_manager_section = ["vendor", "product_code", "revision_number", "physics"] andrej@2354: if slave_infos is not None: andrej@2355: # this method is same as "TextCtrl.SetValue" andrej@2354: for textctrl_name in sync_manager_section: andrej@2354: self.TextCtrlDic[textctrl_name].SetValue(slave_infos[textctrl_name]) andrej@2354: self.SyncManagersTable.SetData(slave_infos["sync_managers"]) andrej@2354: self.SyncManagersTable.ResetView(self.SyncManagersGrid) andrej@2354: else: andrej@2354: for textctrl_name in sync_manager_section: andrej@2354: self.TextCtrlDic[textctrl_name].SetValue("") andrej@2354: self.SyncManagersTable.SetData([]) andrej@2354: self.SyncManagersTable.ResetView(self.SyncManagersGrid) andrej@2355: andrej@2354: def OnButtonClick(self, event): andrej@2354: """ andrej@2354: Event handler for slave state transition button click (Init, PreOP, SafeOP, OP button) andrej@2354: @param event : wx.EVT_BUTTON object andrej@2354: """ andrej@2354: check_connect_flag = self.Controler.CommonMethod.CheckConnect(False) andrej@2375: if check_connect_flag: andrej@2354: state_dic = ["INIT", "PREOP", "SAFEOP", "OP"] andrej@2355: andrej@2354: # If target state is one of {INIT, PREOP, SAFEOP}, request slave state transition immediately. andrej@2375: if event.GetId() < 3: andrej@2354: self.Controler.CommonMethod.RequestSlaveState(state_dic[event.GetId()]) andrej@2354: self.TextCtrlDic["TargetState"].SetValue(state_dic[event.GetId()]) andrej@2354: andrej@2354: # If target state is OP, first check "PLC status". andrej@2354: # (1) If current PLC status is "Started", then request slave state transition andrej@2354: # (2) Otherwise, show error message and return andrej@2375: else: andrej@2406: status, _log_count = self.Controler.GetCTRoot()._connector.GetPLCstatus() andrej@2416: if status == PlcStatus.Started: andrej@2354: self.Controler.CommonMethod.RequestSlaveState("OP") andrej@2354: self.TextCtrlDic["TargetState"].SetValue("OP") andrej@2375: else: andrej@2355: self.Controler.CommonMethod.CreateErrorDialog("PLC is Not Started") andrej@2355: andrej@2354: def GetCurrentState(self, event): andrej@2354: """ andrej@2354: Timer event handler for periodic slave state monitoring (Default period: 1 sec = 1000 msec). andrej@2354: @param event : wx.TIMER object andrej@2354: """ andrej@2354: check_connect_flag = self.Controler.CommonMethod.CheckConnect(True) andrej@2354: if check_connect_flag: andrej@2354: returnVal = self.Controler.CommonMethod.GetSlaveStateFromSlave() andrej@2354: line = returnVal.split("\n") andrej@2375: try: andrej@2354: self.SetCurrentState(line[self.Controler.GetSlavePos()]) andrej@2354: except Exception: andrej@2355: pass andrej@2355: andrej@2354: def SetCurrentState(self, line): andrej@2354: """ andrej@2354: Show current slave state using the executiob result of "ethercat slaves" command. andrej@2354: @param line : result of "ethercat slaves" command andrej@2354: """ andrej@2354: state_array = ["INIT", "PREOP", "SAFEOP", "OP"] andrej@2375: try: andrej@2354: # parse the execution result of "ethercat slaves" command andrej@2354: # Result example : 0 0:0 PREOP + EL9800 (V4.30) (PIC24, SPI, ET1100) andrej@2354: token = line.split(" ") andrej@2354: if token[2] in state_array: andrej@2355: self.TextCtrlDic["CurrentState"].SetValue(token[2]) andrej@2354: except Exception: andrej@2355: pass andrej@2355: andrej@2354: def StartTimer(self, event): andrej@2354: """ andrej@2354: Event handler for "Start State Monitoring" button. andrej@2354: - start slave state monitoring thread andrej@2354: @param event : wx.EVT_BUTTON object andrej@2354: """ andrej@2354: self.SlaveStateThread = wx.Timer(self) andrej@2354: # set timer period (1000 ms) andrej@2354: self.SlaveStateThread.Start(1000) andrej@2355: andrej@2354: def CurrentStateThreadStop(self, event): andrej@2354: """ andrej@2354: Event handler for "Stop State Monitoring" button. andrej@2354: - stop slave state monitoring thread andrej@2354: @param event : wx.EVT_BUTTON object andrej@2354: """ andrej@2354: try: andrej@2354: self.SlaveStateThread.Stop() andrej@2354: except Exception: andrej@2354: pass andrej@2355: andrej@2360: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For SDO Management Panel andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class SDOPanelClass(wx.Panel): andrej@2354: def __init__(self, parent, controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent EtherCATManagementTreebook class andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2354: wx.Panel.__init__(self, parent, -1) andrej@2355: andrej@2354: self.DatatypeDescription, self.CommunicationObject, self.ManufacturerSpecific, \ andrej@2384: self.ProfileSpecific, self.Reserved, self.AllSDOData = range(6) andrej@2355: andrej@2354: self.Controler = controler andrej@2355: andrej@2354: self.SDOManagementMainSizer = wx.BoxSizer(wx.VERTICAL) andrej@2354: self.SDOManagementInnerMainSizer = wx.FlexGridSizer(cols=1, hgap=10, rows=2, vgap=10) andrej@2355: andrej@2355: self.SDOUpdate = wx.Button(self, label=_('update')) andrej@2354: self.SDOUpdate.Bind(wx.EVT_BUTTON, self.SDOInfoUpdate) andrej@2355: andrej@2354: self.CallSDONoteBook = SDONoteBook(self, controler=self.Controler) andrej@2354: self.SDOManagementInnerMainSizer.Add(self.SDOUpdate) andrej@2355: self.SDOManagementInnerMainSizer.Add(self.CallSDONoteBook, wx.ALL | wx.EXPAND) andrej@2354: andrej@2354: self.SDOManagementMainSizer.Add(self.SDOManagementInnerMainSizer) andrej@2355: andrej@2354: self.SetSizer(self.SDOManagementMainSizer) andrej@2355: andrej@2354: def SDOInfoUpdate(self, event): andrej@2354: """ andrej@2354: Evenet handler for SDO "update" button. andrej@2355: - Load SDO data from current slave andrej@2354: @param event : wx.EVT_BUTTON object andrej@2355: """ andrej@2354: self.Controler.CommonMethod.SaveSDOData = [] andrej@2354: self.Controler.CommonMethod.ClearSDODataSet() andrej@2354: self.SDOFlag = False andrej@2355: andrej@2354: # Check whether beremiz connected or not. andrej@2354: check_connect_flag = self.Controler.CommonMethod.CheckConnect(False) andrej@2354: if check_connect_flag: andrej@2354: self.SDOs = self.Controler.CommonMethod.GetSlaveSDOFromSlave() andrej@2354: # SDOFlag is "False", user click "Cancel" button andrej@2355: self.SDOFlag = self.SDOParser() andrej@2354: andrej@2375: if self.SDOFlag: andrej@2355: self.CallSDONoteBook.CreateNoteBook() andrej@2354: self.Refresh() andrej@2355: andrej@2355: def SDOParser(self): andrej@2354: """ andrej@2354: Parse SDO data set that obtain "SDOInfoUpdate" Method andrej@2355: @return True or False andrej@2355: """ andrej@2354: andrej@2354: slaveSDO_progress = wx.ProgressDialog("Slave SDO Monitoring", "Now Uploading...", andrej@2381: maximum=len(self.SDOs.splitlines()), andrej@2381: parent=self, andrej@2381: style=wx.PD_CAN_ABORT | wx.PD_APP_MODAL | wx.PD_ELAPSED_TIME | andrej@2381: wx.PD_ESTIMATED_TIME | wx.PD_REMAINING_TIME | andrej@2381: wx.PD_AUTO_HIDE | wx.PD_SMOOTH) andrej@2355: andrej@2354: # If keep_going flag is False, SDOParser method is stop and return "False". andrej@2354: keep_going = True andrej@2354: count = 0 andrej@2355: andrej@2355: # SDO data example andrej@2354: # SDO 0x1000, "Device type" andrej@2354: # 0x1000:00,r-r-r-,uint32,32 bit,"Device type",0x00020192, 131474 andrej@2354: for details_line in self.SDOs.splitlines(): andrej@2354: count += 1 andrej@2354: line_token = details_line.split("\"") andrej@2354: # len(line_token[2]) case : SDO 0x1000, "Device type" andrej@2354: if len(line_token[2]) == 0: andrej@2354: title_name = line_token[1] andrej@2354: # else case : 0x1000:00,r-r-r-,uint32,32 bit,"Device type",0x00020192, 131474 andrej@2375: else: andrej@2354: # line_token = ['0x1000:00,r-r-r-,uint32,32 bit,', 'Device type', ',0x00020192, 131474'] andrej@2354: token_head, name, token_tail = line_token andrej@2355: andrej@2354: # token_head = ['0x1000:00', 'r-r-r-', 'uint32', '32 bit', ''] andrej@2354: token_head = token_head.split(",") andrej@2406: ful_idx, access, type, size, _empty = token_head andrej@2354: # ful_idx.split(":") = ['0x1000', '00'] andrej@2354: idx, sub_idx = ful_idx.split(":") andrej@2355: andrej@2354: # token_tail = ['', '0x00020192', '131474'] andrej@2354: token_tail = token_tail.split(",") andrej@2375: try: andrej@2406: _empty, hex_val, _dec_val = token_tail andrej@2355: andrej@2354: # SDO data is not return "dec value" andrej@2355: # line example : andrej@2355: # 0x1702:01,rwr-r-,uint32,32 bit," 1st mapping", ---- andrej@2354: except Exception: andrej@2406: _empty, hex_val = token_tail andrej@2355: andrej@2354: name_after_check = self.StringTest(name) andrej@2355: andrej@2354: # convert hex type andrej@2354: sub_idx = "0x" + sub_idx andrej@2354: andrej@2354: if type == "octet_string": andrej@2354: hex_val = ' ---- ' andrej@2355: andrej@2354: # SResult of SlaveSDO data parsing. (data type : dictionary) andrej@2363: self.Data = {'idx': idx.strip(), 'subIdx': sub_idx.strip(), 'access': access.strip(), andrej@2363: 'type': type.strip(), 'size': size.strip(), 'name': name_after_check.strip("\""), andrej@2363: 'value': hex_val.strip(), "category": title_name.strip("\"")} andrej@2355: andrej@2354: category_divide_value = [0x1000, 0x2000, 0x6000, 0xa000, 0xffff] andrej@2354: andrej@2375: for count in range(len(category_divide_value)): andrej@2354: if int(idx, 0) < category_divide_value[count]: andrej@2354: self.Controler.CommonMethod.SaveSDOData[count].append(self.Data) andrej@2354: break andrej@2355: andrej@2354: self.Controler.CommonMethod.SaveSDOData[self.AllSDOData].append(self.Data) andrej@2355: andrej@2354: if count >= len(self.SDOs.splitlines()) / 2: andrej@2406: (keep_going, _skip) = slaveSDO_progress.Update(count, "Please waiting a moment!!") andrej@2354: else: andrej@2406: (keep_going, _skip) = slaveSDO_progress.Update(count) andrej@2355: andrej@2355: # If user click "Cancel" loop suspend immediately andrej@2374: if not keep_going: andrej@2354: break andrej@2355: andrej@2355: slaveSDO_progress.Destroy() andrej@2355: return keep_going andrej@2354: andrej@2354: def StringTest(self, check_string): andrej@2354: """ andrej@2355: Test value 'name' is alphanumeric andrej@2355: @param check_string : input data for check andrej@2354: @return result : output data after check andrej@2355: """ andrej@2354: # string.printable is print this result andrej@2356: # '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ andrej@2356: # !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c andrej@2354: allow_range = string.printable andrej@2354: result = check_string andrej@2354: for i in range(0, len(check_string)): andrej@2354: # string.isalnum() is check whether string is alphanumeric or not andrej@2375: if check_string[len(check_string)-1-i:len(check_string)-i] in allow_range: andrej@2354: result = check_string[:len(check_string) - i] andrej@2354: break andrej@2354: return result andrej@2355: andrej@2355: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For SDO Notebook (divide category) andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class SDONoteBook(wx.Notebook): andrej@2354: def __init__(self, parent, controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent SDOPanelClass class andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2366: wx.Notebook.__init__(self, parent, id=-1, size=(850, 500)) andrej@2354: self.Controler = controler andrej@2354: self.parent = parent andrej@2355: andrej@2354: self.CreateNoteBook() andrej@2355: andrej@2355: def CreateNoteBook(self): andrej@2354: """ andrej@2354: Create each NoteBook page, divided SDO index andrej@2354: According to EtherCAT Communication(03/2011), 158p andrej@2355: """ andrej@2354: self.Data = [] andrej@2354: count = 1 andrej@2355: andrej@2354: page_texts = [("all", self.parent.AllSDOData), andrej@2381: ("0x0000 - 0x0ff", self.parent.DatatypeDescription), andrej@2381: ("0x1000 - 0x1fff", self.parent.CommunicationObject), andrej@2381: ("0x2000 - 0x5fff", self.parent.ManufacturerSpecific), andrej@2381: ("0x6000 - 0x9fff", self.parent.ProfileSpecific), andrej@2381: ("0xa000 - 0xffff", self.parent.Reserved)] andrej@2354: andrej@2406: # page_tooltip_string = ["SDO Index 0x0000 - 0x0fff : Data Type Description", andrej@2406: # "SDO Index 0x1000 - 0x1fff : Communication object", andrej@2406: # "SDO Index 0x2000 - 0x5fff : Manufacturer specific", andrej@2406: # "SDO Index 0x6000 - 0x9fff : Profile specific", andrej@2406: # "SDO Index 0xa000 - 0xffff : Reserved", andrej@2406: # "All SDO Object"] andrej@2354: andrej@2354: self.DeleteAllPages() andrej@2355: andrej@2354: for txt, count in page_texts: andrej@2354: self.Data = self.Controler.CommonMethod.SaveSDOData[count] andrej@2355: self.Win = SlaveSDOTable(self, self.Data) andrej@2354: self.AddPage(self.Win, txt) andrej@2355: andrej@2360: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For SDO Grid (fill index, subindex, etc...) andrej@2356: # ------------------------------------------------------------------------------- andrej@2355: class SlaveSDOTable(wx.grid.Grid): andrej@2354: def __init__(self, parent, data): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent SDOPanelClass class andrej@2354: @param data: SDO data after parsing "SDOParser" method andrej@2354: """ andrej@2363: wx.grid.Grid.__init__(self, parent, -1, size=(830, 490), andrej@2367: style=wx.EXPAND | wx.ALIGN_CENTRE_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL) andrej@2355: andrej@2354: self.Controler = parent.Controler andrej@2354: self.parent = parent andrej@2354: self.SDOFlag = True andrej@2375: if data is None: andrej@2354: self.SDOs = [] andrej@2375: else: andrej@2354: self.SDOs = data andrej@2355: andrej@2354: self.CreateGrid(len(self.SDOs), 8) andrej@2355: SDOCellSize = [(0, 65), (1, 65), (2, 50), (3, 55), andrej@2380: (4, 40), (5, 200), (6, 250), (7, 85)] andrej@2355: andrej@2354: for (index, size) in SDOCellSize: andrej@2354: self.SetColSize(index, size) andrej@2355: andrej@2354: self.SetRowLabelSize(0) andrej@2355: andrej@2354: SDOTableLabel = [(0, "Index"), (1, "Subindex"), (2, "Access"), andrej@2354: (3, "Type"), (4, "Size"), (5, "Category"), andrej@2354: (6, "Name"), (7, "Value")] andrej@2355: andrej@2354: for (index, label) in SDOTableLabel: andrej@2354: self.SetColLabelValue(index, label) andrej@2354: self.SetColLabelAlignment(index, wx.ALIGN_CENTRE) andrej@2355: andrej@2354: attr = wx.grid.GridCellAttr() andrej@2354: andrej@2355: # for SDO download andrej@2389: self.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.SDOModifyDialog) andrej@2355: andrej@2355: for i in range(7): andrej@2363: self.SetColAttr(i, attr) andrej@2355: andrej@2354: self.SetColLabelAlignment(wx.ALIGN_CENTER, wx.ALIGN_CENTER) andrej@2355: andrej@2355: self.SetTableValue() andrej@2355: andrej@2354: def SetTableValue(self): andrej@2354: """ andrej@2354: Cell is filled by new parsing data andrej@2354: """ andrej@2354: sdo_list = ['idx', 'subIdx', 'access', 'type', 'size', 'category', 'name', 'value'] andrej@2354: for row_idx in range(len(self.SDOs)): andrej@2355: for col_idx in range(len(self.SDOs[row_idx])): andrej@2354: self.SetCellValue(row_idx, col_idx, self.SDOs[row_idx][sdo_list[col_idx]]) andrej@2354: self.SetReadOnly(row_idx, col_idx, True) andrej@2375: if col_idx < 5: andrej@2354: self.SetCellAlignment(row_idx, col_idx, wx.ALIGN_CENTRE, wx.ALIGN_CENTRE) andrej@2355: andrej@2354: def CheckSDODataAccess(self, row): andrej@2354: """ andrej@2354: CheckSDODataAccess method is checking that access data has "w" andrej@2354: Access field consist 6 char, if mean andrej@2354: rw rw rw andrej@2355: (preop) (safeop) (op) andrej@2355: Example Access field : rwrwrw, rwrw-- andrej@2354: @param row : Selected cell by user andrej@2354: @return Write_flag : If data has "w", flag is true andrej@2354: """ andrej@2354: write_flag = False andrej@2354: check = self.SDOs[row]['access'] andrej@2375: if check[1:2] == 'w': andrej@2354: self.Controler.CommonMethod.Check_PREOP = True andrej@2354: write_flag = True andrej@2375: if check[3:4] == 'w': andrej@2354: self.Controler.CommonMethod.Check_SAFEOP = True andrej@2354: write_flag = True andrej@2375: if check[5:] == 'w': andrej@2354: self.Controler.CommonMethod.Check_OP = True andrej@2354: write_flag = True andrej@2355: andrej@2354: return write_flag andrej@2355: andrej@2354: def DecideSDODownload(self, state): andrej@2354: """ andrej@2355: compare current state and "access" field, andrej@2354: result notify SDOModifyDialog method andrej@2354: @param state : current slave state andrej@2354: @return True or False andrej@2354: """ andrej@2355: # Example of 'state' parameter : "0 0:0 PREOP + EL9800 (V4.30) (PIC24, SPI, ET1100)" andrej@2354: state = state[self.Controler.GetSlavePos()].split(" ")[2] andrej@2375: if state == "PREOP" and self.Controler.CommonMethod.Check_PREOP: andrej@2354: return True andrej@2375: elif state == "SAFEOP" and self.Controler.CommonMethod.Check_SAFEOP: andrej@2354: return True andrej@2375: elif state == "OP" and self.Controler.CommonMethod.Check_OP: andrej@2354: return True andrej@2355: andrej@2354: return False andrej@2355: andrej@2354: def ClearStateFlag(self): andrej@2354: """ andrej@2354: Initialize StateFlag andrej@2354: StateFlag is notice SDOData access each slave state andrej@2354: """ andrej@2354: self.Controler.CommonMethod.Check_PREOP = False andrej@2354: self.Controler.CommonMethod.Check_SAFEOP = False andrej@2354: self.Controler.CommonMethod.Check_OP = False andrej@2355: andrej@2382: def SDOModifyDialog(self, event): andrej@2354: """ andrej@2354: Create dialog for SDO value modify andrej@2355: if user enter data, perform command "ethercat download" andrej@2389: @param event : wx.grid.EVT_GRID_CELL_LEFT_DCLICK object andrej@2354: """ andrej@2354: self.ClearStateFlag() andrej@2355: andrej@2355: # CheckSDODataAccess is checking that OD(Object Dictionary) has "w" andrej@2375: if event.GetCol() == 7 and self.CheckSDODataAccess(event.GetRow()): andrej@2382: dlg = wx.TextEntryDialog( andrej@2382: self, andrej@2382: _("Enter hex or dec value (if enter dec value, it automatically conversed hex value)"), andrej@2382: "SDOModifyDialog", andrej@2382: style=wx.OK | wx.CANCEL) andrej@2354: andrej@2355: start_value = self.GetCellValue(event.GetRow(), event.GetCol()) andrej@2354: dlg.SetValue(start_value) andrej@2355: andrej@2354: if dlg.ShowModal() == wx.ID_OK: andrej@2375: try: andrej@2354: int(dlg.GetValue(), 0) andrej@2354: # check "Access" field andrej@2375: if self.DecideSDODownload(self.Controler.CommonMethod.SlaveState[self.Controler.GetSlavePos()]): andrej@2354: # Request "SDODownload" andrej@2381: self.Controler.CommonMethod.SDODownload( andrej@2381: self.SDOs[event.GetRow()]['type'], andrej@2381: self.SDOs[event.GetRow()]['idx'], andrej@2381: self.SDOs[event.GetRow()]['subIdx'], andrej@2381: dlg.GetValue()) andrej@2381: andrej@2354: self.SetCellValue(event.GetRow(), event.GetCol(), hex(int(dlg.GetValue(), 0))) andrej@2375: else: andrej@2355: self.Controler.CommonMethod.CreateErrorDialog('You cannot SDO download this state') andrej@2354: # Error occured process of "int(variable)" andrej@2354: # User input is not hex, dec value andrej@2354: except ValueError: andrej@2355: self.Controler.CommonMethod.CreateErrorDialog('You can input only hex, dec value') andrej@2354: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For PDO Monitoring Panel andrej@2355: # PDO Class UI : Panel -> Choicebook (RxPDO, TxPDO) -> andrej@2354: # Notebook (PDO Index) -> Grid (PDO entry) andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class PDOPanelClass(wx.Panel): andrej@2354: def __init__(self, parent, controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent EtherCATManagementTreebook class andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2354: wx.Panel.__init__(self, parent, -1) andrej@2354: self.Controler = controler andrej@2354: andrej@2354: self.PDOMonitoringEditorMainSizer = wx.BoxSizer(wx.VERTICAL) andrej@2354: self.PDOMonitoringEditorInnerMainSizer = wx.FlexGridSizer(cols=1, hgap=10, rows=2, vgap=10) andrej@2355: andrej@2355: self.CallPDOChoicebook = PDOChoicebook(self, controler=self.Controler) andrej@2355: self.PDOMonitoringEditorInnerMainSizer.Add(self.CallPDOChoicebook, wx.ALL) andrej@2355: andrej@2354: self.PDOMonitoringEditorMainSizer.Add(self.PDOMonitoringEditorInnerMainSizer) andrej@2355: andrej@2354: self.SetSizer(self.PDOMonitoringEditorMainSizer) andrej@2354: andrej@2354: def PDOInfoUpdate(self): andrej@2354: """ andrej@2354: Call RequestPDOInfo method and create Choicebook andrej@2354: """ andrej@2354: self.Controler.CommonMethod.RequestPDOInfo() andrej@2354: self.CallPDOChoicebook.Destroy() andrej@2354: self.CallPDOChoicebook = PDOChoicebook(self, controler=self.Controler) andrej@2354: self.Refresh() andrej@2354: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For PDO Choicebook (divide Tx, Rx PDO) andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class PDOChoicebook(wx.Choicebook): andrej@2354: def __init__(self, parent, controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent PDOPanelClass class andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2354: wx.Choicebook.__init__(self, parent, id=-1, size=(500, 500), style=wx.CHB_DEFAULT) andrej@2354: self.Controler = controler andrej@2355: andrej@2354: RxWin = PDONoteBook(self, controler=self.Controler, name="Rx") andrej@2354: TxWin = PDONoteBook(self, controler=self.Controler, name="Tx") andrej@2354: self.AddPage(RxWin, "RxPDO") andrej@2354: self.AddPage(TxWin, "TxPDO") andrej@2355: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For PDO Notebook (divide PDO index) andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class PDONoteBook(wx.Notebook): andrej@2354: def __init__(self, parent, name, controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent PDOChoicebook class andrej@2354: @param name: identifier whether RxPDO or TxPDO andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2354: wx.Notebook.__init__(self, parent, id=-1, size=(640, 400)) andrej@2354: self.Controler = controler andrej@2355: andrej@2354: count = 0 andrej@2354: page_texts = [] andrej@2355: andrej@2354: self.Controler.CommonMethod.RequestPDOInfo() andrej@2355: andrej@2375: if name == "Tx": andrej@2354: # obtain pdo_info and pdo_entry andrej@2354: # pdo_info include (PDO index, name, number of entry) andrej@2373: pdo_info = self.Controler.CommonMethod.GetTxPDOCategory() andrej@2354: pdo_entry = self.Controler.CommonMethod.GetTxPDOInfo() andrej@2375: for tmp in pdo_info: andrej@2354: title = str(hex(tmp['pdo_index'])) andrej@2354: page_texts.append(title) andrej@2354: # RX PDO case andrej@2375: else: andrej@2354: pdo_info = self.Controler.CommonMethod.GetRxPDOCategory() andrej@2354: pdo_entry = self.Controler.CommonMethod.GetRxPDOInfo() andrej@2375: for tmp in pdo_info: andrej@2354: title = str(hex(tmp['pdo_index'])) andrej@2354: page_texts.append(title) andrej@2355: andrej@2354: # Add page depending on the number of pdo_info andrej@2354: for txt in page_texts: andrej@2354: win = PDOEntryTable(self, pdo_info, pdo_entry, count) andrej@2354: self.AddPage(win, txt) andrej@2355: count += 1 andrej@2354: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For PDO Grid (fill entry index, subindex etc...) andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class PDOEntryTable(wx.grid.Grid): andrej@2354: def __init__(self, parent, info, entry, count): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent PDONoteBook class andrej@2354: @param info : data structure including entry index, sub index, name, length, type andrej@2354: @param entry : data structure including index, name, entry number andrej@2354: @param count : page number andrej@2354: """ andrej@2363: wx.grid.Grid.__init__(self, parent, -1, size=(500, 400), pos=wx.Point(0, 0), andrej@2367: style=wx.EXPAND | wx.ALIGN_CENTRE_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL) andrej@2355: andrej@2354: self.Controler = parent.Controler andrej@2355: andrej@2354: self.PDOInfo = info andrej@2354: self.PDOEntry = entry andrej@2354: self.Count = count andrej@2355: andrej@2354: self.CreateGrid(self.PDOInfo[self.Count]['number_of_entry'], 5) andrej@2355: self.SetColLabelSize(25) andrej@2354: self.SetRowLabelSize(0) andrej@2355: andrej@2354: PDOTableLabel = [(0, "Index"), (1, "Subindex"), (2, "Length"), andrej@2354: (3, "Type"), (4, "Name")] andrej@2355: andrej@2354: for (index, label) in PDOTableLabel: andrej@2354: self.SetColLabelValue(index, label) andrej@2355: andrej@2354: PDOCellSize = [(0, 45), (1, 65), (2, 55), (3, 40), (4, 300)] andrej@2355: andrej@2354: for (index, size) in PDOCellSize: andrej@2354: self.SetColSize(index, size) andrej@2354: self.SetColLabelAlignment(index, wx.ALIGN_LEFT) andrej@2355: andrej@2354: attr = wx.grid.GridCellAttr() andrej@2355: andrej@2354: for i in range(5): andrej@2354: self.SetColAttr(i, attr) andrej@2355: andrej@2354: self.SetTableValue() andrej@2355: andrej@2354: def SetTableValue(self): andrej@2354: """ andrej@2354: Cell is filled by new parsing data in XML andrej@2354: """ andrej@2354: list_index = 0 andrej@2354: # number of entry andrej@2375: for i in range(self.Count + 1): andrej@2354: list_index += self.PDOInfo[i]['number_of_entry'] andrej@2354: andrej@2354: start_value = list_index - self.PDOInfo[self.Count]['number_of_entry'] andrej@2355: andrej@2354: pdo_list = ['entry_index', 'subindex', 'bitlen', 'type', 'name'] andrej@2354: for row_idx in range(self.PDOInfo[self.Count]['number_of_entry']): andrej@2354: for col_idx in range(len(self.PDOEntry[row_idx])): andrej@2354: # entry index is converted hex value. andrej@2375: if col_idx == 0: andrej@2354: self.SetCellValue(row_idx, col_idx, hex(self.PDOEntry[start_value][pdo_list[col_idx]])) andrej@2375: else: andrej@2354: self.SetCellValue(row_idx, col_idx, str(self.PDOEntry[start_value][pdo_list[col_idx]])) andrej@2375: if col_idx != 4: andrej@2354: self.SetCellAlignment(row_idx, col_idx, wx.ALIGN_CENTRE, wx.ALIGN_CENTRE) andrej@2375: else: andrej@2354: self.SetCellAlignment(row_idx, col_idx, wx.ALIGN_LEFT, wx.ALIGN_CENTRE) andrej@2354: self.SetReadOnly(row_idx, col_idx, True) andrej@2354: self.SetRowSize(row_idx, 25) andrej@2354: start_value += 1 andrej@2354: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2355: # For EEPROM Access Main Panel andrej@2354: # (This class explain EEPROM Access) andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class EEPROMAccessPanel(wx.Panel): andrej@2354: def __init__(self, parent, controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent EtherCATManagementTreebook class andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2354: wx.Panel.__init__(self, parent, -1) andrej@2363: sizer = wx.FlexGridSizer(cols=1, hgap=20, rows=3, vgap=20) andrej@2355: andrej@2354: line = wx.StaticText(self, -1, "\n EEPROM Access is composed to SmartView and HexView. \ andrej@2354: \n\n - SmartView shows Config Data, Device Identity, Mailbox settings, etc. \ andrej@2354: \n\n - HexView shows EEPROM's contents.") andrej@2355: andrej@2354: sizer.Add(line) andrej@2355: andrej@2354: self.SetSizer(sizer) andrej@2354: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2355: # For Smart View Panel andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class SlaveSiiSmartView(wx.Panel): andrej@2354: def __init__(self, parent, controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent EtherCATManagementTreebook class andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2354: wx.Panel.__init__(self, parent, -1) andrej@2354: self.parent = parent andrej@2354: self.Controler = controler andrej@2355: andrej@2363: self.PDIType = { andrej@2375: 0: ['none', '00000000'], andrej@2375: 4: ['Digital I/O', '00000100'], andrej@2375: 5: ['SPI Slave', '00000101'], andrej@2375: 7: ['EtherCAT Bridge (port3)', '00000111'], andrej@2375: 8: ['uC async. 16bit', '00001000'], andrej@2375: 9: ['uC async. 8bit', '00001001'], andrej@2375: 10: ['uC sync. 16bit', '00001010'], andrej@2375: 11: ['uC sync. 8bit', '00001011'], andrej@2375: 16: ['32 Digtal Input and 0 Digital Output', '00010000'], andrej@2375: 17: ['24 Digtal Input and 8 Digital Output', '00010001'], andrej@2375: 18: ['16 Digtal Input and 16 Digital Output', '00010010'], andrej@2375: 19: ['8 Digtal Input and 24 Digital Output', '00010011'], andrej@2375: 20: ['0 Digtal Input and 32 Digital Output', '00010100'], andrej@2363: 128: ['On-chip bus', '11111111'] andrej@2363: } andrej@2355: andrej@2354: sizer = wx.FlexGridSizer(cols=1, hgap=5, rows=2, vgap=5) andrej@2354: button_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=1, vgap=5) andrej@2354: andrej@2354: for button, mapping_method in [("Write EEPROM", self.WriteToEEPROM), andrej@2354: ("Read EEPROM", self.ReadFromEEPROM)]: andrej@2354: btn = wx.Button(self, -1, button, size=(150, 40)) andrej@2354: button_sizer.Add(btn, border=10, flag=wx.ALL) andrej@2354: btn.Bind(wx.EVT_BUTTON, mapping_method) andrej@2355: andrej@2354: self.TreeListCtrl = SmartViewTreeListCtrl(self, self.Controler) andrej@2355: andrej@2354: sizer.Add(button_sizer, border=10, flag=wx.ALL) andrej@2354: sizer.Add(self.TreeListCtrl, border=10, flag=wx.ALL) andrej@2354: self.SetSizer(sizer) andrej@2355: andrej@2354: self.Create_SmartView() andrej@2355: andrej@2354: def Create_SmartView(self): andrej@2354: """ andrej@2354: SmartView shows information based on XML as initial value. andrej@2355: """ andrej@2354: self.Controler.CommonMethod.SmartViewInfosFromXML = self.Controler.CommonMethod.GetSmartViewInfos() andrej@2354: self.SetXMLData() andrej@2355: andrej@2354: def WriteToEEPROM(self, event): andrej@2354: """ andrej@2354: Open binary file (user select) and write the selected binary data to EEPROM andrej@2354: @param event : wx.EVT_BUTTON object andrej@2355: """ andrej@2354: # Check whether beremiz connected or not, and whether status is "Started" or not. andrej@2354: check_connect_flag = self.Controler.CommonMethod.CheckConnect(False) andrej@2354: if check_connect_flag: andrej@2406: status, _log_count = self.Controler.GetCTRoot()._connector.GetPLCstatus() andrej@2416: if status is not PlcStatus.Started: andrej@2354: dialog = wx.FileDialog(self, _("Choose a binary file"), os.getcwd(), "", _("bin files (*.bin)|*.bin"), wx.OPEN) andrej@2355: andrej@2354: if dialog.ShowModal() == wx.ID_OK: andrej@2354: filepath = dialog.GetPath() andrej@2354: try: andrej@2363: binfile = open(filepath, "rb") andrej@2354: self.SiiBinary = binfile.read() andrej@2354: dialog.Destroy() andrej@2355: andrej@2354: self.Controler.CommonMethod.SiiWrite(self.SiiBinary) andrej@2354: # refresh data structure kept by master andrej@2354: self.Controler.CommonMethod.Rescan() andrej@2355: # save binary data as inner global data of beremiz andrej@2354: # for fast loading when slave plugin node is reopened. andrej@2354: self.Controler.CommonMethod.SiiData = self.SiiBinary andrej@2354: self.SetEEPROMData() andrej@2354: except Exception: andrej@2354: self.Controler.CommonMethod.CreateErrorDialog('The file does not exist!') andrej@2354: dialog.Destroy() andrej@2355: andrej@2354: def ReadFromEEPROM(self, event): andrej@2354: """ andrej@2354: Refresh displayed data based on slave EEPROM and save binary file through dialog andrej@2354: @param event : wx.EVT_BUTTON object andrej@2355: """ andrej@2354: # Check whether beremiz connected or not. andrej@2354: check_connect_flag = self.Controler.CommonMethod.CheckConnect(False) andrej@2354: if check_connect_flag: andrej@2354: self.SiiBinary = self.Controler.CommonMethod.LoadData() andrej@2354: self.SetEEPROMData() andrej@2355: dialog = wx.FileDialog(self, _("Save as..."), os.getcwd(), andrej@2355: "slave0.bin", _("bin files (*.bin)|*.bin|All files|*.*"), andrej@2367: wx.SAVE | wx.OVERWRITE_PROMPT) andrej@2355: andrej@2354: if dialog.ShowModal() == wx.ID_OK: andrej@2354: filepath = dialog.GetPath() andrej@2363: binfile = open(filepath, "wb") andrej@2354: binfile.write(self.SiiBinary) andrej@2354: binfile.close() andrej@2355: andrej@2354: dialog.Destroy() andrej@2355: andrej@2354: def SetXMLData(self): andrej@2354: """ andrej@2354: Set data based on XML initially andrej@2355: """ andrej@2354: # Config Data: EEPROM Size, PDI Type, Device Emulation andrej@2354: # Find PDI Type in pdiType dictionary andrej@2354: cnt_pdi_type = self.Controler.CommonMethod.SmartViewInfosFromXML["pdi_type"] andrej@2354: for i in self.PDIType.keys(): andrej@2354: if cnt_pdi_type == i: andrej@2354: cnt_pdi_type = self.PDIType[i][0] andrej@2354: break andrej@2354: # Set Config Data andrej@2355: for treelist, data in [("EEPROM Size (Bytes)", andrej@2354: str(self.Controler.CommonMethod.SmartViewInfosFromXML["eeprom_size"])), andrej@2355: ("PDI Type", andrej@2354: cnt_pdi_type), andrej@2355: ("Device Emulation", andrej@2354: self.Controler.CommonMethod.SmartViewInfosFromXML["device_emulation"])]: andrej@2354: self.TreeListCtrl.Tree.SetItemText(self.TreeListCtrl.ConfigData[treelist], data, 1) andrej@2355: andrej@2354: # Device Identity: Vendor ID, Product Code, Revision No., Serial No. andrej@2354: # Set Device Identity andrej@2354: for treelist, data in [("Vendor ID", self.Controler.CommonMethod.SmartViewInfosFromXML["vendor_id"]), andrej@2354: ("Product Code", self.Controler.CommonMethod.SmartViewInfosFromXML["product_code"]), andrej@2354: ("Revision No.", self.Controler.CommonMethod.SmartViewInfosFromXML["revision_no"]), andrej@2354: ("Serial No.", self.Controler.CommonMethod.SmartViewInfosFromXML["serial_no"])]: andrej@2354: self.TreeListCtrl.Tree.SetItemText(self.TreeListCtrl.DeviceIdentity[treelist], data, 1) andrej@2355: andrej@2354: # Mailbox: Supported Mailbox, Bootstrap Configuration, Standard Configuration andrej@2354: # Set Mailbox andrej@2354: for treelist, data in [("Supported Mailbox", self.Controler.CommonMethod.SmartViewInfosFromXML["supported_mailbox"]), andrej@2354: ("Bootstrap Configuration", ""), andrej@2354: ("Standard Configuration", "")]: andrej@2355: self.TreeListCtrl.Tree.SetItemText(self.TreeListCtrl.Mailbox[treelist], data, 1) andrej@2354: # Set Bootstrap Configuration: Receive Offset, Receive Size, Send Offset, Send Size andrej@2354: for treelist, data in [("Receive Offset", self.Controler.CommonMethod.SmartViewInfosFromXML["mailbox_bootstrapconf_outstart"]), andrej@2354: ("Receive Size", self.Controler.CommonMethod.SmartViewInfosFromXML["mailbox_bootstrapconf_outlength"]), andrej@2354: ("Send Offset", self.Controler.CommonMethod.SmartViewInfosFromXML["mailbox_bootstrapconf_instart"]), andrej@2354: ("Send Size", self.Controler.CommonMethod.SmartViewInfosFromXML["mailbox_bootstrapconf_inlength"])]: andrej@2355: self.TreeListCtrl.Tree.SetItemText(self.TreeListCtrl.BootstrapConfig[treelist], data, 1) andrej@2354: # Set Standard Configuration: Receive Offset, Receive Size, Send Offset, Send Size andrej@2354: for treelist, data in [("Receive Offset", self.Controler.CommonMethod.SmartViewInfosFromXML["mailbox_standardconf_outstart"]), andrej@2354: ("Receive Size", self.Controler.CommonMethod.SmartViewInfosFromXML["mailbox_standardconf_outlength"]), andrej@2354: ("Send Offset", self.Controler.CommonMethod.SmartViewInfosFromXML["mailbox_standardconf_instart"]), andrej@2354: ("Send Size", self.Controler.CommonMethod.SmartViewInfosFromXML["mailbox_standardconf_inlength"])]: andrej@2354: self.TreeListCtrl.Tree.SetItemText(self.TreeListCtrl.StandardConfig[treelist], data, 1) andrej@2355: andrej@2354: def SetEEPROMData(self): andrej@2354: """ andrej@2354: Set data based on slave EEPROM. andrej@2355: """ andrej@2354: # sii_dict = { Parameter : (WordAddress, WordSize) } andrej@2365: sii_dict = { andrej@2375: 'PDIControl': ('0', 1), andrej@2375: 'PDIConfiguration': ('1', 1), andrej@2375: 'PulseLengthOfSYNCSignals': ('2', 1), andrej@2375: 'ExtendedPDIConfiguration': ('3', 1), andrej@2375: 'ConfiguredStationAlias': ('4', 1), andrej@2375: 'Checksum': ('7', 1), andrej@2375: 'VendorID': ('8', 2), andrej@2375: 'ProductCode': ('a', 2), andrej@2375: 'RevisionNumber': ('c', 2), andrej@2375: 'SerialNumber': ('e', 2), andrej@2375: 'Execution Delay': ('10', 1), andrej@2375: 'Port0Delay': ('11', 1), andrej@2375: 'Port1Delay': ('12', 1), andrej@2375: 'BootstrapReceiveMailboxOffset': ('14', 1), andrej@2375: 'BootstrapReceiveMailboxSize': ('15', 1), andrej@2375: 'BootstrapSendMailboxOffset': ('16', 1), andrej@2375: 'BootstrapSendMailboxSize': ('17', 1), andrej@2375: 'StandardReceiveMailboxOffset': ('18', 1), andrej@2375: 'StandardReceiveMailboxSize': ('19', 1), andrej@2375: 'StandardSendMailboxOffset': ('1a', 1), andrej@2375: 'StandardSendMailboxSize': ('1b', 1), andrej@2375: 'MailboxProtocol': ('1c', 1), andrej@2375: 'Size': ('3e', 1), andrej@2375: 'Version': ('3f', 1), andrej@2375: 'First Category Type/Vendor Specific': ('40', 1), andrej@2375: 'Following Category Word Size': ('41', 1), andrej@2375: 'Category Data': ('42', 1), andrej@2365: } andrej@2355: andrej@2354: # Config Data: EEPROM Size, PDI Type, Device Emulation andrej@2354: # EEPROM's data in address '0x003f' is Size of EEPROM in KBit-1 andrej@2369: eeprom_size = str((int(self.GetWordAddressData(sii_dict.get('Size'), 10))+1)/8*1024) andrej@2354: # Find PDI Type in pdiType dictionary andrej@2369: cnt_pdi_type = int(self.GetWordAddressData(sii_dict.get('PDIControl'), 16).split('x')[1][2:4], 16) andrej@2354: for i in self.PDIType.keys(): andrej@2354: if cnt_pdi_type == i: andrej@2354: cnt_pdi_type = self.PDIType[i][0] andrej@2354: break andrej@2354: # Get Device Emulation andrej@2369: device_emulation = str(bool(int("{:0>16b}".format(int(self.GetWordAddressData(sii_dict.get('PDIControl'), 16), 16))[7]))) andrej@2354: # Set Config Data andrej@2354: for treelist, data in [("EEPROM Size (Bytes)", eeprom_size), andrej@2354: ("PDI Type", cnt_pdi_type), andrej@2354: ("Device Emulation", device_emulation)]: andrej@2354: self.TreeListCtrl.Tree.SetItemText(self.TreeListCtrl.ConfigData[treelist], data, 1) andrej@2355: andrej@2354: # Device Identity: Vendor ID, Product Code, Revision No., Serial No. andrej@2354: # Set Device Identity andrej@2363: for treelist, data in [ andrej@2369: ("Vendor ID", self.GetWordAddressData(sii_dict.get('VendorID'), 16)), andrej@2369: ("Product Code", self.GetWordAddressData(sii_dict.get('ProductCode'), 16)), andrej@2369: ("Revision No.", self.GetWordAddressData(sii_dict.get('RevisionNumber'), 16)), andrej@2369: ("Serial No.", self.GetWordAddressData(sii_dict.get('SerialNumber'), 16))]: andrej@2354: self.TreeListCtrl.Tree.SetItemText(self.TreeListCtrl.DeviceIdentity[treelist], data, 1) andrej@2355: andrej@2354: # Mailbox andrej@2354: # EEORPOM's word address '1c' indicates supported mailbox protocol. andrej@2355: # each value of mailbox protocol : andrej@2354: # VoE(0x0020), SoE(0x0010), FoE(0x0008), CoE(0x0004), EoE(0x0002), AoE(0x0001) andrej@2354: supported_mailbox = "" andrej@2365: mailbox_protocol = ["VoE, ", "SoE, ", "FoE, ", "CoE, ", "EoE, ", "AoE, "] andrej@2369: mailbox_data = "{:0>8b}".format(int(self.GetWordAddressData(sii_dict.get('MailboxProtocol'), 16), 16)) andrej@2354: for protocol in range(6): andrej@2354: if mailbox_data[protocol+2] == '1': andrej@2354: supported_mailbox += mailbox_protocol[protocol] andrej@2354: supported_mailbox = supported_mailbox.strip(", ") andrej@2354: # Set Mailbox andrej@2354: for treelist, data in [("Supported Mailbox", supported_mailbox), andrej@2354: ("Bootstrap Configuration", ""), andrej@2354: ("Standard Configuration", "")]: andrej@2355: self.TreeListCtrl.Tree.SetItemText(self.TreeListCtrl.Mailbox[treelist], data, 1) andrej@2354: # Set Bootstrap Configuration: Receive Offset, Receive Size, Send Offset, Send Size andrej@2363: for treelist, data in [ andrej@2369: ("Receive Offset", self.GetWordAddressData(sii_dict.get('BootstrapReceiveMailboxOffset'), 10)), andrej@2369: ("Receive Size", self.GetWordAddressData(sii_dict.get('BootstrapReceiveMailboxSize'), 10)), andrej@2369: ("Send Offset", self.GetWordAddressData(sii_dict.get('BootstrapSendMailboxOffset'), 10)), andrej@2369: ("Send Size", self.GetWordAddressData(sii_dict.get('BootstrapSendMailboxSize'), 10))]: andrej@2355: self.TreeListCtrl.Tree.SetItemText(self.TreeListCtrl.BootstrapConfig[treelist], data, 1) andrej@2354: # Set Standard Configuration: Receive Offset, Receive Size, Send Offset, Send Size andrej@2363: for treelist, data in [ andrej@2369: ("Receive Offset", self.GetWordAddressData(sii_dict.get('StandardReceiveMailboxOffset'), 10)), andrej@2369: ("Receive Size", self.GetWordAddressData(sii_dict.get('StandardReceiveMailboxSize'), 10)), andrej@2369: ("Send Offset", self.GetWordAddressData(sii_dict.get('StandardSendMailboxOffset'), 10)), andrej@2369: ("Send Size", self.GetWordAddressData(sii_dict.get('StandardSendMailboxSize'), 10))]: andrej@2355: self.TreeListCtrl.Tree.SetItemText(self.TreeListCtrl.StandardConfig[treelist], data, 1) andrej@2355: andrej@2354: def MakeStaticBoxSizer(self, boxlabel): andrej@2354: """ andrej@2354: Make StaticBoxSizer andrej@2354: @param boxlabel : label of box sizer andrej@2354: @return sizer : the StaticBoxSizer labeled 'boxlabel' andrej@2354: """ andrej@2354: box = wx.StaticBox(self, -1, boxlabel) andrej@2354: sizer = wx.StaticBoxSizer(box, wx.VERTICAL) andrej@2354: andrej@2354: return sizer andrej@2355: andrej@2354: def GetWordAddressData(self, dict_tuple, format): andrej@2354: """ andrej@2354: This method converts word address data from EEPROM binary. andrej@2354: @param dict_tuple : element of 'sii_dict' dictionary in SetEEPROMData() andrej@2354: @param format : format of data. It can be 16(hex), 10(decimal) and 2(binary). andrej@2354: @return formatted value andrej@2355: """ andrej@2354: offset = int(str(dict_tuple[0]), 16) * 2 andrej@2354: length = int(str(dict_tuple[1]), 16) * 2 andrej@2354: list = [] andrej@2354: data = '' andrej@2354: for index in range(length): andrej@2354: hexdata = hex(ord(self.SiiBinary[offset + index]))[2:] andrej@2354: list.append(hexdata.zfill(2)) andrej@2355: andrej@2354: list.reverse() andrej@2354: data = list[0:length] andrej@2354: andrej@2354: if format == 16: andrej@2355: return '0x' + ''.join(data) andrej@2354: elif format == 10: andrej@2354: return str(int(str(''.join(data)), 16)) andrej@2355: elif format == 2: andrej@2355: ''.join(data) andrej@2354: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For Smart View TreeListCtrl andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class SmartViewTreeListCtrl(wx.Panel): andrej@2354: def __init__(self, parent, Controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent SlaveSiiSmartView class andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2354: andrej@2354: wx.Panel.__init__(self, parent, -1, size=(350, 500)) andrej@2355: andrej@2355: self.Tree = wx.gizmos.TreeListCtrl(self, -1, size=(350, 500), andrej@2386: style=(wx.TR_DEFAULT_STYLE | andrej@2386: wx.TR_FULL_ROW_HIGHLIGHT | andrej@2386: wx.TR_HIDE_ROOT | andrej@2386: wx.TR_COLUMN_LINES | andrej@2386: wx.TR_ROW_LINES)) andrej@2355: andrej@2354: self.Tree.AddColumn("Description", width=200) andrej@2354: self.Tree.AddColumn("Value", width=140) andrej@2354: self.Tree.SetMainColumn(0) andrej@2355: andrej@2354: self.Root = self.Tree.AddRoot("") andrej@2355: andrej@2354: # Add item andrej@2354: # Level 1 nodes andrej@2354: self.Level1Nodes = {} andrej@2354: for lv1 in ["Config Data", "Device Identity", "Mailbox"]: andrej@2354: self.Level1Nodes[lv1] = self.Tree.AppendItem(self.Root, lv1) andrej@2355: andrej@2354: # Level 2 nodes andrej@2354: # Config Data andrej@2354: self.ConfigData = {} andrej@2354: for lv2 in ["EEPROM Size (Bytes)", "PDI Type", "Device Emulation"]: andrej@2354: self.ConfigData[lv2] = self.Tree.AppendItem(self.Level1Nodes["Config Data"], lv2) andrej@2354: # Device Identity andrej@2354: self.DeviceIdentity = {} andrej@2354: for lv2 in ["Vendor ID", "Product Code", "Revision No.", "Serial No."]: andrej@2354: self.DeviceIdentity[lv2] = self.Tree.AppendItem(self.Level1Nodes["Device Identity"], lv2) andrej@2354: # Mailbox andrej@2354: self.Mailbox = {} andrej@2354: for lv2 in ["Supported Mailbox", "Bootstrap Configuration", "Standard Configuration"]: andrej@2354: self.Mailbox[lv2] = self.Tree.AppendItem(self.Level1Nodes["Mailbox"], lv2) andrej@2355: andrej@2354: # Level 3 nodes andrej@2354: # Children of Bootstrap Configuration andrej@2354: self.BootstrapConfig = {} andrej@2354: for lv3 in ["Receive Offset", "Receive Size", "Send Offset", "Send Size"]: andrej@2354: self.BootstrapConfig[lv3] = self.Tree.AppendItem(self.Mailbox["Bootstrap Configuration"], lv3) andrej@2354: # Children of Standard Configuration andrej@2354: self.StandardConfig = {} andrej@2354: for lv3 in ["Receive Offset", "Receive Size", "Send Offset", "Send Size"]: andrej@2354: self.StandardConfig[lv3] = self.Tree.AppendItem(self.Mailbox["Standard Configuration"], lv3) andrej@2355: andrej@2354: # Expand Tree andrej@2355: for tree in [self.Root, andrej@2355: self.Level1Nodes["Config Data"], andrej@2355: self.Level1Nodes["Device Identity"], andrej@2354: self.Level1Nodes["Mailbox"], andrej@2355: self.Mailbox["Bootstrap Configuration"], andrej@2354: self.Mailbox["Standard Configuration"]]: andrej@2354: self.Tree.Expand(tree) andrej@2354: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For Hex View Panel andrej@2354: # shows EEPROM binary as hex data and characters. andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class HexView(wx.Panel): andrej@2354: def __init__(self, parent, controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent EtherCATManagementTreebook class andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2354: wx.Panel.__init__(self, parent, -1) andrej@2354: self.parent = parent andrej@2354: self.Controler = controler andrej@2355: andrej@2354: self.HexRow = 8 andrej@2354: self.HexCol = 17 andrej@2355: andrej@2375: self.HexViewSizer = {"view": wx.FlexGridSizer(cols=1, hgap=10, rows=2, vgap=10), andrej@2375: "siiButton": wx.BoxSizer()} andrej@2354: self.HexViewButton = {} andrej@2354: andrej@2381: for key, evt_handler in [ andrej@2381: ("Sii Upload", self.OnButtonSiiUpload), andrej@2381: ("Sii Download", self.OnButtonSiiDownload), andrej@2381: ("Write to File", self.OnButtonWriteToBinFile), andrej@2381: ("Read from File", self.OnButtonReadFromBinFile), andrej@2381: ("XML to EEPROM Image", self.OnButtonXmlToEEPROMImg)]: andrej@2355: self.HexViewButton[key] = wx.Button(self, -1, key) andrej@2354: self.HexViewButton[key].Bind(wx.EVT_BUTTON, evt_handler) andrej@2354: self.HexViewSizer["siiButton"].Add(self.HexViewButton[key]) andrej@2354: andrej@2354: self.SiiBinary = self.Controler.CommonMethod.XmlToEeprom() andrej@2354: self.HexCode, self.HexRow, self.HexCol = self.Controler.CommonMethod.HexRead(self.SiiBinary) andrej@2354: self.SiiGrid = SiiGridTable(self, self.Controler, self.HexRow, self.HexCol) andrej@2355: self.HexViewSizer["view"].AddMany([self.HexViewSizer["siiButton"], self.SiiGrid]) andrej@2354: self.SiiGrid.CreateGrid(self.HexRow, self.HexCol) andrej@2355: self.SetSizer(self.HexViewSizer["view"]) andrej@2354: self.HexViewSizer["view"].FitInside(self.parent.parent) andrej@2354: self.parent.parent.FitInside() andrej@2354: self.SiiGrid.SetValue(self.HexCode) andrej@2354: self.SiiGrid.Update() andrej@2354: andrej@2354: def UpdateSiiGridTable(self, row, col): andrej@2354: """ andrej@2354: Destroy existing grid and recreate andrej@2354: @param row, col : Hex View grid size andrej@2355: """ andrej@2354: self.HexViewSizer["view"].Detach(self.SiiGrid) andrej@2354: self.SiiGrid.Destroy() andrej@2354: self.SiiGrid = SiiGridTable(self, self.Controler, row, col) andrej@2354: self.HexViewSizer["view"].Add(self.SiiGrid) andrej@2354: self.SiiGrid.CreateGrid(row, col) andrej@2354: self.SetSizer(self.HexViewSizer["view"]) andrej@2354: self.HexViewSizer["view"].FitInside(self.parent.parent) andrej@2354: self.parent.parent.FitInside() andrej@2354: andrej@2354: def OnButtonSiiUpload(self, event): andrej@2354: """ andrej@2354: Load EEPROM data from slave and refresh Hex View grid andrej@2354: Binded to 'Sii Upload' button. andrej@2354: @param event : wx.EVT_BUTTON object andrej@2355: """ andrej@2354: # Check whether beremiz connected or not. andrej@2354: check_connect_flag = self.Controler.CommonMethod.CheckConnect(False) andrej@2354: if check_connect_flag: andrej@2354: # load from EEPROM data and parsing andrej@2354: self.SiiBinary = self.Controler.CommonMethod.LoadData() andrej@2354: self.HexCode, self.HexRow, self.HexCol = self.Controler.CommonMethod.HexRead(self.SiiBinary) andrej@2354: self.UpdateSiiGridTable(self.HexRow, self.HexCol) andrej@2354: self.SiiGrid.SetValue(self.HexCode) andrej@2354: self.SiiGrid.Update() andrej@2355: andrej@2354: def OnButtonSiiDownload(self, event): andrej@2354: """ andrej@2355: Write current EEPROM data to slave and refresh data structure kept by master andrej@2354: Binded to 'Sii Download' button. andrej@2354: @param event : wx.EVT_BUTTON object andrej@2355: """ andrej@2355: # Check whether beremiz connected or not, andrej@2355: # and whether status is "Started" or not. andrej@2354: check_connect_flag = self.Controler.CommonMethod.CheckConnect(False) andrej@2354: if check_connect_flag: andrej@2406: status, _log_count = self.Controler.GetCTRoot()._connector.GetPLCstatus() andrej@2416: if status is not PlcStatus.Started: andrej@2354: self.Controler.CommonMethod.SiiWrite(self.SiiBinary) andrej@2354: self.Controler.CommonMethod.Rescan() andrej@2355: andrej@2354: def OnButtonWriteToBinFile(self, event): andrej@2355: """ andrej@2354: Save current EEPROM data to binary file through FileDialog andrej@2354: Binded to 'Write to File' button. andrej@2354: @param event : wx.EVT_BUTTON object andrej@2355: """ andrej@2355: dialog = wx.FileDialog(self, _("Save as..."), os.getcwd(), "slave0.bin", andrej@2367: _("bin files (*.bin)|*.bin|All files|*.*"), wx.SAVE | wx.OVERWRITE_PROMPT) andrej@2354: andrej@2354: if dialog.ShowModal() == wx.ID_OK: andrej@2354: filepath = dialog.GetPath() andrej@2363: binfile = open(filepath, "wb") andrej@2354: binfile.write(self.SiiBinary) andrej@2354: binfile.close() andrej@2355: andrej@2355: dialog.Destroy() andrej@2355: andrej@2354: def OnButtonReadFromBinFile(self, event): andrej@2354: """ andrej@2354: Load binary file through FileDialog andrej@2354: Binded to 'Read from File' button. andrej@2354: @param event : wx.EVT_BUTTON object andrej@2354: """ andrej@2355: dialog = wx.FileDialog(self, _("Choose a binary file"), os.getcwd(), "", andrej@2354: _("bin files (*.bin)|*.bin"), wx.OPEN) andrej@2355: andrej@2354: if dialog.ShowModal() == wx.ID_OK: andrej@2354: filepath = dialog.GetPath() andrej@2355: andrej@2354: try: andrej@2354: binfile = open(filepath, "rb") andrej@2354: self.SiiBinary = binfile.read() andrej@2354: self.HexCode, self.HexRow, self.HexCol = self.Controler.CommonMethod.HexRead(self.SiiBinary) andrej@2355: self.UpdateSiiGridTable(self.HexRow, self.HexCol) andrej@2354: self.SiiGrid.SetValue(self.HexCode) andrej@2354: self.SiiGrid.Update() andrej@2354: except Exception: andrej@2354: self.Controler.CommonMethod.CreateErrorDialog('The file does not exist!') andrej@2355: andrej@2354: dialog.Destroy() andrej@2355: andrej@2354: def OnButtonXmlToEEPROMImg(self, event): andrej@2354: """ andrej@2354: Create EEPROM data based XML data that current imported andrej@2354: Binded to 'XML to EEPROM' button. andrej@2354: @param event : wx.EVT_BUTTON object andrej@2354: """ andrej@2354: self.SiiBinary = self.Controler.CommonMethod.XmlToEeprom() andrej@2354: self.HexCode, self.HexRow, self.HexCol = self.Controler.CommonMethod.HexRead(self.SiiBinary) andrej@2354: self.UpdateSiiGridTable(self.HexRow, self.HexCol) andrej@2354: self.SiiGrid.SetValue(self.HexCode) andrej@2354: self.SiiGrid.Update() andrej@2354: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For Hex View grid (fill hex data) andrej@2356: # ------------------------------------------------------------------------------- andrej@2355: class SiiGridTable(wx.grid.Grid): andrej@2354: def __init__(self, parent, controler, row, col): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: Reference to the parent HexView class andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: @param row, col: Hex View grid size andrej@2354: """ andrej@2354: self.parent = parent andrej@2354: self.Controler = controler andrej@2354: self.Row = row andrej@2355: self.Col = col andrej@2355: andrej@2363: wx.grid.Grid.__init__(self, parent, -1, size=(830, 450), andrej@2367: style=wx.ALIGN_CENTRE_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL) andrej@2354: andrej@2354: def SetValue(self, value): andrej@2354: """ andrej@2354: Set data in the table andrej@2354: @param value: EEPROM data list of which element is 1 Byte hex data andrej@2354: """ andrej@2354: # set label name and size andrej@2354: self.SetRowLabelSize(100) andrej@2354: for col in range(self.Col): andrej@2354: if col == 16: andrej@2354: self.SetColLabelValue(16, "Text View") andrej@2354: self.SetColSize(16, (self.GetSize().x-120)*4/20) andrej@2354: else: andrej@2358: self.SetColLabelValue(col, '%s' % col) andrej@2354: self.SetColSize(col, (self.GetSize().x-120)/20) andrej@2355: andrej@2354: # set data into table andrej@2354: row = col = 0 andrej@2355: for row_idx in value: andrej@2354: col = 0 andrej@2354: self.SetRowLabelValue(row, "0x"+"{:0>4x}".format(row*(self.Col-1))) andrej@2354: for hex in row_idx: andrej@2354: self.SetCellValue(row, col, hex) andrej@2355: andrej@2355: if col == 16: andrej@2354: self.SetCellAlignment(row, col, wx.ALIGN_LEFT, wx.ALIGN_CENTER) andrej@2354: else: andrej@2354: self.SetCellAlignment(row, col, wx.ALIGN_CENTRE, wx.ALIGN_CENTER) andrej@2355: andrej@2354: self.SetReadOnly(row, col, True) andrej@2354: col = col + 1 andrej@2354: row = row + 1 andrej@2355: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For Register Access Panel andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class RegisterAccessPanel(wx.Panel): andrej@2354: def __init__(self, parent, controler): andrej@2354: """ andrej@2383: Constructor andrej@2383: @param parent: EEPROMAccessPanel object andrej@2383: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2383: """ andrej@2354: self.parent = parent andrej@2354: self.Controler = controler andrej@2354: self.__init_data() andrej@2355: andrej@2354: wx.Panel.__init__(self, parent, -1) andrej@2355: andrej@2354: sizer = wx.FlexGridSizer(cols=1, hgap=20, rows=2, vgap=5) andrej@2354: button_sizer = wx.FlexGridSizer(cols=2, hgap=10, rows=1, vgap=10) andrej@2355: andrej@2354: self.ReloadButton = wx.Button(self, -1, "Reload") andrej@2355: self.CompactViewCheckbox = wx.CheckBox(self, -1, "Compact View") andrej@2354: self.RegisterNotebook = RegisterNotebook(self, self.Controler) andrej@2355: andrej@2354: button_sizer.AddMany([self.ReloadButton, self.CompactViewCheckbox]) andrej@2354: sizer.AddMany([button_sizer, self.RegisterNotebook]) andrej@2354: self.SetSizer(sizer) andrej@2355: andrej@2354: self.ReloadButton.Bind(wx.EVT_BUTTON, self.OnReloadButton) andrej@2354: self.CompactViewCheckbox.Bind(wx.EVT_CHECKBOX, self.ToggleCompactViewCheckbox) andrej@2355: andrej@2354: for index in range(4): andrej@2354: self.RegisterNotebook.RegPage[index].MainTable.CreateGrid(self.MainRow[index], self.MainCol) andrej@2354: self.RegisterNotebook.RegPage[index].MainTable.SetValue(self, 0, index*512, (index+1)*512) andrej@2355: andrej@2354: # data default setting andrej@2355: if self.Controler.CommonMethod.RegData == "": andrej@2355: self.CompactViewCheckbox.Disable() andrej@2355: for index in range(4): andrej@2354: self.RegisterNotebook.RegPage[index].MainTable.SetValue(self, 0, index*512, (index+1)*512) andrej@2361: else: # If data was saved, andrej@2354: self.BasicSetData() andrej@2354: self.ParseData() andrej@2354: for index in range(4): andrej@2354: self.RegisterNotebook.RegPage[index].MainTable.SetValue(self, self.RegMonitorData, index*512, (index+1)*512) andrej@2354: andrej@2354: def __init_data(self): andrej@2354: """ andrej@2383: Declare initial data. andrej@2383: """ andrej@2354: # flag for compact view andrej@2354: self.CompactFlag = False andrej@2355: andrej@2354: # main grid의 rows and cols andrej@2354: self.MainRow = [512, 512, 512, 512] andrej@2354: self.MainCol = 4 andrej@2355: andrej@2354: # main grids' data range andrej@2354: self.PageRange = [] andrej@2354: for index in range(4): andrej@2354: self.PageRange.append([512*index, 512*(index+1)]) andrej@2355: andrej@2354: # Previous value of register data for register description configuration andrej@2354: self.PreRegSpec = {"ESCType": "", andrej@2354: "FMMUNumber": "", andrej@2354: "SMNumber": "", andrej@2354: "PDIType": ""} andrej@2355: andrej@2354: def LoadData(self): andrej@2354: """ andrej@2354: Get data from the register. andrej@2354: """ andrej@2354: self.Controler.CommonMethod.RegData = "" andrej@2356: # ethercat reg_read andrej@2356: # ex : ethercat reg_read -p 0 0x0000 0x0001 andrej@2356: # return value : 0x11 andrej@2354: for index in range(4): andrej@2354: self.Controler.CommonMethod.RegData = self.Controler.CommonMethod.RegData + " " + self.Controler.CommonMethod.RegRead("0x"+"{:0>4x}".format(index*1024), "0x0400") andrej@2355: andrej@2355: # store previous value andrej@2354: # (ESC type, port number of FMMU, port number of SM, and PDI type)) andrej@2363: for reg_spec in ["ESCType", "FMMUNumber", "SMNumber", "PDIType"]: andrej@2354: self.PreRegSpec[reg_spec] = self.Controler.CommonMethod.CrtRegSpec[reg_spec] andrej@2355: andrej@2355: # update registers' description andrej@2354: # (ESC type, port number of FMMU, port number of SM, and PDI type) andrej@2354: for reg_spec, address in [("ESCType", "0x0000"), andrej@2354: ("FMMUNumber", "0x0004"), andrej@2354: ("SMNumber", "0x0005"), andrej@2354: ("PDIType", "0x0140")]: andrej@2354: self.Controler.CommonMethod.CrtRegSpec[reg_spec] = self.Controler.CommonMethod.RegRead(address, "0x0001") andrej@2355: andrej@2354: # Enable compactView checkbox andrej@2354: self.CompactViewCheckbox.Enable() andrej@2355: andrej@2354: def BasicSetData(self): andrej@2354: """ andrej@2355: Get and save the description of registers. andrej@2354: It's done by parsing register_information.xml. andrej@2354: """ andrej@2354: # parse the above register's value andrej@2354: # If the value is 0x12, the result is 12 andrej@2354: self.ESCType = self.Controler.CommonMethod.CrtRegSpec["ESCType"].split('x')[1] andrej@2354: self.PDIType = self.Controler.CommonMethod.CrtRegSpec["PDIType"].split('x')[1] andrej@2354: # If the value is 0x12, the result is 18 (It's converted to decimal value) andrej@2354: self.FMMUNumber = int(self.Controler.CommonMethod.CrtRegSpec["FMMUNumber"], 16) andrej@2354: self.SMNumber = int(self.Controler.CommonMethod.CrtRegSpec["SMNumber"], 16) andrej@2355: andrej@2354: # initialize description dictionary of register main table and register sub table. andrej@2354: self.RegisterDescriptionDict = {} andrej@2354: self.RegisterSubGridDict = {} andrej@2355: andrej@2354: # ./EthercatMaster/register_information.xml contains register description. andrej@2354: if wx.Platform == '__WXMSW__': andrej@2354: reg_info_file = open("../../EthercatMaster/register_information.xml", 'r') andrej@2354: else: andrej@2354: reg_info_file = open("./EthercatMaster/register_information.xml", 'r') andrej@2354: reg_info_tree = minidom.parse(reg_info_file) andrej@2354: reg_info_file.close() andrej@2355: andrej@2354: # parse register description andrej@2354: for register_info in reg_info_tree.childNodes: andrej@2354: for register in register_info.childNodes: andrej@2354: if register.nodeType == reg_info_tree.ELEMENT_NODE and register.nodeName == "Register": andrej@2354: # If it depends on the property(ESC type, PDI type, FMMU number, SM number) andrej@2354: for property, type, value in [("esc", "type", self.ESCType), andrej@2354: ("pdi", "type", self.PDIType), andrej@2354: ("fmmu", "number", self.FMMUNumber), andrej@2354: ("sm", "number", self.SMNumber)]: andrej@2355: if property in register.attributes.keys(): andrej@2354: if type == "type": andrej@2354: if register.attributes[property].value == value: andrej@2354: self.GetRegisterInfo(reg_info_tree, register) andrej@2354: break andrej@2361: else: # type == "number" andrej@2354: if register.attributes[property].value < value: andrej@2354: self.GetRegisterInfo(reg_info_tree, register) andrej@2354: break andrej@2354: else: andrej@2354: self.GetRegisterInfo(reg_info_tree, register) andrej@2354: break andrej@2355: andrej@2354: def GetRegisterInfo(self, reg_info_tree, register): andrej@2354: """ andrej@2354: Save the register's description into the dictionary. andrej@2354: reg_info_tree is based on the register_information.xml. andrej@2354: @param reg_info_tree: XML tree andrej@2354: @param register: register which you want to get the description andrej@2354: """ andrej@2354: # temporary variables for register main table idescription dictionary andrej@2354: reg_index = "" andrej@2354: reg_main_description = "" andrej@2355: andrej@2354: for data in register.childNodes: andrej@2354: if data.nodeType == reg_info_tree.ELEMENT_NODE and data.nodeName == "Index": andrej@2354: for index in data.childNodes: andrej@2354: reg_index = index.nodeValue andrej@2354: if data.nodeType == reg_info_tree.ELEMENT_NODE and data.nodeName == "Description": andrej@2354: for description in data.childNodes: andrej@2354: reg_main_description = description.nodeValue andrej@2355: andrej@2355: # Add description for register main table andrej@2417: if reg_index != "" and reg_main_description != "": andrej@2354: self.RegisterDescriptionDict[reg_index] = reg_main_description andrej@2355: andrej@2354: if data.nodeType == reg_info_tree.ELEMENT_NODE and data.nodeName == "Details": andrej@2354: # declare register sub table description dictionary about this index andrej@2354: self.RegisterSubGridDict[reg_index] = [] andrej@2355: andrej@2354: for detail in data.childNodes: andrej@2354: if detail.nodeType == reg_info_tree.ELEMENT_NODE and detail.nodeName == "Detail": andrej@2354: # If it depends on the property(ESC type, PDI type, FMMU number, SM number) andrej@2354: for property, type, value in [("esc", "type", self.ESCType), andrej@2354: ("pdi", "type", self.PDIType), andrej@2354: ("fmmu", "number", self.FMMUNumber), andrej@2354: ("sm", "number", self.SMNumber)]: andrej@2355: if property in detail.attributes.keys(): andrej@2354: if type == "type": andrej@2354: if detail.attributes[property].value == value: andrej@2354: self.GetRegisterDetailInfo(reg_info_tree, reg_index, detail) andrej@2354: break andrej@2361: else: # type == "number" andrej@2354: if detail.attributes[property].value < value: andrej@2354: self.GetRegisterDetailInfo(reg_info_tree, reg_index, detail) andrej@2354: break andrej@2354: else: andrej@2354: self.GetRegisterDetailInfo(reg_info_tree, reg_index, detail) andrej@2354: break andrej@2355: andrej@2354: def GetRegisterDetailInfo(self, reg_info_tree, reg_index, detail): andrej@2354: """ andrej@2354: Get the resgister's detailed description(for sub table) from the reg_info_tree. andrej@2354: @param reg_info_tree: XML tree (register_information.xml) andrej@2354: @param reg_index: index of the register andrej@2354: @param detail: description of the register andrej@2354: """ andrej@2355: # temporary variables for register sub table description dictionary andrej@2355: # - It is initialized in every sub description andrej@2354: reg_bit_range = "" andrej@2354: reg_sub_description = "" andrej@2354: reg_enum_dictionary = {} andrej@2355: andrej@2354: for detail_data in detail.childNodes: andrej@2355: if detail_data.nodeType == reg_info_tree.ELEMENT_NODE and detail_data.nodeName == "Range": andrej@2354: for range in detail_data.childNodes: andrej@2354: reg_bit_range = range.nodeValue andrej@2354: if detail_data.nodeType == reg_info_tree.ELEMENT_NODE and detail_data.nodeName == "Description": andrej@2354: for description in detail_data.childNodes: andrej@2354: reg_sub_description = description.nodeValue andrej@2355: andrej@2354: if detail_data.nodeType == reg_info_tree.ELEMENT_NODE and detail_data.nodeName == "Enum": andrej@2354: for enum in detail_data.childNodes: andrej@2354: if enum.nodeType == reg_info_tree.ELEMENT_NODE and enum.nodeName == "item": andrej@2355: andrej@2355: # temporary variables for a description of each value andrej@2355: # For example, if the bit is 1, it is 'enabled'('On', 'True', etc.), andrej@2355: # otherwise 'disabled'('Off', 'False', etc.). andrej@2354: reg_sub_value = "" andrej@2354: reg_sub_value_description = "" andrej@2355: andrej@2354: for item in enum.childNodes: andrej@2354: if item.nodeType == reg_info_tree.ELEMENT_NODE and item.nodeName == "value": andrej@2354: for value in item.childNodes: andrej@2354: reg_sub_value = value.nodeValue andrej@2354: if item.nodeType == reg_info_tree.ELEMENT_NODE and item.nodeName == "Description": andrej@2354: for description in item.childNodes: andrej@2354: reg_sub_value_description = description.nodeValue andrej@2355: andrej@2354: # Add a description of each value to register enum dictionary andrej@2417: if reg_sub_value != "" and reg_sub_value_description != "": andrej@2354: reg_enum_dictionary[reg_sub_value] = reg_sub_value_description andrej@2355: andrej@2354: # add a description to register sub table description dictionary andrej@2417: if reg_bit_range != "" and reg_sub_description != "": andrej@2355: self.RegisterSubGridDict[reg_index].append([reg_bit_range, andrej@2380: reg_sub_description, andrej@2380: reg_enum_dictionary]) andrej@2355: andrej@2354: def ParseData(self): andrej@2354: """ andrej@2354: Transform the data into dec, hex, string, and description andrej@2354: """ andrej@2354: row_data = [] andrej@2354: self.RegMonitorData = [] andrej@2354: reg_word = "" andrej@2355: andrej@2354: reg_data = self.Controler.CommonMethod.RegData.split() andrej@2355: andrej@2354: # loop for register(0x0000:0x0fff) andrej@2354: for address in range(0x1000): andrej@2355: # arrange 2 Bytes of register data andrej@2354: reg_word = reg_data[address].split('x')[1] + reg_word andrej@2358: if (address % 2) == 1: andrej@2354: # append address andrej@2354: hex_address = "{:0>4x}".format(address-1) andrej@2354: row_data.append(hex_address) andrej@2355: andrej@2354: # append description andrej@2377: if hex_address in self.RegisterDescriptionDict: andrej@2354: row_data.append(self.RegisterDescriptionDict[hex_address]) andrej@2354: else: andrej@2354: row_data.append("") andrej@2355: andrej@2354: # append Decimal value andrej@2354: row_data.append(str(int(reg_word, 16))) andrej@2355: andrej@2354: # append Hex value andrej@2354: row_data.append('0x'+reg_word) andrej@2355: andrej@2354: # append ASCII value andrej@2354: char_data = "" andrej@2354: for iter in range(2): andrej@2365: if int(reg_word[iter*2:iter*2+2], 16) >= 32 and int(reg_word[iter*2:iter*2+2], 16) <= 126: andrej@2354: char_data = char_data + chr(int(reg_word[iter*2:iter*2+2], 16)) andrej@2354: else: andrej@2354: char_data = char_data + "." andrej@2354: row_data.append(char_data) andrej@2355: andrej@2354: self.RegMonitorData.append(row_data) andrej@2361: reg_word = "" # initialize regWord andrej@2354: row_data = [] andrej@2355: andrej@2354: def OnReloadButton(self, event): andrej@2354: """ andrej@2354: Handle the click event of the 'Reload' button. andrej@2354: Get the data from registers again, and update the table. andrej@2354: @param event: wx.EVT_BUTTON object andrej@2354: """ andrej@2354: # Check whether beremiz connected or not. andrej@2354: check_connect_flag = self.Controler.CommonMethod.CheckConnect(False) andrej@2354: if check_connect_flag: andrej@2354: self.LoadData() andrej@2354: self.BasicSetData() andrej@2354: self.ParseData() andrej@2354: # set data into UI andrej@2354: if self.CompactFlag: andrej@2354: self.ToggleCompactViewCheckbox(True) andrej@2375: else: andrej@2354: for index in range(4): andrej@2355: self.RegisterNotebook.RegPage[index].UpdateMainTable(self.MainRow[index], self.MainCol, andrej@2355: self.PageRange[index][0], self.PageRange[index][1], andrej@2354: self.RegMonitorData) andrej@2354: andrej@2354: def ToggleCompactViewCheckbox(self, event): andrej@2354: """ andrej@2354: Handles the event of the 'Compact view' check box. andrej@2354: If it's checked, show only the registers that have a description. andrej@2354: If not, show all the registers. andrej@2354: @param event: wx.EVT_CHECKBOX object andrej@2354: """ andrej@2355: andrej@2354: # If "Compact View" Checkbox is True andrej@2385: # 'event' is argument of this method or event of checkbox. andrej@2374: if event is True or event.GetEventObject().GetValue(): andrej@2354: self.CompactFlag = True andrej@2355: andrej@2354: reg_compact_data = [] andrej@2354: page_row = [0, 0, 0, 0] andrej@2354: for index in range(4): andrej@2354: self.PageRange[index] = [0, 0] andrej@2354: andrej@2354: for reg_row_data in self.RegMonitorData: andrej@2417: if reg_row_data[1] != "": andrej@2354: # data structure for "compact view" andrej@2354: reg_compact_data.append(reg_row_data) andrej@2354: # count for each register notebooks' row andrej@2354: # It compare with register's address. andrej@2354: for index in range(4): andrej@2354: if int('0x'+reg_row_data[0], 16) < (index+1)*1024: andrej@2354: page_row[index] += 1 andrej@2354: break andrej@2354: andrej@2354: # Setting tables' rows and cols, range for compact view andrej@2354: for index in range(4): andrej@2354: self.MainRow[index] = page_row[index] andrej@2354: self.PageRange[index][1] = page_row[index] andrej@2354: for iter in range(index): andrej@2354: self.PageRange[index][0] += page_row[iter] andrej@2355: self.PageRange[index][1] += page_row[iter] andrej@2355: andrej@2354: # Update table andrej@2354: for index in range(4): andrej@2380: self.RegisterNotebook.RegPage[index].UpdateMainTable( andrej@2380: self.MainRow[index], andrej@2380: self.MainCol, andrej@2380: self.PageRange[index][0], andrej@2380: self.PageRange[index][1], andrej@2380: reg_compact_data) andrej@2355: andrej@2355: # Compact View Checkbox is False andrej@2354: else: andrej@2354: self.CompactFlag = False andrej@2354: # Setting original rows, cols and range andrej@2354: self.MainRow = [512, 512, 512, 512] andrej@2354: self.PageRange = [] andrej@2355: andrej@2354: for index in range(4): andrej@2354: self.PageRange.append([512*index, 512*(index+1)]) andrej@2355: andrej@2355: # Update table andrej@2354: for index in range(4): andrej@2380: self.RegisterNotebook.RegPage[index].UpdateMainTable( andrej@2380: self.MainRow[index], andrej@2380: self.MainCol, andrej@2380: self.PageRange[index][0], andrej@2380: self.PageRange[index][1], andrej@2380: self.RegMonitorData) andrej@2355: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For Register Access Notebook (divide index range) andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class RegisterNotebook(wx.Notebook): andrej@2354: def __init__(self, parent, controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: RegisterAccessPanel object andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2366: wx.Notebook.__init__(self, parent, id=-1) andrej@2355: andrej@2354: self.parent = parent andrej@2354: self.Controler = controler andrej@2355: andrej@2354: # Initialize pages andrej@2354: self.RegPage = [] andrej@2406: pages = 4 andrej@2406: for dummy in range(pages): andrej@2354: self.RegPage.append(None) andrej@2355: andrej@2406: for index in range(pages): andrej@2355: self.RegPage[index] = RegisterNotebookPanel(self, self.Controler, andrej@2381: parent.MainRow[index], parent.MainCol) andrej@2355: self.AddPage(self.RegPage[index], andrej@2354: "0x"+"{:0>4x}".format(index*1024)+" - 0x"+"{:0>4x}".format((index+1)*1024-1)) andrej@2355: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2355: # For Register Access Notebook Panel andrej@2354: # (Main UI : including main, sub table) andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class RegisterNotebookPanel(wx.Panel): andrej@2354: def __init__(self, parent, controler, row, col): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: RegisterAccessPanel object andrej@2354: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: @param row, col: size of the table andrej@2383: """ andrej@2354: wx.Panel.__init__(self, parent, -1) andrej@2355: andrej@2354: self.parent = parent andrej@2354: self.Controler = controler andrej@2354: self.Row = row andrej@2354: self.Col = col andrej@2354: sub_row = 0 andrej@2354: sub_col = 4 andrej@2355: andrej@2354: self.Sizer = wx.FlexGridSizer(cols=1, hgap=10, rows=2, vgap=30) andrej@2355: andrej@2354: self.MainTable = RegisterMainTable(self, self.Row, self.Col, self.Controler) andrej@2354: self.SubTable = RegisterSubTable(self, sub_row, sub_col) andrej@2355: andrej@2354: self.SubTable.CreateGrid(sub_row, sub_col) andrej@2354: self.SubTable.SetValue(self, []) andrej@2355: andrej@2354: self.Sizer.AddMany([self.MainTable, self.SubTable]) andrej@2355: andrej@2354: self.SetSizer(self.Sizer) andrej@2355: andrej@2354: def UpdateMainTable(self, row, col, low_index, high_index, data): andrej@2354: """ andrej@2354: Updates main table. andrej@2354: It's done by deleting the main table and creating it again. andrej@2354: @param row, col: size of the table andrej@2354: @param low_index: the lowest index of the page andrej@2354: @param high_index: the highest index of the page andrej@2354: @param data: data andrej@2383: """ andrej@2354: self.MainTable.Destroy() andrej@2354: self.MainTable = RegisterMainTable(self, row, col, self.Controler) andrej@2354: self.Sizer.Detach(self.SubTable) andrej@2354: self.Sizer.AddMany([self.MainTable, self.SubTable]) andrej@2354: self.SetSizer(self.Sizer) andrej@2354: self.MainTable.CreateGrid(row, col) andrej@2354: self.MainTable.SetValue(self, data, low_index, high_index) andrej@2354: self.MainTable.Update() andrej@2354: andrej@2354: def UpdateSubTable(self, row, col, data): andrej@2354: """ andrej@2354: Updates sub table. andrej@2354: It's done by deleting the sub table and creating it again. andrej@2354: @param row, col: size of the table andrej@2354: @param data: data andrej@2383: """ andrej@2354: self.SubTable.Destroy() andrej@2354: self.SubTable = RegisterSubTable(self, row, col) andrej@2354: self.Sizer.Detach(self.MainTable) andrej@2354: self.Sizer.AddMany([self.MainTable, self.SubTable]) andrej@2354: self.Sizer.Layout() andrej@2354: self.SetSizer(self.Sizer) andrej@2354: self.SubTable.CreateGrid(row, col) andrej@2354: self.SubTable.SetValue(self, data) andrej@2354: self.SubTable.Update() andrej@2355: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For Register Access Notebook Panel (Main Table) andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class RegisterMainTable(wx.grid.Grid): andrej@2355: def __init__(self, parent, row, col, controler): andrej@2354: """ andrej@2383: Constructor andrej@2383: @param parent: RegisterNotebook object andrej@2383: @param row, col: size of the table andrej@2383: @param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2383: """ andrej@2354: self.parent = parent andrej@2354: self.Data = {} andrej@2354: self.Row = row andrej@2354: self.Col = col andrej@2354: self.Controler = controler andrej@2354: self.RegisterAccessPanel = self.parent.parent.parent andrej@2355: andrej@2363: wx.grid.Grid.__init__(self, parent, -1, size=(820, 300), andrej@2367: style=wx.EXPAND | wx.ALIGN_CENTRE_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL) andrej@2355: andrej@2389: for evt, mapping_method in [(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.OnSelectCell), andrej@2389: (wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.OnSelectCell), andrej@2389: (wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnRegModifyDialog)]: andrej@2354: self.Bind(evt, mapping_method) andrej@2355: andrej@2355: def SetValue(self, parent, reg_monitor_data, low_index, high_index): andrej@2354: """ andrej@2383: Set the RegMonitorData into the main table. andrej@2383: @param parent: RegisterNotebook object andrej@2383: @param reg_monitor_data: data andrej@2383: @param low_index: the lowest index of the page andrej@2383: @param high_index: the highest index of the page andrej@2383: """ andrej@2354: self.RegMonitorData = reg_monitor_data andrej@2355: andrej@2354: # set label name and size andrej@2355: register_maintable_label = [(0, "Description"), (1, "Dec"), andrej@2354: (2, "Hex"), (3, "Char")] andrej@2355: andrej@2354: for (index, label) in register_maintable_label: andrej@2354: self.SetColLabelValue(index, label) andrej@2355: andrej@2354: self.SetColSize(0, 200) andrej@2355: andrej@2354: # if reg_monitor_data is 0, it is initialization of register access. andrej@2354: if reg_monitor_data == 0: andrej@2354: return 0 andrej@2355: andrej@2355: # set data into UI andrej@2354: row = col = 0 andrej@2354: for row_index in reg_monitor_data[low_index:high_index]: andrej@2354: col = 0 andrej@2354: self.SetRowLabelValue(row, row_index[0]) andrej@2354: for data_index in range(4): andrej@2354: self.SetCellValue(row, col, row_index[data_index+1]) andrej@2354: self.SetCellAlignment(row, col, wx.ALIGN_CENTRE, wx.ALIGN_CENTER) andrej@2354: self.SetReadOnly(row, col, True) andrej@2354: col = col + 1 andrej@2354: row = row + 1 andrej@2355: andrej@2355: def OnSelectCell(self, event): andrej@2354: """ andrej@2383: Handles the event of the cell of the main table. andrej@2389: @param event: wx.grid object (left click) andrej@2383: """ andrej@2354: # if reg_monitor_data is 0, it is initialization of register access. andrej@2354: if self.RegMonitorData == 0: andrej@2354: event.Skip() andrej@2354: return 0 andrej@2355: andrej@2354: sub_row = 0 andrej@2354: sub_col = 4 andrej@2355: andrej@2354: address = self.GetRowLabelValue(event.GetRow()) andrej@2355: andrej@2354: reg_sub_grid_data = [] andrej@2355: andrej@2354: BIT_RANGE, NAME, DESCRIPTIONS = range(3) andrej@2355: andrej@2355: # Check if this register's detail description is exist or not, andrej@2354: # and create data structure for the detail description table ; sub grid andrej@2354: if address in self.RegisterAccessPanel.RegisterSubGridDict: andrej@2354: for element in self.RegisterAccessPanel.RegisterSubGridDict[address]: andrej@2365: row_data = [] andrej@2354: row_data.append(element[BIT_RANGE]) andrej@2354: row_data.append(element[NAME]) andrej@2354: bin_data = "{:0>16b}".format(int(self.GetCellValue(event.GetRow(), 1))) andrej@2354: value_range = element[BIT_RANGE].split('-') andrej@2354: value = (bin_data[8:16][::-1]+bin_data[0:8][::-1])[int(value_range[0]):(int(value_range[-1])+1)][::-1] andrej@2354: row_data.append(str(int(('0b'+str(value)), 2))) andrej@2354: if value in element[DESCRIPTIONS]: andrej@2354: row_data.append(element[DESCRIPTIONS][value]) andrej@2354: else: andrej@2354: row_data.append('') andrej@2354: reg_sub_grid_data.append(row_data) andrej@2354: sub_row = sub_row + 1 andrej@2355: andrej@2354: self.parent.UpdateSubTable(sub_row, sub_col, reg_sub_grid_data) andrej@2354: # event.Skip() updates UI of selecting cell andrej@2354: event.Skip() andrej@2355: andrej@2354: def OnRegModifyDialog(self, event): andrej@2354: """ andrej@2354: Handle the event of the cell of the main table. andrej@2354: Display the window where the user modifies the value of the cell. andrej@2389: @param event: wx.grid object (double click) andrej@2383: """ andrej@2354: # user can enter a value in case that user double-clicked 'Dec' or 'Hex' value. andrej@2354: if event.GetCol() == 1 or event.GetCol() == 2: andrej@2355: dlg = wx.TextEntryDialog(self, "Enter hex(0xnnnn) or dec(n) value", andrej@2367: "Register Modify Dialog", style=wx.OK | wx.CANCEL) andrej@2355: andrej@2354: # Setting value in initial dialog value andrej@2354: start_value = self.GetCellValue(event.GetRow(), event.GetCol()) andrej@2354: dlg.SetValue(start_value) andrej@2355: andrej@2354: if dlg.ShowModal() == wx.ID_OK: andrej@2354: try: andrej@2355: # It int(input) success, this input is dev or hex value. andrej@2354: # Otherwise, it's error, so it goes except. andrej@2354: int(dlg.GetValue(), 0) andrej@2354: andrej@2354: # reg_write andrej@2354: # ex) ethercat reg_write -p 0 -t uint16 0x0000 0x0000 andrej@2354: return_val = self.Controler.CommonMethod.RegWrite('0x'+self.GetRowLabelValue(event.GetRow()), dlg.GetValue()) andrej@2354: andrej@2365: if len(return_val) == 0: andrej@2354: # set dec andrej@2355: self.SetCellValue(event.GetRow(), 1, str(int(dlg.GetValue(), 0))) andrej@2354: # set hex andrej@2354: hex_data = '0x'+"{:0>4x}".format(int(dlg.GetValue(), 0)) andrej@2354: self.SetCellValue(event.GetRow(), 2, hex_data) andrej@2354: # set char andrej@2354: char_data = "" andrej@2354: # If hex_data is been able to convert to ascii code, append ascii code. andrej@2354: for iter in range(2): andrej@2365: if int(hex_data[(iter+1)*2:(iter+2)*2], 16) >= 32 and int(hex_data[(iter+1)*2:(iter+2)*2], 16) <= 126: andrej@2354: char_data = char_data + chr(int(hex_data[(iter+1)*2:(iter+2)*2], 16)) andrej@2354: else: andrej@2354: char_data = char_data + "." andrej@2355: andrej@2355: self.SetCellValue(event.GetRow(), 3, char_data) andrej@2355: andrej@2354: else: andrej@2354: self.Controler.CommonMethod.CreateErrorDialog('You can\'t modify it. This register is read-only or it\'s not connected.') andrej@2355: andrej@2354: except ValueError: andrej@2354: self.Controler.CommonMethod.CreateErrorDialog('You entered wrong value. You can enter dec or hex value only.') andrej@2355: andrej@2355: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For Register Access Notebook Panel (Sub Table) andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class RegisterSubTable(wx.grid.Grid): andrej@2354: def __init__(self, parent, row, col): andrej@2354: """ andrej@2383: Constructor andrej@2383: @param parent: RegisterNotebook object andrej@2383: @param row, col: size of the table andrej@2383: """ andrej@2354: self.parent = parent andrej@2354: self.Data = {} andrej@2354: self.Row = row andrej@2354: self.Col = col andrej@2354: andrej@2363: wx.grid.Grid.__init__(self, parent, -1, size=(820, 150), andrej@2367: style=wx.EXPAND | wx.ALIGN_CENTRE_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL) andrej@2354: andrej@2354: def SetValue(self, parent, data): andrej@2354: """ andrej@2383: Set the data into the subtable. andrej@2383: @param parent: RegisterNotebook object andrej@2383: @param data: data andrej@2383: """ andrej@2354: # lset label name and size andrej@2355: Register_SubTable_Label = [(0, "Bits"), (1, "Name"), andrej@2380: (2, "Value"), (3, "Enum")] andrej@2355: andrej@2354: for (index, label) in Register_SubTable_Label: andrej@2354: self.SetColLabelValue(index, label) andrej@2355: andrej@2354: self.SetColSize(1, 200) andrej@2354: self.SetColSize(3, 200) andrej@2355: andrej@2354: # set data into table andrej@2354: row = col = 0 andrej@2355: for rowData in data: andrej@2355: col = 0 andrej@2354: for element in rowData: andrej@2354: self.SetCellValue(row, col, element) andrej@2354: self.SetCellAlignment(row, col, wx.ALIGN_CENTRE, wx.ALIGN_CENTER) andrej@2354: self.SetReadOnly(row, col, True) andrej@2354: col = col + 1 andrej@2354: row = row + 1 andrej@2355: andrej@2354: andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: # For Master State Panel andrej@2356: # ------------------------------------------------------------------------------- andrej@2354: class MasterStatePanelClass(wx.Panel): andrej@2354: def __init__(self, parent, controler): andrej@2354: """ andrej@2354: Constructor andrej@2354: @param parent: wx.ScrollWindow object andrej@2354: @Param controler: _EthercatSlaveCTN class in EthercatSlave.py andrej@2354: """ andrej@2355: wx.Panel.__init__(self, parent, -1, (0, 0), andrej@2366: size=wx.DefaultSize, style=wx.SUNKEN_BORDER) andrej@2354: self.Controler = controler andrej@2354: self.parent = parent andrej@2354: self.StaticBox = {} andrej@2354: self.StaticText = {} andrej@2354: self.TextCtrl = {} andrej@2355: andrej@2354: # ----------------------- Main Sizer and Update Button -------------------------------------------- andrej@2375: self.MasterStateSizer = {"main": wx.BoxSizer(wx.VERTICAL)} andrej@2354: for key, attr in [ andrej@2379: ("innerMain", [1, 10, 2, 10]), andrej@2379: ("innerTopHalf", [2, 10, 1, 10]), andrej@2379: ("innerBottomHalf", [2, 10, 1, 10]), andrej@2379: ("innerMasterState", [2, 10, 3, 10]), andrej@2379: ("innerDeviceInfo", [4, 10, 3, 10]), andrej@2379: ("innerFrameInfo", [4, 10, 5, 10])]: andrej@2354: self.MasterStateSizer[key] = wx.FlexGridSizer(cols=attr[0], hgap=attr[1], rows=attr[2], vgap=attr[3]) andrej@2354: andrej@2354: self.UpdateButton = wx.Button(self, label=_('Update')) andrej@2354: self.UpdateButton.Bind(wx.EVT_BUTTON, self.OnButtonClick) andrej@2355: andrej@2355: for key, label in [ andrej@2379: ('masterState', 'EtherCAT Master State'), andrej@2379: ('deviceInfo', 'Ethernet Network Card Information'), andrej@2379: ('frameInfo', 'Network Frame Information')]: andrej@2354: self.StaticBox[key] = wx.StaticBox(self, label=_(label)) andrej@2354: self.MasterStateSizer[key] = wx.StaticBoxSizer(self.StaticBox[key]) andrej@2355: andrej@2354: # ----------------------- Master State ----------------------------------------------------------- andrej@2354: for key, label in [ andrej@2379: ('Phase', 'Phase:'), andrej@2379: ('Active', 'Active:'), andrej@2379: ('Slaves', 'Slave Count:')]: andrej@2354: self.StaticText[key] = wx.StaticText(self, label=_(label)) andrej@2354: self.TextCtrl[key] = wx.TextCtrl(self, size=wx.Size(130, 24), style=wx.TE_READONLY) andrej@2355: self.MasterStateSizer['innerMasterState'].AddMany([self.StaticText[key], self.TextCtrl[key]]) andrej@2355: andrej@2354: self.MasterStateSizer['masterState'].AddSizer(self.MasterStateSizer['innerMasterState']) andrej@2355: andrej@2355: # ----------------------- Ethernet Network Card Information --------------------------------------- andrej@2354: for key, label in [ andrej@2379: ('Main', 'MAC Address:'), andrej@2379: ('Link', 'Link State:'), andrej@2379: ('Tx frames', 'Tx Frames:'), andrej@2379: ('Rx frames', 'Rx Frames:'), andrej@2379: ('Lost frames', 'Lost Frames:')]: andrej@2354: self.StaticText[key] = wx.StaticText(self, label=_(label)) andrej@2354: self.TextCtrl[key] = wx.TextCtrl(self, size=wx.Size(130, 24), style=wx.TE_READONLY) andrej@2354: self.MasterStateSizer['innerDeviceInfo'].AddMany([self.StaticText[key], self.TextCtrl[key]]) andrej@2355: andrej@2354: self.MasterStateSizer['deviceInfo'].AddSizer(self.MasterStateSizer['innerDeviceInfo']) andrej@2355: andrej@2354: # ----------------------- Network Frame Information ----------------------------------------------- andrej@2354: for key, label in [ andrej@2379: ('Tx frame rate [1/s]', 'Tx Frame Rate [1/s]:'), andrej@2379: ('Rx frame rate [1/s]', 'Tx Rate [kByte/s]:'), andrej@2379: ('Loss rate [1/s]', 'Loss Rate [1/s]:'), andrej@2379: ('Frame loss [%]', 'Frame Loss [%]:')]: andrej@2354: self.StaticText[key] = wx.StaticText(self, label=_(label)) andrej@2354: self.MasterStateSizer['innerFrameInfo'].Add(self.StaticText[key]) andrej@2355: self.TextCtrl[key] = {} andrej@2355: for index in ['0', '1', '2']: andrej@2354: self.TextCtrl[key][index] = wx.TextCtrl(self, size=wx.Size(130, 24), style=wx.TE_READONLY) andrej@2354: self.MasterStateSizer['innerFrameInfo'].Add(self.TextCtrl[key][index]) andrej@2355: andrej@2354: self.MasterStateSizer['frameInfo'].AddSizer(self.MasterStateSizer['innerFrameInfo']) andrej@2355: andrej@2354: # --------------------------------- Main Sizer ---------------------------------------------------- andrej@2354: for key, sub, in [ andrej@2407: ('innerTopHalf', ['masterState', 'deviceInfo']), andrej@2407: ('innerBottomHalf', ['frameInfo']), andrej@2407: ('innerMain', ['innerTopHalf', 'innerBottomHalf']) andrej@2407: ]: andrej@2354: for key2 in sub: andrej@2354: self.MasterStateSizer[key].AddSizer(self.MasterStateSizer[key2]) andrej@2354: andrej@2354: self.MasterStateSizer['main'].AddSizer(self.UpdateButton) andrej@2354: self.MasterStateSizer['main'].AddSizer(self.MasterStateSizer['innerMain']) andrej@2355: andrej@2354: self.SetSizer(self.MasterStateSizer['main']) andrej@2354: andrej@2354: def OnButtonClick(self, event): andrej@2354: """ andrej@2354: Handle the event of the 'Update' button. andrej@2354: Update the data of the master state. andrej@2354: @param event: wx.EVT_BUTTON object andrej@2354: """ andrej@2354: if self.Controler.GetCTRoot()._connector is not None: andrej@2354: self.MasterState = self.Controler.CommonMethod.GetMasterState() andrej@2354: # Update each TextCtrl andrej@2354: if self.MasterState: andrej@2354: for key in self.TextCtrl: andrej@2354: if isinstance(self.TextCtrl[key], dict): andrej@2354: for index in self.TextCtrl[key]: andrej@2354: self.TextCtrl[key][index].SetValue(self.MasterState[key][int(index)]) andrej@2354: else: andrej@2354: self.TextCtrl[key].SetValue(self.MasterState[key][0]) andrej@2375: else: andrej@2354: self.Controler.CommonMethod.CreateErrorDialog('PLC not connected!')