Skip to content

Commit e55d414

Browse files
author
Federico Fissore
committed
SerialMonitor suspend/resume: dealing with boards that change serial port
between uploads. Fixes #3255 Fixed a missing status management, leading IDE to believe Serial Monitor was opened while it was not. See #3268
1 parent 740a14e commit e55d414

File tree

7 files changed

+113
-95
lines changed

7 files changed

+113
-95
lines changed

app/src/processing/app/AbstractMonitor.java

+46-39
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import javax.swing.border.EmptyBorder;
3131
import javax.swing.text.DefaultCaret;
3232

33+
import cc.arduino.packages.BoardPort;
3334
import processing.app.debug.TextAreaFIFO;
3435
import processing.app.legacy.PApplet;
3536

@@ -50,8 +51,11 @@ public abstract class AbstractMonitor extends JFrame implements ActionListener {
5051
private Timer updateTimer;
5152
private StringBuffer updateBuffer;
5253

53-
public AbstractMonitor(String title) {
54-
super(title);
54+
private BoardPort boardPort;
55+
56+
public AbstractMonitor(BoardPort boardPort) {
57+
super(boardPort.getLabel());
58+
this.boardPort = boardPort;
5559

5660
addWindowListener(new WindowAdapter() {
5761
public void windowClosing(WindowEvent event) {
@@ -136,10 +140,7 @@ public void actionPerformed(ActionEvent event) {
136140
}
137141
lineEndings.setMaximumSize(lineEndings.getMinimumSize());
138142

139-
String[] serialRateStrings = {
140-
"300", "1200", "2400", "4800", "9600",
141-
"19200", "38400", "57600", "115200", "230400", "250000"
142-
};
143+
String[] serialRateStrings = {"300", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400", "250000"};
143144

144145
serialRates = new JComboBox();
145146
for (String rate : serialRateStrings) {
@@ -185,8 +186,7 @@ public void actionPerformed(ActionEvent event) {
185186
closed = false;
186187
}
187188

188-
public void enableWindow(boolean enable)
189-
{
189+
public void enableWindow(boolean enable) {
190190
textArea.setEnabled(enable);
191191
scrollPane.setEnabled(enable);
192192
textField.setEnabled(enable);
@@ -200,33 +200,24 @@ public void enableWindow(boolean enable)
200200

201201
// Puts the window in suspend state, closing the serial port
202202
// to allow other entity (the programmer) to use it
203-
public void suspend()
204-
{
205-
enableWindow(false);
206-
207-
try {
208-
close();
209-
}
210-
catch(Exception e) {
211-
//throw new SerialException("Failed closing the port");
212-
}
203+
public void suspend() throws Exception {
204+
enableWindow(false);
213205

206+
close();
214207
}
215208

216-
public void resume() throws SerialException
217-
{
209+
public void resume(BoardPort boardPort) throws Exception {
210+
setBoardPort(boardPort);
211+
218212
// Enable the window
219213
enableWindow(true);
220214

221215
// If the window is visible, try to open the serial port
222-
if (isVisible())
223-
try {
224-
open();
225-
}
226-
catch(Exception e) {
227-
throw new SerialException("Failed opening the port");
228-
}
216+
if (!isVisible()) {
217+
return;
218+
}
229219

220+
open();
230221
}
231222

232223
public void onSerialRateChange(ActionListener listener) {
@@ -275,12 +266,25 @@ public String getAuthorizationKey() {
275266
}
276267

277268
public boolean isClosed() {
278-
return closed;
269+
return closed;
270+
}
271+
272+
public void open() throws Exception {
273+
closed = false;
279274
}
280275

281-
public abstract void open() throws Exception;
276+
public void close() throws Exception {
277+
closed = true;
278+
}
282279

283-
public abstract void close() throws Exception;
280+
public BoardPort getBoardPort() {
281+
return boardPort;
282+
}
283+
284+
public void setBoardPort(BoardPort boardPort) {
285+
setTitle(boardPort.getLabel());
286+
this.boardPort = boardPort;
287+
}
284288

285289
public synchronized void addToUpdateBuffer(char buff[], int n) {
286290
updateBuffer.append(buff, 0, n);
@@ -293,15 +297,18 @@ private synchronized String consumeUpdateBuffer() {
293297
}
294298

295299
public void actionPerformed(ActionEvent e) {
296-
final String s = consumeUpdateBuffer();
297-
if (s.length() > 0) {
298-
//System.out.println("gui append " + s.length());
299-
if (autoscrollBox.isSelected()) {
300-
textArea.appendTrim(s);
301-
textArea.setCaretPosition(textArea.getDocument().getLength());
302-
} else {
303-
textArea.appendNoTrim(s);
304-
}
300+
String s = consumeUpdateBuffer();
301+
302+
if (s.isEmpty()) {
303+
return;
304+
}
305+
306+
//System.out.println("gui append " + s.length());
307+
if (autoscrollBox.isSelected()) {
308+
textArea.appendTrim(s);
309+
textArea.setCaretPosition(textArea.getDocument().getLength());
310+
} else {
311+
textArea.appendNoTrim(s);
305312
}
306313
}
307314

app/src/processing/app/Base.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,9 @@ public void onBoardOrPortChange() {
11731173
BaseNoGui.onBoardOrPortChange();
11741174

11751175
// Update editors status bar
1176-
for (Editor editor : editors)
1176+
for (Editor editor : editors) {
11771177
editor.onBoardOrPortChange();
1178+
}
11781179
}
11791180

11801181
private void openManageLibrariesDialog() {

app/src/processing/app/Editor.java

+32-22
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ protected void selectSerialPort(String name) {
11181118
}
11191119
}
11201120

1121-
onBoardOrPortChange();
1121+
base.onBoardOrPortChange();
11221122

11231123
//System.out.println("set to " + get("serial.port"));
11241124
}
@@ -2533,7 +2533,6 @@ public void run() {
25332533
// error message will already be visible
25342534
}
25352535
} catch (SerialNotFoundException e) {
2536-
populatePortMenu();
25372536
if (serialMenu.getItemCount() == 0) statusError(e);
25382537
else if (serialPrompt()) run();
25392538
else statusNotice(_("Upload canceled."));
@@ -2548,22 +2547,34 @@ public void run() {
25482547
statusError(e);
25492548
} catch (Exception e) {
25502549
e.printStackTrace();
2550+
} finally {
2551+
populatePortMenu();
25512552
}
25522553
status.unprogress();
25532554
uploading = false;
25542555
//toolbar.clear();
25552556
toolbar.deactivate(EditorToolbar.EXPORT);
25562557

2557-
// Return the serial monitor window to its initial state
2558+
resumeOrCloseSerialMonitor();
2559+
base.onBoardOrPortChange();
2560+
}
2561+
}
2562+
2563+
private void resumeOrCloseSerialMonitor() {
2564+
// Return the serial monitor window to its initial state
2565+
if (serialMonitor != null) {
2566+
BoardPort boardPort = BaseNoGui.getDiscoveryManager().find(PreferencesData.get("serial.port"));
25582567
try {
2559-
if (serialMonitor != null)
2560-
serialMonitor.resume();
2561-
}
2562-
catch (SerialException e) {
2563-
statusError(e);
2568+
if (boardPort == null) {
2569+
serialMonitor.close();
2570+
handleSerial();
2571+
} else {
2572+
serialMonitor.resume(boardPort);
2573+
}
2574+
} catch (Exception e) {
2575+
statusError(e);
25642576
}
2565-
2566-
}
2577+
}
25672578
}
25682579

25692580
// DAM: in Arduino, this is upload (with verbose output)
@@ -2584,7 +2595,6 @@ public void run() {
25842595
// error message will already be visible
25852596
}
25862597
} catch (SerialNotFoundException e) {
2587-
populatePortMenu();
25882598
if (serialMenu.getItemCount() == 0) statusError(e);
25892599
else if (serialPrompt()) run();
25902600
else statusNotice(_("Upload canceled."));
@@ -2599,21 +2609,16 @@ public void run() {
25992609
statusError(e);
26002610
} catch (Exception e) {
26012611
e.printStackTrace();
2612+
} finally {
2613+
populatePortMenu();
26022614
}
26032615
status.unprogress();
26042616
uploading = false;
26052617
//toolbar.clear();
26062618
toolbar.deactivate(EditorToolbar.EXPORT);
26072619

2608-
if (serialMonitor != null) {
2609-
try {
2610-
if (serialMonitor != null)
2611-
serialMonitor.resume();
2612-
}
2613-
catch (SerialException e) {
2614-
statusError(e);
2615-
}
2616-
}
2620+
resumeOrCloseSerialMonitor();
2621+
base.onBoardOrPortChange();
26172622
}
26182623
}
26192624

@@ -2685,8 +2690,13 @@ public void handleSerial() {
26852690

26862691
// If currently uploading, disable the monitor (it will be later
26872692
// enabled when done uploading)
2688-
if (uploading)
2689-
serialMonitor.suspend();
2693+
if (uploading) {
2694+
try {
2695+
serialMonitor.suspend();
2696+
} catch (Exception e) {
2697+
statusError(e);
2698+
}
2699+
}
26902700

26912701
boolean success = false;
26922702
do {

app/src/processing/app/NetworkMonitor.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,13 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
2626

2727
private static final int MAX_CONNECTION_ATTEMPTS = 5;
2828

29-
private final BoardPort port;
30-
private final String ipAddress;
31-
3229
private MessageSiphon inputConsumer;
3330
private Session session;
3431
private Channel channel;
3532
private int connectionAttempts;
3633

3734
public NetworkMonitor(BoardPort port) {
38-
super(port.getLabel());
39-
this.port = port;
40-
this.ipAddress = port.getAddress();
35+
super(port);
4136

4237
onSendCommand(new ActionListener() {
4338
public void actionPerformed(ActionEvent event) {
@@ -61,16 +56,17 @@ public boolean requiresAuthorization() {
6156

6257
@Override
6358
public String getAuthorizationKey() {
64-
return "runtime.pwd." + ipAddress;
59+
return "runtime.pwd." + getBoardPort().getAddress();
6560
}
6661

6762
@Override
6863
public void open() throws Exception {
64+
super.open();
6965
this.connectionAttempts = 0;
7066

7167
JSch jSch = new JSch();
7268
SSHClientSetupChainRing sshClientSetupChain = new SSHConfigFileSetup(new SSHPwdSetup());
73-
session = sshClientSetupChain.setup(port, jSch);
69+
session = sshClientSetupChain.setup(getBoardPort(), jSch);
7470

7571
session.setUserInfo(new NoInteractionUserInfo(PreferencesData.get(getAuthorizationKey())));
7672
session.connect(30000);
@@ -156,6 +152,8 @@ public void run() {
156152

157153
@Override
158154
public void close() throws Exception {
155+
super.close();
156+
159157
if (channel != null) {
160158
inputConsumer.stop();
161159
channel.disconnect();

app/src/processing/app/SerialMonitor.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@
3030
@SuppressWarnings("serial")
3131
public class SerialMonitor extends AbstractMonitor {
3232

33-
private final String port;
3433
private Serial serial;
3534
private int serialRate;
3635

3736
public SerialMonitor(BoardPort port) {
38-
super(port.getLabel());
39-
40-
this.port = port.getAddress();
37+
super(port);
4138

4239
serialRate = PreferencesData.getInteger("serial.debug_rate");
4340
serialRates.setSelectedItem(serialRate + " " + _("baud"));
@@ -89,9 +86,11 @@ private void send(String s) {
8986
}
9087

9188
public void open() throws Exception {
89+
super.open();
90+
9291
if (serial != null) return;
9392

94-
serial = new Serial(port, serialRate) {
93+
serial = new Serial(getBoardPort().getAddress(), serialRate) {
9594
@Override
9695
protected void message(char buff[], int n) {
9796
addToUpdateBuffer(buff, n);
@@ -101,6 +100,7 @@ protected void message(char buff[], int n) {
101100

102101
public void close() throws Exception {
103102
if (serial != null) {
103+
super.close();
104104
int[] location = getPlacement();
105105
String locationStr = PApplet.join(PApplet.str(location), ",");
106106
PreferencesData.set("last.serial.location", locationStr);

0 commit comments

Comments
 (0)