From: Keith Packard Date: Sun, 13 Nov 2011 02:10:18 +0000 (-0800) Subject: altosui: Deal with serial port exceptions a bit better X-Git-Tag: 1.0.9.4~85 X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=commitdiff_plain;h=f6db11c3c87725c809c518f5f19b07325faf9c84 altosui: Deal with serial port exceptions a bit better This catches a few exceptions and tries to make sure the serial port is closed afterwards. Signed-off-by: Keith Packard --- diff --git a/altosui/AltosDebug.java b/altosui/AltosDebug.java index d18de80d..ce1cf5dd 100644 --- a/altosui/AltosDebug.java +++ b/altosui/AltosDebug.java @@ -163,7 +163,11 @@ public class AltosDebug extends AltosSerial { int i = 0; byte[] data = new byte[length]; while (i < length) { - String line = get_reply().trim(); + String line = get_reply(); + + if (line == null) + throw new IOException("Timeout in read_bytes"); + line = line.trim(); String tokens[] = line.split("\\s+"); for (int j = 0; j < tokens.length; j++) { if (!Altos.ishex(tokens[j]) || @@ -172,7 +176,12 @@ public class AltosDebug extends AltosSerial { String.format ("Invalid read_bytes reply \"%s\"", line)); try { - data[i + j] = (byte) Integer.parseInt(tokens[j], 16); + if (i + j >= length) + throw new IOException( + String.format + ("Invalid read_bytes reply \"%s\"", line)); + else + data[i + j] = (byte) Integer.parseInt(tokens[j], 16); } catch (NumberFormatException ne) { throw new IOException( String.format diff --git a/altosui/AltosFlashUI.java b/altosui/AltosFlashUI.java index 39151641..704ce35c 100644 --- a/altosui/AltosFlashUI.java +++ b/altosui/AltosFlashUI.java @@ -247,7 +247,7 @@ public class AltosFlashUI flash.set_romconfig(ui.rom_config); flash.flash(); } - } catch (Exception ee) { + } catch (InterruptedException ee) { final Exception e = ee; System.out.printf("exception %s\n", e.toString()); SwingUtilities.invokeLater(new Runnable() { @@ -255,9 +255,26 @@ public class AltosFlashUI ui.exception(e); } }); + } catch (IOException ee) { + final Exception e = ee; + System.out.printf("exception %s\n", e.toString()); + SwingUtilities.invokeLater(new Runnable() { + public void run() { + ui.exception(e); + } + }); + } catch (AltosSerialInUseException ee) { + final Exception e = ee; + System.out.printf("exception %s\n", e.toString()); + SwingUtilities.invokeLater(new Runnable() { + public void run() { + ui.exception(e); + } + }); + } finally { + if (flash != null) + flash.close(); } - if (flash != null) - flash.close(); } public flash_task(AltosFlashUI in_ui) {