Skip to content

Commit 7eb76d1

Browse files
committed
Made some changes to the upload process in regards to issue #209
I simplified the upload process in case of "looking for new com port" and most of all added lots of comments to the console so we can better analize problems.
1 parent 714b132 commit 7eb76d1

File tree

2 files changed

+65
-42
lines changed

2 files changed

+65
-42
lines changed

it.baeyens.arduino.core/src/it/baeyens/arduino/communication/ArduinoSerial.java

+64-41
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.eclipse.core.resources.IProject;
1515
import org.eclipse.core.runtime.IStatus;
1616
import org.eclipse.core.runtime.Status;
17+
import org.eclipse.ui.console.MessageConsoleStream;
1718

1819
public class ArduinoSerial {
1920
/**
@@ -53,54 +54,55 @@ public static boolean reset_Arduino_by_baud_rate(String ComPort, int baudrate, l
5354
* The port to return if no new com port is found
5455
* @return the new comport if found else the defaultComPort
5556
*/
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) {
5758

5859
Vector<String> NewPorts;
59-
Vector<String> OriginalPortsCopy;
60-
60+
Vector<String> NewPortsCopy;
6161

6262
// wait for port to disappear
6363
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;
6666
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+
7968
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));
8273
}
8374

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 */
8690

87-
NumTries = 0;
88-
do {
8991
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");
9193
return defaultComPort;
9294
}
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+
}
101102
}
102-
} while (NewPorts.size() != 1);
103-
return NewPorts.get(0);
103+
} while (NewPortsCopy.size() == 0);
104+
105+
return NewPortsCopy.get(0);
104106
}
105107

106108
/**
@@ -161,60 +163,81 @@ public static void flushSerialBuffer(Serial serialPort) {
161163
* The name of the com port to reset
162164
* @return The com port to upload to
163165
*/
164-
public static String makeArduinoUploadready(IProject project, String configName, String ComPort) {
166+
public static String makeArduinoUploadready(MessageConsoleStream console, IProject project, String configName, String ComPort) {
165167
// 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");
167170
boolean bDisableFlushing = Common.getBuildEnvironmentVariable(project, configName, ArduinoConst.ENV_KEY_upload_disable_flushing, "false")
168171
.equalsIgnoreCase("true");
169172
boolean bwait_for_upload_port = Common.getBuildEnvironmentVariable(project, configName, ArduinoConst.ENV_KEY_wait_for_upload_port, "false")
170173
.equalsIgnoreCase("true");
171174
String boardName = Common.getBuildEnvironmentVariable(project, configName, ArduinoConst.ENV_KEY_JANTJE_BOARD_NAME, "");
172175

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");
175182
Vector<String> OriginalPorts = Serial.list();
176-
// OriginalPorts.remove(ComPort);
177183

178184
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");
179186
// Give the DUE/DigiX Atmel SAM-BA bootloader time to switch-in after the reset
180187
try {
181188
Thread.sleep(2000);
182189
} catch (InterruptedException ex) {
183190
// ignore error
184191
}
192+
console.println("Continuing to use " + ComPort);
193+
console.println("Ending reset using 1200bps touch process");
185194
return ComPort;
186195
}
187196
if (boardName.equalsIgnoreCase("Arduino leonardo") || boardName.equalsIgnoreCase("Arduino Micro")
188197
|| 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;
190202
}
191203
}
192204

193205
// connect to the serial port
206+
console.println("Starting reset using DTR toggle process");
194207
Serial serialPort;
195208
try {
196209
serialPort = new Serial(ComPort, 9600);
197210
} catch (Exception e) {
198211
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");
200216
return ComPort;
201217
// throw new RunnerException(e.getMessage());
202218
}
203219
if (!serialPort.IsConnected()) {
204220
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");
205224
return ComPort;
206225
}
207226

208227
if (!bDisableFlushing) {
209228
// Cleanup the serial buffer
229+
console.println("Flushing buffer");
210230
flushSerialBuffer(serialPort);// I wonder is this code on the right
211231
// place (I mean before the reset?;
212232
// shouldn't it be after?)
213233
}
214234
// reset arduino
235+
console.println("Toggling DTR");
215236
ToggleDTR(serialPort, 100);
216237

217238
serialPort.dispose();
239+
console.println("Continuing to use " + ComPort);
240+
console.println("Ending reset using DTR toggle process");
218241
return ComPort;
219242

220243
}

it.baeyens.arduino.core/src/it/baeyens/arduino/tools/uploaders/arduinoUploader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public boolean uploadUsingPreferences(IFile hexFile, IProject project, boolean u
3232
String boardName = Common.getBuildEnvironmentVariable(myProject, mycConf, ArduinoConst.ENV_KEY_JANTJE_BOARD_NAME, "");
3333
String NewComPort = MComPort;
3434
String command = Common.getBuildEnvironmentVariable(myProject, mycConf, "A.TOOLS." + myUploadTool.toUpperCase() + ".UPLOAD.PATTERN", "");
35-
NewComPort = ArduinoSerial.makeArduinoUploadready(myProject, mycConf, MComPort);
35+
NewComPort = ArduinoSerial.makeArduinoUploadready(myConsole.newMessageStream(), myProject, mycConf, MComPort);
3636

3737
command = command.replaceAll(" -P ", " -P " + NewComPort + " ");
3838
String nakedPort = NewComPort.replace("/dev/", "");

0 commit comments

Comments
 (0)