connect to DAC
This commit is contained in:
parent
8f69f86a71
commit
fc3f677116
|
@ -3,8 +3,61 @@ gi.require_version("Gtk", "3.0")
|
|||
from gi.repository import Gtk
|
||||
import serial
|
||||
import serial.tools.list_ports
|
||||
import re
|
||||
|
||||
def error_popup(parent, msg):
|
||||
box = Gtk.MessageDialog(
|
||||
parent=parent,
|
||||
modal=True,
|
||||
destroy_with_parent=True,
|
||||
message_type=Gtk.MessageType.ERROR,
|
||||
buttons=Gtk.ButtonsType.CLOSE,
|
||||
text = msg
|
||||
)
|
||||
box.connect("response", lambda _0, _1: box.close())
|
||||
box.show_all()
|
||||
|
||||
class SerComm:
|
||||
def error_popup(self, x):
|
||||
error_popup(self.getobj("win"), x)
|
||||
|
||||
def get_or_to(self):
|
||||
s = self.ser.read_until().decode()
|
||||
if not s:
|
||||
raise Exception(f"DAC timeout: '{s}'")
|
||||
return s.rstrip("\r\n")
|
||||
|
||||
def connect(self):
|
||||
s = self.sel_box.get_active_id()
|
||||
if self.ser.is_open:
|
||||
self.ser.close()
|
||||
|
||||
if s == None:
|
||||
self.getobj("dac_idn").set_label("(disconnected)")
|
||||
return None
|
||||
|
||||
self.ser.port = s
|
||||
self.ser.open()
|
||||
|
||||
while True:
|
||||
s = self.get_or_to()
|
||||
if s == "INITIALIZATION COMPLETE":
|
||||
break
|
||||
|
||||
loc = re.search(r'[0-9]', s)
|
||||
if loc is None:
|
||||
raise Exception(f"Unknown message from DAC: '{s}'")
|
||||
self.getobj(f"Ch{int(loc.group(0)) + 1}").set_label('<span color="red">ERR</span>')
|
||||
|
||||
self.ser.write(b"*IDN?\r\n")
|
||||
self.getobj("dac_idn").set_label(self.get_or_to())
|
||||
|
||||
def on_connect(self, _):
|
||||
try:
|
||||
self.connect()
|
||||
except Exception as e:
|
||||
self.error_popup(str(e))
|
||||
|
||||
def refresh(self, _):
|
||||
# Does this cause a memory error? This is a GTK interned string
|
||||
curid = self.sel_box.get_active_id()
|
||||
|
@ -12,6 +65,7 @@ class SerComm:
|
|||
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":
|
||||
|
@ -24,11 +78,15 @@ class SerComm:
|
|||
|
||||
def __init__(self, getobj):
|
||||
self.getobj = getobj
|
||||
self.ser = serial.Serial()
|
||||
self.ser = serial.Serial(timeout=3)
|
||||
self.ser.baudrate = 115200
|
||||
|
||||
self.sel_box = getobj("sel_box")
|
||||
|
||||
getobj("sel_ref").connect("clicked", self.refresh)
|
||||
self.refresh(None)
|
||||
getobj("sel_do").connect("clicked", self.on_connect)
|
||||
|
||||
if __name__ == "__main__":
|
||||
builder = Gtk.Builder()
|
||||
builder.add_from_file("gui.glade")
|
||||
|
@ -36,7 +94,6 @@ if __name__ == "__main__":
|
|||
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