diff options
author | Keith Packard <keithp@keithp.com> | 2011-11-12 18:10:18 -0800 |
---|---|---|
committer | Keith Packard <keithp@keithp.com> | 2011-11-12 18:10:18 -0800 |
commit | f6db11c3c87725c809c518f5f19b07325faf9c84 (patch) | |
tree | 0a78d71cc8589c02ad27b7385fa018b848ea9d3d /altosui/AltosDebug.java | |
parent | b132eefc5f63412bb4a98a4bb72b9055e40d5d42 (diff) |
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 <keithp@keithp.com>
Diffstat (limited to 'altosui/AltosDebug.java')
-rw-r--r-- | altosui/AltosDebug.java | 13 |
1 files changed, 11 insertions, 2 deletions
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 |