serial: search for serial connections
This commit is contained in:
parent
a920002d54
commit
8f69f86a71
|
@ -1,10 +1,42 @@
|
|||
import gi
|
||||
gi.require_version("Gtk", "3.0")
|
||||
from gi.repository import Gtk
|
||||
import serial
|
||||
import serial.tools.list_ports
|
||||
|
||||
class SerComm:
|
||||
def refresh(self, _):
|
||||
# Does this cause a memory error? This is a GTK interned string
|
||||
curid = self.sel_box.get_active_id()
|
||||
|
||||
self.sel_box.remove_all()
|
||||
self.sel_box.append(None, "(select device)")
|
||||
self.sel_box.set_active(0)
|
||||
for (nm, desc, id) in serial.tools.list_ports.comports():
|
||||
s = nm
|
||||
if desc != "n/a":
|
||||
s = s + f" - {desc}"
|
||||
if id != "n/a":
|
||||
s = s + f" - {id}"
|
||||
self.sel_box.append(nm, s)
|
||||
if curid == nm:
|
||||
self.sel_box.set_active_id(nm)
|
||||
|
||||
def __init__(self, getobj):
|
||||
self.getobj = getobj
|
||||
self.ser = serial.Serial()
|
||||
self.ser.baudrate = 115200
|
||||
|
||||
self.sel_box = getobj("sel_box")
|
||||
|
||||
if __name__ == "__main__":
|
||||
builder = Gtk.Builder()
|
||||
builder.add_from_file("gui.glade")
|
||||
builder.get_object("win").connect("destroy", Gtk.main_quit)
|
||||
builder.get_object("win").show_all()
|
||||
|
||||
sc = SerComm(builder.get_object)
|
||||
builder.get_object("sel_ref").connect("clicked", sc.refresh)
|
||||
sc.refresh(None)
|
||||
|
||||
Gtk.main()
|
||||
|
|
Reference in New Issue