Skip to content

Commit 5d9602a

Browse files
committed
Showing dialog when serial port not found on upload.
1 parent 1d13cd4 commit 5d9602a

File tree

6 files changed

+112
-28
lines changed

6 files changed

+112
-28
lines changed

app/src/processing/app/Editor.java

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -898,23 +898,7 @@ class SerialMenuListener implements ActionListener {
898898
//public SerialMenuListener() { }
899899

900900
public void actionPerformed(ActionEvent e) {
901-
if(serialMenu == null) {
902-
System.out.println("serialMenu is null");
903-
return;
904-
}
905-
int count = serialMenu.getItemCount();
906-
for (int i = 0; i < count; i++) {
907-
((JCheckBoxMenuItem)serialMenu.getItem(i)).setState(false);
908-
}
909-
JCheckBoxMenuItem item = (JCheckBoxMenuItem)e.getSource();
910-
item.setState(true);
911-
String name = item.getText();
912-
//System.out.println(item.getLabel());
913-
Preferences.set("serial.port", name);
914-
serialMonitor.closeSerialPort();
915-
serialMonitor.setVisible(false);
916-
serialMonitor = new SerialMonitor(Preferences.get("serial.port"));
917-
//System.out.println("set to " + get("serial.port"));
901+
selectSerialPort(((JCheckBoxMenuItem)e.getSource()).getText());
918902
}
919903

920904
/*
@@ -929,7 +913,35 @@ public void actionPerformed(ActionEvent e) {
929913
*/
930914
}
931915

932-
916+
protected void selectSerialPort(String name) {
917+
if(serialMenu == null) {
918+
System.out.println("serialMenu is null");
919+
return;
920+
}
921+
if (name == null) {
922+
System.out.println("name is null");
923+
return;
924+
}
925+
JCheckBoxMenuItem selection = null;
926+
for (int i = 0; i < serialMenu.getItemCount(); i++) {
927+
JCheckBoxMenuItem item = ((JCheckBoxMenuItem)serialMenu.getItem(i));
928+
if (item == null) {
929+
System.out.println("name is null");
930+
continue;
931+
}
932+
item.setState(false);
933+
if (name.equals(item.getText())) selection = item;
934+
}
935+
if (selection != null) selection.setState(true);
936+
//System.out.println(item.getLabel());
937+
Preferences.set("serial.port", name);
938+
serialMonitor.closeSerialPort();
939+
serialMonitor.setVisible(false);
940+
serialMonitor = new SerialMonitor(Preferences.get("serial.port"));
941+
//System.out.println("set to " + get("serial.port"));
942+
}
943+
944+
933945
protected void populateSerialMenu() {
934946
// getting list of ports
935947

@@ -2218,6 +2230,31 @@ public boolean handleSaveAs() {
22182230

22192231
return true;
22202232
}
2233+
2234+
2235+
public boolean serialPrompt() {
2236+
populateSerialMenu();
2237+
int count = serialMenu.getItemCount();
2238+
Object[] names = new Object[count];
2239+
for (int i = 0; i < count; i++) {
2240+
names[i] = ((JCheckBoxMenuItem)serialMenu.getItem(i)).getText();
2241+
}
2242+
2243+
String result = (String)
2244+
JOptionPane.showInputDialog(this,
2245+
"Serial port " +
2246+
Preferences.get("serial.port") +
2247+
" not found.\n" +
2248+
"Retry the upload with another serial port?",
2249+
"Serial port not found",
2250+
JOptionPane.PLAIN_MESSAGE,
2251+
null,
2252+
names,
2253+
0);
2254+
if (result == null) return false;
2255+
selectSerialPort(result);
2256+
return true;
2257+
}
22212258

22222259

22232260
/**
@@ -2260,6 +2297,9 @@ public void run() {
22602297
} else {
22612298
// error message will already be visible
22622299
}
2300+
} catch (SerialNotFoundException e) {
2301+
if (serialPrompt()) run();
2302+
else statusNotice("Upload canceled.");
22632303
} catch (RunnerException e) {
22642304
//statusError("Error during upload.");
22652305
//e.printStackTrace();

app/src/processing/app/Serial.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public Serial(String iname, int irate,
150150
}
151151

152152
if (port == null) {
153-
throw new SerialException("Serial port '" + iname + "' not found. Did you select the right one from the Tools > Serial Port menu?");
153+
throw new SerialNotFoundException("Serial port '" + iname + "' not found. Did you select the right one from the Tools > Serial Port menu?");
154154
}
155155
}
156156

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Copyright (c) 2007 David A. Mellis
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation; either version 2 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software Foundation,
18+
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19+
*/
20+
21+
package processing.app;
22+
23+
public class SerialNotFoundException extends SerialException {
24+
public SerialNotFoundException() {
25+
super();
26+
}
27+
28+
public SerialNotFoundException(String message) {
29+
super(message);
30+
}
31+
32+
public SerialNotFoundException(String message, Throwable cause) {
33+
super(message, cause);
34+
}
35+
36+
public SerialNotFoundException(Throwable cause) {
37+
super(cause);
38+
}
39+
}

app/src/processing/app/Sketch.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,8 +1518,8 @@ public String build(String buildPath, boolean verbose)
15181518
}
15191519
return null;
15201520
}
1521-
1522-
1521+
1522+
15231523
protected boolean exportApplet(boolean verbose) throws Exception {
15241524
return exportApplet(tempBuildFolder.getAbsolutePath(), verbose);
15251525
}
@@ -1529,7 +1529,7 @@ protected boolean exportApplet(boolean verbose) throws Exception {
15291529
* Handle export to applet.
15301530
*/
15311531
public boolean exportApplet(String appletPath, boolean verbose)
1532-
throws RunnerException, IOException {
1532+
throws RunnerException, IOException, SerialException {
15331533

15341534
// Make sure the user didn't hide the sketch folder
15351535
ensureExistence();
@@ -1566,7 +1566,7 @@ public boolean exportApplet(String appletPath, boolean verbose)
15661566
// }
15671567

15681568
upload(appletFolder.getPath(), foundName, verbose);
1569-
1569+
15701570
return true;
15711571
}
15721572

@@ -1593,7 +1593,7 @@ protected void size(String buildPath, String suggestedClassName)
15931593

15941594

15951595
protected String upload(String buildPath, String suggestedClassName, boolean verbose)
1596-
throws RunnerException {
1596+
throws RunnerException, SerialException {
15971597

15981598
Uploader uploader;
15991599

app/src/processing/app/debug/AvrdudeUploader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import processing.app.Base;
3030
import processing.app.Preferences;
3131
import processing.app.Serial;
32+
import processing.app.SerialException;
3233

3334
import java.io.*;
3435
import java.util.*;
@@ -43,7 +44,7 @@ public AvrdudeUploader() {
4344

4445
// XXX: add support for uploading sketches using a programmer
4546
public boolean uploadUsingPreferences(String buildPath, String className, boolean verbose)
46-
throws RunnerException {
47+
throws RunnerException, SerialException {
4748
this.verbose = verbose;
4849
Map<String, String> boardPreferences = Base.getBoardPreferences();
4950
String uploadUsing = boardPreferences.get("upload.using");
@@ -71,7 +72,7 @@ public boolean uploadUsingPreferences(String buildPath, String className, boolea
7172
}
7273

7374
private boolean uploadViaBootloader(String buildPath, String className)
74-
throws RunnerException {
75+
throws RunnerException, SerialException {
7576
Map<String, String> boardPreferences = Base.getBoardPreferences();
7677
List commandDownloader = new ArrayList();
7778
String protocol = boardPreferences.get("upload.protocol");

app/src/processing/app/debug/Uploader.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import processing.app.Base;
3030
import processing.app.Preferences;
3131
import processing.app.Serial;
32+
import processing.app.SerialException;
33+
import processing.app.SerialNotFoundException;
3234

3335
import java.io.*;
3436
import java.util.*;
@@ -63,11 +65,11 @@ public Uploader() {
6365
}
6466

6567
public abstract boolean uploadUsingPreferences(String buildPath, String className, boolean verbose)
66-
throws RunnerException;
68+
throws RunnerException, SerialException;
6769

6870
public abstract boolean burnBootloader(String target, String programmer) throws RunnerException;
6971

70-
protected void flushSerialBuffer() throws RunnerException {
72+
protected void flushSerialBuffer() throws RunnerException, SerialException {
7173
// Cleanup the serial buffer
7274
try {
7375
Serial serialPort = new Serial();
@@ -90,6 +92,8 @@ protected void flushSerialBuffer() throws RunnerException {
9092
serialPort.setRTS(true);
9193

9294
serialPort.dispose();
95+
} catch (SerialNotFoundException e) {
96+
throw e;
9397
} catch(Exception e) {
9498
e.printStackTrace();
9599
throw new RunnerException(e.getMessage());

0 commit comments

Comments
 (0)