From cede26883e8b94ee62d72d3f2936eeecf02ffca4 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 9 Nov 2018 16:15:47 +0100 Subject: [PATCH] Fix 'Select port on upload' error message --- app/src/processing/app/Editor.java | 2 + app/src/processing/app/SketchController.java | 4 -- .../src/cc/arduino/UploaderUtils.java | 3 - .../packages/uploaders/SerialUploader.java | 57 +++++++++---------- build/shared/lib/preferences.txt | 4 +- 5 files changed, 30 insertions(+), 40 deletions(-) diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index cfaea455e..fef31f2c0 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -2345,6 +2345,8 @@ public class Editor extends JFrame implements RunnerListener { SwingUtilities.invokeLater(() -> statusError(tr("Error while burning bootloader."))); // error message will already be visible } + } catch (SerialNotFoundException e) { + SwingUtilities.invokeLater(() -> statusError(tr("Error while burning bootloader: please select a serial port."))); } catch (PreferencesMapException e) { SwingUtilities.invokeLater(() -> { statusError(I18n.format( diff --git a/app/src/processing/app/SketchController.java b/app/src/processing/app/SketchController.java index 392422c59..89f1a3516 100644 --- a/app/src/processing/app/SketchController.java +++ b/app/src/processing/app/SketchController.java @@ -709,10 +709,6 @@ public class SketchController { UploaderUtils uploaderInstance = new UploaderUtils(); Uploader uploader = uploaderInstance.getUploaderByPreferences(false); - if (uploader == null) { - editor.statusError(tr("Please select a Port before Upload")); - return false; - } EditorConsole.setCurrentEditorConsole(editor.console); diff --git a/arduino-core/src/cc/arduino/UploaderUtils.java b/arduino-core/src/cc/arduino/UploaderUtils.java index 9987354a3..875f41d76 100644 --- a/arduino-core/src/cc/arduino/UploaderUtils.java +++ b/arduino-core/src/cc/arduino/UploaderUtils.java @@ -48,9 +48,6 @@ public class UploaderUtils { BoardPort boardPort = null; if (!noUploadPort) { String port = PreferencesData.get("serial.port"); - if (port == null || port.isEmpty()) { - return null; - } boardPort = BaseNoGui.getDiscoveryManager().find(port); } diff --git a/arduino-core/src/cc/arduino/packages/uploaders/SerialUploader.java b/arduino-core/src/cc/arduino/packages/uploaders/SerialUploader.java index 9d6c29eaa..4a322fcbe 100644 --- a/arduino-core/src/cc/arduino/packages/uploaders/SerialUploader.java +++ b/arduino-core/src/cc/arduino/packages/uploaders/SerialUploader.java @@ -41,6 +41,7 @@ import cc.arduino.packages.BoardPort; import processing.app.debug.RunnerException; import processing.app.debug.TargetPlatform; import processing.app.helpers.PreferencesMap; +import processing.app.helpers.PreferencesMapException; import processing.app.helpers.StringReplacer; import java.io.File; @@ -105,17 +106,11 @@ public class SerialUploader extends Uploader { else prefs.put("upload.verify", prefs.get("upload.params.noverify", "")); - boolean uploadResult; try { - String pattern = prefs.getOrExcept("upload.pattern"); - String[] cmd = StringReplacer.formatAndSplit(pattern, prefs); - uploadResult = executeUploadCommand(cmd); - } catch (Exception e) { - throw new RunnerException(e); + return runCommand("upload.pattern", prefs); } finally { BaseNoGui.getDiscoveryManager().getSerialDiscoverer().pausePolling(false); } - return uploadResult; } // need to do a little dance for Leonardo and derivatives: @@ -127,7 +122,7 @@ public class SerialUploader extends Uploader { boolean doTouch = prefs.getBoolean("upload.use_1200bps_touch"); boolean waitForUploadPort = prefs.getBoolean("upload.wait_for_upload_port"); - String userSelectedUploadPort = prefs.getOrExcept("serial.port"); + String userSelectedUploadPort = prefs.get("serial.port", ""); String actualUploadPort = null; if (doTouch) { @@ -177,7 +172,7 @@ public class SerialUploader extends Uploader { Thread.sleep(100); } - BoardPort boardPort = BaseNoGui.getDiscoveryManager().find(PreferencesData.get("serial.port")); + BoardPort boardPort = BaseNoGui.getDiscoveryManager().find(PreferencesData.get("serial.port", "")); try { prefs.put("serial.port.iserial", boardPort.getPrefs().getOrExcept("iserial")); } catch (Exception e) { @@ -199,13 +194,7 @@ public class SerialUploader extends Uploader { boolean uploadResult; try { - String pattern = prefs.getOrExcept("upload.pattern"); - String[] cmd = StringReplacer.formatAndSplit(pattern, prefs); - uploadResult = executeUploadCommand(cmd); - } catch (RunnerException e) { - throw e; - } catch (Exception e) { - throw new RunnerException(e); + uploadResult = runCommand("upload.pattern", prefs); } finally { BaseNoGui.getDiscoveryManager().getSerialDiscoverer().pausePolling(false); } @@ -328,15 +317,7 @@ public class SerialUploader extends Uploader { else prefs.put("program.verify", prefs.get("program.params.noverify", "")); - try { - String pattern = prefs.getOrExcept("program.pattern"); - String[] cmd = StringReplacer.formatAndSplit(pattern, prefs); - return executeUploadCommand(cmd); - } catch (RunnerException e) { - throw e; - } catch (Exception e) { - throw new RunnerException(e); - } + return runCommand("program.pattern", prefs); } @Override @@ -393,13 +374,27 @@ public class SerialUploader extends Uploader { new LoadVIDPIDSpecificPreferences().load(prefs); - String pattern = prefs.getOrExcept("erase.pattern"); - String[] cmd = StringReplacer.formatAndSplit(pattern, prefs); - if (!executeUploadCommand(cmd)) + if (!runCommand("erase.pattern", prefs)) return false; - pattern = prefs.getOrExcept("bootloader.pattern"); - cmd = StringReplacer.formatAndSplit(pattern, prefs); - return executeUploadCommand(cmd); + return runCommand("bootloader.pattern", prefs); + } + + private boolean runCommand(String patternKey, PreferencesMap prefs) throws Exception, RunnerException { + try { + String pattern = prefs.getOrExcept(patternKey); + StringReplacer.checkIfRequiredKeyIsMissingOrExcept("serial.port", pattern, prefs); + String[] cmd = StringReplacer.formatAndSplit(pattern, prefs); + return executeUploadCommand(cmd); + } catch (RunnerException e) { + throw e; + } catch (PreferencesMapException e) { + if (e.getMessage().equals("serial.port")) { + throw new SerialNotFoundException(e); + } + throw e; + } catch (Exception e) { + throw new RunnerException(e); + } } } diff --git a/build/shared/lib/preferences.txt b/build/shared/lib/preferences.txt index 7e7fd7c38..156775c1d 100644 --- a/build/shared/lib/preferences.txt +++ b/build/shared/lib/preferences.txt @@ -267,8 +267,8 @@ programmer = arduino:avrispmkii upload.using = bootloader upload.verify = true -#default port is empty to prevent running AVRDUDE before Port selected (issue #7943) -serial.port= +# default port is not defined to prevent running AVRDUDE before Port selected (issue #7943) +#serial.port= serial.databits=8 serial.stopbits=1 serial.parity=N