Add serial searching

I am not quite sure how to structure this program. I put the serial
object in the Application object, but it seems that over time the
MainWin class will just have a million initialization parameters.
This commit is contained in:
Peter McGoron 2021-08-12 12:05:26 -04:00
parent 1786411365
commit b10c5c075b
4 changed files with 66 additions and 27 deletions

View File

@ -1,6 +1,4 @@
import wx from app import App
import mainwin
app = wx.App(False) app = App()
mainwin.MainWin()
app.MainLoop() app.MainLoop()

14
app.py Normal file
View File

@ -0,0 +1,14 @@
import wx
import serial
from mainwin import MainWin
class App(wx.App):
def serial_setup(self, port):
self.ser.port = port
print(self.ser.port)
def __init__(self):
wx.App.__init__(self, False)
self.ser = serial.Serial(baudrate=115200)
self.win = MainWin(self.serial_setup)

View File

@ -1,31 +1,16 @@
import wx import wx
from sercomm import SerialMenu
import serial
class MainWin(wx.Frame): class MainWin(wx.Frame):
def __init__(self): def __init__(self, serconn):
wx.Frame.__init__(self, None, wx.ID_ANY, "Dagon Controller") wx.Frame.__init__(self, None, wx.ID_ANY, "Dagon Controller")
self.main_sizer = wx.BoxSizer(wx.VERTICAL) self.ser = serial.Serial(baudrate=115200)
# Top buttons serial_menu = SerialMenu(serconn)
self.top_sizer = wx.BoxSizer(wx.HORIZONTAL) menubar = wx.MenuBar()
self.plan_btn = wx.Button(self, label="Plan", menubar.Append(serial_menu, "&Serial")
style=wx.BU_EXACTFIT) self.SetMenuBar(menubar)
self.conn_btn = wx.Button(self, label="(Re)Connect",
style=wx.BU_EXACTFIT)
self.ser_sel = wx.Choice(self, choices=["(none)"])
for i in (self.plan_btn, self.conn_btn):
self.top_sizer.Add(i, 0, wx.EXPAND)
self.top_sizer.Add(self.ser_sel, 1, wx.EXPAND)
self.main_sizer.Add(self.top_sizer, 0, wx.EXPAND)
self.commbox = wx.TextCtrl(self, style=
wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_RICH2)
self.main_sizer.Add(self.commbox, 1, wx.EXPAND)
self.SetSizer(self.main_sizer)
self.SetAutoLayout(1)
self.main_sizer.Fit(self)
self.Show(True) self.Show(True)

42
sercomm.py Normal file
View File

@ -0,0 +1,42 @@
import wx
import serial
from serial.tools.list_ports import comports
def repr_and_name(v):
s = v.device
for i in (v.name, v.description, v.hwid):
if i != "n/a":
s = f"{s} - {i}"
return (v.device, s)
def repr_and_name_list():
return [repr_and_name(x) for x in comports()]
class SerialSelector(wx.MenuItem):
def __init__(self, parent, port, disp):
wx.MenuItem.__init__(self, parent, wx.ID_ANY, disp)
self.port = port
class SerialMenu(wx.Menu):
def _refresh_devices(self, _):
for i in self.devices:
self.Bind(wx.EVT_MENU, None, i)
self.Delete(i)
self.devices = []
for (nm, listing) in repr_and_name_list():
x = SerialSelector(self, nm, listing)
self.devices.append(x)
self.Append(x)
self.Bind(wx.EVT_MENU, lambda _ : self.serconn(x.port),
x)
def __init__(self, serconn):
wx.Menu.__init__(self)
self.invar = [
self.Append(wx.ID_ANY, "&Refresh", "Refresh devices"),
self.AppendSeparator()
]
self.Bind(wx.EVT_MENU, self._refresh_devices, self.invar[0])
self.devices = []
self.serconn = serconn
self._refresh_devices(None)