43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
|
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)
|