Browse Source

Error handling when sending commands

Before now, if an error occurred when sending a command, it passed
silently.  This is a Bad Thing™, so it's been fixed.  Now if there's an
error, an error dialog appears with some information about what
happened.  If the error occurred on the Sink page, the program goes back
to the device list after the error dialog is closed.
Clara Hobbs 7 years ago
parent
commit
73bc4ce454
1 changed files with 49 additions and 19 deletions
  1. 49
    19
      pd-buddy-gtk.py

+ 49
- 19
pd-buddy-gtk.py View File

@@ -9,21 +9,29 @@ gi.require_version('Gtk', '3.0')
9 9
 from gi.repository import Gtk, Gio, GObject, GLib
10 10
 
11 11
 
12
-def pdb_send_message(sp, message):
12
+def pdb_send_message(window, sp, message):
13 13
     """Send a message over the serial port and return the response"""
14
-    # Open the serial port
15
-    # FIXME handle exceptions
16
-    sp = serial.Serial(sp.device, baudrate=115200, timeout=0.01)
14
+    try:
15
+        # Open the serial port
16
+        sp = serial.Serial(sp.device, baudrate=115200, timeout=0.01)
17 17
 
18
-    sp.write(bytes(message, 'utf-8') + b'\r\n')
19
-    sp.flush()
20
-    answer = sp.readlines()
18
+        sp.write(bytes(message, 'utf-8') + b'\r\n')
19
+        sp.flush()
20
+        answer = sp.readlines()
21 21
 
22
-    sp.close()
22
+        sp.close()
23 23
 
24
-    # Remove the echoed command and prompt
25
-    answer = answer[1:-1]
26
-    return answer
24
+        # Remove the echoed command and prompt
25
+        answer = answer[1:-1]
26
+        return answer
27
+    except OSError as e:
28
+        dialog = Gtk.MessageDialog(window, 0, Gtk.MessageType.ERROR,
29
+                Gtk.ButtonsType.CLOSE, "Error communicating with device")
30
+        dialog.format_secondary_text(e.strerror)
31
+        dialog.run()
32
+
33
+        dialog.destroy()
34
+        raise
27 35
 
28 36
 
29 37
 class ListRowModel(GObject.GObject):
@@ -139,7 +147,11 @@ class SelectListRow(Gtk.ListBoxRow):
139 147
         self.show_all()
140 148
 
141 149
     def on_identify_clicked(self, button):
142
-        pdb_send_message(self.model.serport, 'identify')
150
+        window = self.get_toplevel()
151
+        try:
152
+            pdb_send_message(window, self.model.serport, 'identify')
153
+        except:
154
+            return
143 155
 
144 156
 
145 157
 class Handler:
@@ -172,8 +184,12 @@ class Handler:
172 184
 
173 185
         self.serial_port = serport
174 186
 
175
-        pdb_send_message(self.serial_port, 'load')
176
-        tmpcfg = pdb_send_message(self.serial_port, 'get_tmpcfg')
187
+        window = self.builder.get_object("pdb-window")
188
+        try:
189
+            pdb_send_message(window, self.serial_port, 'load')
190
+            tmpcfg = pdb_send_message(window, self.serial_port, 'get_tmpcfg')
191
+        except:
192
+            return
177 193
 
178 194
         # Get voltage and current from device and load them into the GUI
179 195
         for line in tmpcfg:
@@ -205,6 +221,8 @@ class Handler:
205 221
         st.set_visible_child(sink)
206 222
 
207 223
     def on_header_sink_back_clicked(self, data):
224
+        self.serial_port = None
225
+
208 226
         # Show the Select page
209 227
         hst = self.builder.get_object("header-stack")
210 228
         hselect = self.builder.get_object("header-select")
@@ -218,7 +236,11 @@ class Handler:
218 236
         self._store_device_settings()
219 237
         self._set_save_button_visibility()
220 238
 
221
-        pdb_send_message(self.serial_port, 'write')
239
+        window = self.builder.get_object("pdb-window")
240
+        try:
241
+            pdb_send_message(window, self.serial_port, 'write')
242
+        except:
243
+            self.on_header_sink_back_clicked(None)
222 244
 
223 245
     def _store_device_settings(self):
224 246
         """Store the settings that were loaded from the device"""
@@ -244,14 +266,22 @@ class Handler:
244 266
     def on_voltage_combobox_changed(self, combo):
245 267
         self._set_save_button_visibility()
246 268
 
247
-        pdb_send_message(self.serial_port,
248
-                         'set_v {}'.format(int(combo.get_active_text())*1000))
269
+        window = self.builder.get_object("pdb-window")
270
+        try:
271
+            pdb_send_message(window, self.serial_port,
272
+                             'set_v {}'.format(int(combo.get_active_text())*1000))
273
+        except:
274
+            self.on_header_sink_back_clicked(None)
249 275
 
250 276
     def on_current_spinbutton_changed(self, spin):
251 277
         self._set_save_button_visibility()
252 278
 
253
-        pdb_send_message(self.serial_port,
254
-                         'set_i {}'.format(int(spin.get_value()*1000)))
279
+        window = self.builder.get_object("pdb-window")
280
+        try:
281
+            pdb_send_message(window, self.serial_port,
282
+                             'set_i {}'.format(int(spin.get_value()*1000)))
283
+        except:
284
+            self.on_header_sink_back_clicked(None)
255 285
 
256 286
 class Application(Gtk.Application):
257 287
 

Loading…
Cancel
Save