|
14 | 14 | import org.eclipse.core.resources.IProject;
|
15 | 15 | import org.eclipse.core.runtime.IStatus;
|
16 | 16 | import org.eclipse.core.runtime.Status;
|
| 17 | +import org.eclipse.ui.console.MessageConsoleStream; |
17 | 18 |
|
18 | 19 | public class ArduinoSerial {
|
19 | 20 | /**
|
@@ -53,54 +54,55 @@ public static boolean reset_Arduino_by_baud_rate(String ComPort, int baudrate, l
|
53 | 54 | * The port to return if no new com port is found
|
54 | 55 | * @return the new comport if found else the defaultComPort
|
55 | 56 | */
|
56 |
| - public static String wait_for_com_Port_to_appear(Vector<String> OriginalPorts, String defaultComPort) { |
| 57 | + public static String wait_for_com_Port_to_appear(MessageConsoleStream console, Vector<String> OriginalPorts, String defaultComPort) { |
57 | 58 |
|
58 | 59 | Vector<String> NewPorts;
|
59 |
| - Vector<String> OriginalPortsCopy; |
60 |
| - |
| 60 | + Vector<String> NewPortsCopy; |
61 | 61 |
|
62 | 62 | // wait for port to disappear
|
63 | 63 | int NumTries = 0;
|
64 |
| - int MaxTries = 200; // wait for 2 seconds, leaves us 6secs in case we are not seeing disappearing ports but reset worked |
65 |
| - int delayMs = 10; // on faster computers Esplora reconnects *extremely* quickly and we can't catch this |
| 64 | + int MaxTries = 20; // wait for 2 seconds, leaves us 6secs in case we are not seeing disappearing ports but reset worked |
| 65 | + int delayMs = 100; |
66 | 66 | do {
|
67 |
| - if (NumTries > 0) { |
68 |
| - try { |
69 |
| - Thread.sleep(delayMs); |
70 |
| - } catch (InterruptedException e) {// Jaba is not going to write this |
71 |
| - // code |
72 |
| - } |
73 |
| - } |
74 |
| - OriginalPortsCopy = new Vector<String>(OriginalPorts); |
75 |
| - if (NumTries++ > MaxTries) { |
76 |
| - Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID, "Leonardo upload port is not disappearing after reset and " + NumTries + " checks")); |
77 |
| - return defaultComPort; |
78 |
| - } |
| 67 | + |
79 | 68 | NewPorts = Serial.list();
|
80 |
| - for (int i = 0; i < NewPorts.size(); i++) { |
81 |
| - OriginalPortsCopy.remove(NewPorts.get(i)); |
| 69 | + |
| 70 | + NewPortsCopy = new Vector<String>(NewPorts); |
| 71 | + for (int i = 0; i < OriginalPorts.size(); i++) { |
| 72 | + NewPortsCopy.remove(OriginalPorts.get(i)); |
82 | 73 | }
|
83 | 74 |
|
84 |
| - } while (OriginalPortsCopy.size() != 1); |
85 |
| - OriginalPorts.remove(OriginalPortsCopy.get(0)); |
| 75 | + /* dump the serial ports to the console */ |
| 76 | + console.print("PORTS {"); |
| 77 | + for (int i = 0; i < OriginalPorts.size(); i++) { |
| 78 | + console.print(" " + OriginalPorts.get(i) + ","); |
| 79 | + } |
| 80 | + console.print("} / {"); |
| 81 | + for (int i = 0; i < NewPorts.size(); i++) { |
| 82 | + console.print(" " + NewPorts.get(i) + ","); |
| 83 | + } |
| 84 | + console.print("} => {"); |
| 85 | + for (int i = 0; i < NewPortsCopy.size(); i++) { |
| 86 | + console.print(" " + NewPortsCopy.get(i) + ","); |
| 87 | + } |
| 88 | + console.println("}"); |
| 89 | + /* end of dump to the console */ |
86 | 90 |
|
87 |
| - NumTries = 0; |
88 |
| - do { |
89 | 91 | if (NumTries++ > MaxTries) {
|
90 |
| - Common.log(new Status(IStatus.ERROR, ArduinoConst.CORE_PLUGIN_ID, "Leonardo upload port is not appearing after reset")); |
| 92 | + console.println("Comport is not behaving as expected"); |
91 | 93 | return defaultComPort;
|
92 | 94 | }
|
93 |
| - NewPorts = Serial.list(); |
94 |
| - for (int i = 0; i < OriginalPorts.size(); i++) { |
95 |
| - NewPorts.remove(OriginalPorts.get(i)); |
96 |
| - } |
97 |
| - try { |
98 |
| - Thread.sleep(delayMs); |
99 |
| - } catch (InterruptedException e) {// Jaba is not going to write this |
100 |
| - // code |
| 95 | + if (NewPortsCopy.size() == 0) // wait a while before we do the next try |
| 96 | + { |
| 97 | + try { |
| 98 | + Thread.sleep(delayMs); |
| 99 | + } catch (InterruptedException e) {// Jaba is not going to write this |
| 100 | + // code |
| 101 | + } |
101 | 102 | }
|
102 |
| - } while (NewPorts.size() != 1); |
103 |
| - return NewPorts.get(0); |
| 103 | + } while (NewPortsCopy.size() == 0); |
| 104 | + |
| 105 | + return NewPortsCopy.get(0); |
104 | 106 | }
|
105 | 107 |
|
106 | 108 | /**
|
@@ -161,60 +163,81 @@ public static void flushSerialBuffer(Serial serialPort) {
|
161 | 163 | * The name of the com port to reset
|
162 | 164 | * @return The com port to upload to
|
163 | 165 | */
|
164 |
| - public static String makeArduinoUploadready(IProject project, String configName, String ComPort) { |
| 166 | + public static String makeArduinoUploadready(MessageConsoleStream console, IProject project, String configName, String ComPort) { |
165 | 167 | // ArduinoProperties arduinoProperties = new ArduinoProperties(project);
|
166 |
| - String use_1200bps_touch = Common.getBuildEnvironmentVariable(project, configName, ArduinoConst.ENV_KEY_upload_use_1200bps_touch, "false"); |
| 168 | + boolean use_1200bps_touch = Common.getBuildEnvironmentVariable(project, configName, ArduinoConst.ENV_KEY_upload_use_1200bps_touch, "false") |
| 169 | + .equalsIgnoreCase("true"); |
167 | 170 | boolean bDisableFlushing = Common.getBuildEnvironmentVariable(project, configName, ArduinoConst.ENV_KEY_upload_disable_flushing, "false")
|
168 | 171 | .equalsIgnoreCase("true");
|
169 | 172 | boolean bwait_for_upload_port = Common.getBuildEnvironmentVariable(project, configName, ArduinoConst.ENV_KEY_wait_for_upload_port, "false")
|
170 | 173 | .equalsIgnoreCase("true");
|
171 | 174 | String boardName = Common.getBuildEnvironmentVariable(project, configName, ArduinoConst.ENV_KEY_JANTJE_BOARD_NAME, "");
|
172 | 175 |
|
173 |
| - if (boardName.equalsIgnoreCase("Arduino leonardo") || boardName.equalsIgnoreCase("Arduino Micro") |
174 |
| - || boardName.equalsIgnoreCase("Arduino Esplora") || boardName.startsWith("Arduino Due") || use_1200bps_touch.equalsIgnoreCase("true")) { |
| 176 | + if (use_1200bps_touch /* |
| 177 | + * || boardName.equalsIgnoreCase("Arduino leonardo") || boardName.equalsIgnoreCase("Arduino Micro") || |
| 178 | + * boardName.equalsIgnoreCase("Arduino Esplora") || boardName.startsWith("Arduino Due") |
| 179 | + */) { |
| 180 | + // Get the list of the current com serial ports |
| 181 | + console.println("Starting reset using 1200bps touch process"); |
175 | 182 | Vector<String> OriginalPorts = Serial.list();
|
176 |
| - // OriginalPorts.remove(ComPort); |
177 | 183 |
|
178 | 184 | if (!reset_Arduino_by_baud_rate(ComPort, 1200, 100) || boardName.startsWith("Arduino Due") || boardName.startsWith("Digistump DigiX")) {
|
| 185 | + console.println("reset using 1200bps touch failed"); |
179 | 186 | // Give the DUE/DigiX Atmel SAM-BA bootloader time to switch-in after the reset
|
180 | 187 | try {
|
181 | 188 | Thread.sleep(2000);
|
182 | 189 | } catch (InterruptedException ex) {
|
183 | 190 | // ignore error
|
184 | 191 | }
|
| 192 | + console.println("Continuing to use " + ComPort); |
| 193 | + console.println("Ending reset using 1200bps touch process"); |
185 | 194 | return ComPort;
|
186 | 195 | }
|
187 | 196 | if (boardName.equalsIgnoreCase("Arduino leonardo") || boardName.equalsIgnoreCase("Arduino Micro")
|
188 | 197 | || boardName.equalsIgnoreCase("Arduino Esplora") || bwait_for_upload_port) {
|
189 |
| - return wait_for_com_Port_to_appear(OriginalPorts, ComPort); |
| 198 | + String NewComport = wait_for_com_Port_to_appear(console, OriginalPorts, ComPort); |
| 199 | + console.println("Using comport " + NewComport + " from now onwards"); |
| 200 | + console.println("Ending reset using 1200bps touch process"); |
| 201 | + return NewComport; |
190 | 202 | }
|
191 | 203 | }
|
192 | 204 |
|
193 | 205 | // connect to the serial port
|
| 206 | + console.println("Starting reset using DTR toggle process"); |
194 | 207 | Serial serialPort;
|
195 | 208 | try {
|
196 | 209 | serialPort = new Serial(ComPort, 9600);
|
197 | 210 | } catch (Exception e) {
|
198 | 211 | e.printStackTrace();
|
199 |
| - Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID, "Unable to open Serial port " + ComPort, e)); |
| 212 | + Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID, "Exception while opening Serial port " + ComPort, e)); |
| 213 | + console.println("Exception while opening Serial port " + ComPort); |
| 214 | + console.println("Continuing to use " + ComPort); |
| 215 | + console.println("Ending reset using DTR toggle process"); |
200 | 216 | return ComPort;
|
201 | 217 | // throw new RunnerException(e.getMessage());
|
202 | 218 | }
|
203 | 219 | if (!serialPort.IsConnected()) {
|
204 | 220 | Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID, "Unable to open Serial port " + ComPort, null));
|
| 221 | + console.println("Unable to open Serial port " + ComPort); |
| 222 | + console.println("Continuing to use " + ComPort); |
| 223 | + console.println("Ending reset using DTR toggle process"); |
205 | 224 | return ComPort;
|
206 | 225 | }
|
207 | 226 |
|
208 | 227 | if (!bDisableFlushing) {
|
209 | 228 | // Cleanup the serial buffer
|
| 229 | + console.println("Flushing buffer"); |
210 | 230 | flushSerialBuffer(serialPort);// I wonder is this code on the right
|
211 | 231 | // place (I mean before the reset?;
|
212 | 232 | // shouldn't it be after?)
|
213 | 233 | }
|
214 | 234 | // reset arduino
|
| 235 | + console.println("Toggling DTR"); |
215 | 236 | ToggleDTR(serialPort, 100);
|
216 | 237 |
|
217 | 238 | serialPort.dispose();
|
| 239 | + console.println("Continuing to use " + ComPort); |
| 240 | + console.println("Ending reset using DTR toggle process"); |
218 | 241 | return ComPort;
|
219 | 242 |
|
220 | 243 | }
|
|
0 commit comments