#!/usr/bin/env python # -*- coding: UTF-8 -*- import wx import wx.propgrid import gettext import can import cantools db = cantools.database.load_file('res/EBUS_Host.dbc') from BmsView import BmsMonitorFrame def parse_can_id (arbitration_id): mtag = (arbitration_id & 0xfffff000) >> 12 mid = (arbitration_id & 0xf00) >> 8 mtype = arbitration_id & 0xff return mtag, mid, mtype def findCategory (imsg, isignal): tag, id, type = parse_can_id(imsg) if tag != 0x14180: return None if 0x00 <= type <= 0x06: return "bmsprofile" elif type == 0x11: return "packprofile" elif type == 0x10: if isignal < 4: return "packprofile" else: return "packalarms" elif type == 0x15: if isignal < 3: return "packdata" else: return "packalarms" elif type == 0x14: return "packdata" elif 0x16 <= type <= 0x20: return "packdata" elif 0x07 <= type <= 0x0B: return "moduledata" else: print("undefined category ", hex(imsg)) return "undefined" cat_propgrid = ["bmsprofile", "packprofile", "packdata", "packalarms",] class BmsMonitorView(BmsMonitorFrame): def __init__(self, *args, **kwds): BmsMonitorFrame.__init__(self, *args, **kwds) self.timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) self.property_grid_page_info = self.property_grid_1.AddPage("Info") for cat_name in cat_propgrid: cat = wx.propgrid.PropertyCategory(label=cat_name) self.property_grid_page_info.Append(cat) # bms_model = wx.propgrid.StringProperty(label="Model", name="bms_model", value="B3DWER32EW") # self.property_grid_page_info.AppendIn("packdata", bms_model) tohex = "{:#04X}-".format for msg in db.messages: tag, id, type = parse_can_id(msg.frame_id) if id == 0x02: if findCategory(msg.frame_id, 0) == "bmsprofile": prop = wx.propgrid.StringProperty(label=msg.name) self.property_grid_page_info.AppendIn("bmsprofile", prop) for isig, signal in enumerate(msg.signals): plabel = tohex(type)+signal.name if signal.is_float: prop = wx.propgrid.FloatProperty(label=plabel) else: prop = wx.propgrid.IntProperty(label=plabel) prop.SetHelpString(signal.comment) cat = findCategory(msg.frame_id, isig) if cat in cat_propgrid: self.property_grid_page_info.AppendIn(cat, prop) ''' if tag == 0x14180 and id == 0x2 and 0x00 <= type <= 0x06: # BMS Profile self.property_grid_page_info.AppendIn("bmsprofile", prop) if tag == 0x14180 and id == 0x2 and 0x10 <= type <= 0x20: # Pack Profile and Data self.property_grid_page_info.AppendIn("packdata", prop) ''' def OnToggle(self, event): btnLabel = self.button_1.GetLabel() if self.button_1.Value: print ("starting timer...") self.bus = can.Bus() self.timer.Start(10) self.button_1.SetLabel("Stop") else: print ( "timer stopped!" ) self.bus.shutdown() self.bus = None self.timer.Stop() self.button_1.SetLabel("Start") event.Skip() def OnTimer(self, event): # print("Read CAN Message!") msg = self.bus.recv(timeout=1.) mtag, mid, mtype = parse_can_id(msg.frame_id) if 0x07 <= mtype <= 0x0b: try: dmsg = self.candb.decode_message(msg.arbitration_id, msg.data) except KeyError as key: print(key, " does not exist") else: print(hex(mtag), hex(mid), hex(mtype), msg.data, dmsg) self.__update_bm_state(dmsg) for k in self.battery_module_state[0]: if self.property_grid_1.GetProperty(k): self.property_grid_1.SetPropertyValue(k, self.battery_module_state[0][k]) else: self.property_grid_1.Append(wx.propgrid.StringProperty(label=k, value=str(self.battery_module_state[0][k]))) master_id = int(self.choice_1.GetStringSelection()) if mid == master_id: if findCategory(msg.frame_id, 0) == "bmsprofile": prop = wx.propgrid.StringProperty(label=msg.name) self.property_grid_page_info.AppendIn("bmsprofile", prop) for isig, signal in enumerate(msg.signals): plabel = tohex(type) + signal.name if signal.is_float: prop = wx.propgrid.FloatProperty(label=plabel) else: prop = wx.propgrid.IntProperty(label=plabel) prop.SetHelpString(signal.comment) cat = findCategory(msg.frame_id, isig) if cat in cat_propgrid: self.property_grid_page_info.AppendIn(cat, prop) ''' if tag == 0x14180 and id == 0x2 and 0x00 <= type <= 0x06: # BMS Profile self.property_grid_page_info.AppendIn("bmsprofile", prop) if tag == 0x14180 and id == 0x2 and 0x10 <= type <= 0x20: # Pack Profile and Data self.property_grid_page_info.AppendIn("packdata", prop) ''' event.Skip() # end of class BmsMonitorFrame class BmsApp(wx.App): def OnInit(self): self.frame = BmsMonitorView(None, wx.ID_ANY, "") self.SetTopWindow(self.frame) self.frame.Show() return True # end of class BmsApp if __name__ == "__main__": gettext.install("app") # replace with the appropriate catalog name app = BmsApp(0) app.MainLoop()