altosuilib: Wait for Windows to get ready with new device
[fw/altos] / altosuilib / AltosFlashUI.java
index ffb39b8c2aae4e4e7b2035a9e780355114808bf8..7186e91b33024f14fc7c1e21d3c0dda5f99a6cfe 100644 (file)
@@ -319,7 +319,7 @@ public class AltosFlashUI
                                return false;
                }
 
-               if (existing_config.radio_calibration_broken) {
+               if (existing_config != null && existing_config.radio_calibration_broken) {
                        int ret = JOptionPane.showConfirmDialog(this,
                                                                String.format("Radio calibration value %d may be incorrect\nFlash anyways?",
                                                                              existing_config.radio_calibration),
@@ -481,12 +481,24 @@ public class AltosFlashUI
                public void run () {
                        try {
                                AltosLink link = null;
+                               boolean new_device = false;
 
                                for (;;) {
                                        System.out.printf("Attempting to open %s\n", device.toShortString());
 
-                                       link = new AltosSerial(device);
+                                       for (int i = 0; i < 20; i++) {
+                                               link = new AltosSerial(device);
 
+                                               if (link != null)
+                                                       break;
+
+                                               if (!new_device)
+                                                       break;
+
+                                               System.out.printf("Waiting for device to become ready\n");
+
+                                               Thread.sleep(1000);
+                                       }
                                        if (link == null)
                                                throw new IOException(String.format("%s: open failed",
                                                                                    device.toShortString()));
@@ -536,6 +548,7 @@ public class AltosFlashUI
                                                        if (!matched) {
                                                                System.out.printf("Identified new device %s\n", d.toShortString());
                                                                device = (AltosUSBDevice) d;
+                                                               new_device = true;
                                                                break;
                                                        }
                                                }