@@ -106,36 +106,40 @@ public boolean uploadUsingPreferences(File sourcePath, String buildPath, String
106106 t = prefs .get ("upload.wait_for_upload_port" );
107107 boolean waitForUploadPort = (t != null ) && t .equals ("true" );
108108
109- String uploadPort = prefs .getOrExcept ("serial.port" );
109+ String userSelectedUploadPort = prefs .getOrExcept ("serial.port" );
110+ String actualUploadPort = null ;
110111
111112 if (doTouch ) {
112113 try {
113114 // Toggle 1200 bps on selected serial port to force board reset.
114115 List <String > before = Serial .list ();
115- if (before .contains (uploadPort )) {
116+ if (before .contains (userSelectedUploadPort )) {
116117 if (verbose )
117118 System .out .println (
118- I18n .format (_ ("Forcing reset using 1200bps open/close on port {0}" ), uploadPort ));
119- Serial .touchForCDCReset (uploadPort );
119+ I18n .format (_ ("Forcing reset using 1200bps open/close on port {0}" ), userSelectedUploadPort ));
120+ Serial .touchForCDCReset (userSelectedUploadPort );
120121 }
121122 Thread .sleep (400 );
122123 if (waitForUploadPort ) {
123124 // Scanning for available ports seems to open the port or
124125 // otherwise assert DTR, which would cancel the WDT reset if
125126 // it happened within 250 ms. So we wait until the reset should
126127 // have already occured before we start scanning.
127- uploadPort = waitForUploadPort (uploadPort , before );
128+ actualUploadPort = waitForUploadPort (userSelectedUploadPort , before );
128129 }
129130 } catch (SerialException e ) {
130131 throw new RunnerException (e );
131132 } catch (InterruptedException e ) {
132133 throw new RunnerException (e .getMessage ());
133134 }
134- prefs .put ("serial.port" , uploadPort );
135- if (uploadPort .startsWith ("/dev/" )) {
136- prefs .put ("serial.port.file" , uploadPort .substring (5 ));
135+ if (actualUploadPort == null ) {
136+ actualUploadPort = userSelectedUploadPort ;
137+ }
138+ prefs .put ("serial.port" , actualUploadPort );
139+ if (actualUploadPort .startsWith ("/dev/" )) {
140+ prefs .put ("serial.port.file" , actualUploadPort .substring (5 ));
137141 } else {
138- prefs .put ("serial.port.file" , uploadPort );
142+ prefs .put ("serial.port.file" , actualUploadPort );
139143 }
140144 }
141145
@@ -158,9 +162,9 @@ public boolean uploadUsingPreferences(File sourcePath, String buildPath, String
158162 throw new RunnerException (e );
159163 }
160164
165+ String finalUploadPort = null ;
161166 if (uploadResult && doTouch ) {
162167 try {
163- String previousUploadPort = PreferencesData .get ("serial.port" );
164168 if (waitForUploadPort ) {
165169 // For Due/Leonardo wait until the bootloader serial port disconnects and the
166170 // sketch serial port reconnects (or timeout after a few seconds if the
@@ -170,7 +174,11 @@ public boolean uploadUsingPreferences(File sourcePath, String buildPath, String
170174 long started = System .currentTimeMillis ();
171175 while (System .currentTimeMillis () - started < 2000 ) {
172176 List <String > portList = Serial .list ();
173- if (portList .contains (previousUploadPort )) {
177+ if (portList .contains (actualUploadPort )) {
178+ finalUploadPort = actualUploadPort ;
179+ break ;
180+ } else if (portList .contains (userSelectedUploadPort )) {
181+ finalUploadPort = userSelectedUploadPort ;
174182 break ;
175183 }
176184 Thread .sleep (250 );
@@ -181,7 +189,10 @@ public boolean uploadUsingPreferences(File sourcePath, String buildPath, String
181189 }
182190 }
183191
184- BaseNoGui .selectSerialPort (uploadPort );
192+ if (finalUploadPort == null ) {
193+ finalUploadPort = actualUploadPort ;
194+ }
195+ BaseNoGui .selectSerialPort (finalUploadPort );
185196 return uploadResult ;
186197 }
187198
0 commit comments