From d3095755a82e9753fcba3ec46b9681589bb4cc2e Mon Sep 17 00:00:00 2001 From: Yeongdo Park Date: Mon, 3 Aug 2020 17:52:55 -0700 Subject: [PATCH] WIP: intermediate parsing --- BmsApp.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ BmsView.py | 11 +++++++- BmsView.wxg | 20 ++++++++++++++- 3 files changed, 101 insertions(+), 2 deletions(-) diff --git a/BmsApp.py b/BmsApp.py index b49cff6..f105499 100644 --- a/BmsApp.py +++ b/BmsApp.py @@ -6,6 +6,7 @@ import wx.propgrid import gettext +import can import cantools db = cantools.database.load_file('res/EBUS_Host.dbc') @@ -60,6 +61,9 @@ 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: @@ -100,6 +104,74 @@ class BmsMonitorView(BmsMonitorFrame): 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 diff --git a/BmsView.py b/BmsView.py index 5a333de..7ea083d 100644 --- a/BmsView.py +++ b/BmsView.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: UTF-8 -*- # -# generated by wxGlade 0.9.5 on Fri Feb 21 16:44:13 2020 +# generated by wxGlade 0.9.6 on Mon Aug 3 16:52:33 2020 # import wx @@ -22,6 +22,7 @@ class BmsMonitorFrame(wx.Frame): kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) self.SetSize((831, 774)) + self.button_1 = wx.ToggleButton(self, wx.ID_ANY, _("Start")) self.choice_2 = wx.Choice(self, wx.ID_ANY, choices=[_("choice 1")]) self.choice_1 = wx.Choice(self, wx.ID_ANY, choices=[_("0x02"), _("0x03"), _("0x04"), _("0x05"), _("0x06"), _("0x07"), _("0x08"), _("0x09")]) self.property_grid_1 = wx.propgrid.PropertyGridManager(self, wx.ID_ANY, style=wx.propgrid.PG_AUTO_SORT | wx.propgrid.PG_DESCRIPTION | wx.propgrid.PG_TOOLBAR) @@ -32,6 +33,8 @@ class BmsMonitorFrame(wx.Frame): self.__set_properties() self.__do_layout() + + self.Bind(wx.EVT_TOGGLEBUTTON, self.OnToggle, self.button_1) # end wxGlade def __set_properties(self): @@ -68,6 +71,8 @@ class BmsMonitorFrame(wx.Frame): sizer_5 = wx.BoxSizer(wx.HORIZONTAL) sizer_3 = wx.BoxSizer(wx.VERTICAL) sizer_2 = wx.BoxSizer(wx.HORIZONTAL) + sizer_2.Add(self.button_1, 0, 0, 0) + sizer_2.Add((20, 20), 0, 0, 0) label_1 = wx.StaticText(self, wx.ID_ANY, _("CAN Device")) sizer_2.Add(label_1, 0, 0, 0) sizer_2.Add((20, 20), 0, 0, 0) @@ -94,6 +99,10 @@ class BmsMonitorFrame(wx.Frame): self.Layout() # end wxGlade + def OnToggle(self, event): # wxGlade: BmsMonitorFrame. + print("Event handler 'OnToggle' not implemented!") + event.Skip() + # end of class BmsMonitorFrame class BmsApp(wx.App): diff --git a/BmsView.wxg b/BmsView.wxg index b2939e6..b7b9c75 100644 --- a/BmsView.wxg +++ b/BmsView.wxg @@ -1,5 +1,5 @@ - + @@ -20,6 +20,24 @@ wxEXPAND wxHORIZONTAL + + + 0 + + + OnToggle + + + + + + + 0 + + 20 + 20 + + 0