Skip to content

Handle unexpected events on serial ports #8046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/src/processing/app/AbstractMonitor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package processing.app;

import cc.arduino.packages.BoardPort;
import cc.arduino.packages.DiscoveryManager;
import processing.app.legacy.PApplet;

import javax.swing.*;
Expand All @@ -9,6 +10,7 @@
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

@SuppressWarnings("serial")
public abstract class AbstractMonitor extends JFrame implements ActionListener {
Expand All @@ -17,6 +19,7 @@ public abstract class AbstractMonitor extends JFrame implements ActionListener {

private StringBuffer updateBuffer;
private Timer updateTimer;
private Timer portExistsTimer;

private BoardPort boardPort;

Expand Down Expand Up @@ -73,6 +76,26 @@ public void actionPerformed(ActionEvent event) {
updateTimer = new Timer(33, this); // redraw serial monitor at 30 Hz
updateTimer.start();

ActionListener portExists = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
if (!Base.getDiscoveryManager().discovery().contains(boardPort)) {
if (!closed) {
suspend();
}
} else {
if (closed && (Editor.avoidMultipleOperations == false)) {
resume(boardPort);
}
}
} catch (Exception e) {}
}
};

portExistsTimer = new Timer(1000, portExists); // check if the port is still there every second
portExistsTimer.start();

closed = false;
}

Expand All @@ -92,6 +115,11 @@ public void suspend() throws Exception {
close();
}

public void dispose() {
super.dispose();
portExistsTimer.stop();
}

public void resume(BoardPort boardPort) throws Exception {
setBoardPort(boardPort);

Expand Down
24 changes: 11 additions & 13 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public boolean test(SketchController controller) {

private int numTools = 0;

public boolean avoidMultipleOperations = false;
static public boolean avoidMultipleOperations = false;

private final EditorToolbar toolbar;
// these menus are shared so that they needn't be rebuilt for all windows
Expand Down Expand Up @@ -1015,22 +1015,20 @@ private void selectSerialPort(String name) {
//System.out.println(item.getLabel());

BaseNoGui.selectSerialPort(name);
if (serialMonitor != null) {
try {
try {
boolean reopenMonitor = ((serialMonitor != null && serialMonitor.isVisible()) ||
serialPlotter != null && serialPlotter.isVisible());
if (serialMonitor != null) {
serialMonitor.close();
serialMonitor.setVisible(false);
} catch (Exception e) {
// ignore
}
}

if (serialPlotter != null) {
try {
if (serialPlotter != null) {
serialPlotter.close();
serialPlotter.setVisible(false);
} catch (Exception e) {
// ignore
}
if (reopenMonitor) {
handleSerial();
}
} catch (Exception e) {
// ignore
}

onBoardOrPortChange();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ public boolean uploadUsingPreferences(File sourcePath, String buildPath, String

// Reuse waitForUploadPort for this task, but this time we are simply waiting
// for one port to reappear. If no port reappears before the timeout, actualUploadPort is selected
finalUploadPort = waitForUploadPort(actualUploadPort, Serial.list(), false);
finalUploadPort = waitForUploadPort(actualUploadPort, Serial.list(), false, 2000);
}
} catch (InterruptedException ex) {
} catch (RunnerException ex) {
// noop
}
}
Expand All @@ -229,13 +229,13 @@ public boolean uploadUsingPreferences(File sourcePath, String buildPath, String
}

private String waitForUploadPort(String uploadPort, List<String> before) throws InterruptedException, RunnerException {
return waitForUploadPort(uploadPort, before, verbose);
return waitForUploadPort(uploadPort, before, verbose, 10000);
}

private String waitForUploadPort(String uploadPort, List<String> before, boolean verbose) throws InterruptedException, RunnerException {
private String waitForUploadPort(String uploadPort, List<String> before, boolean verbose, int timeout) throws InterruptedException, RunnerException {
// Wait for a port to appear on the list
int elapsed = 0;
while (elapsed < 10000) {
while (elapsed < timeout) {
List<String> now = Serial.list();
List<String> diff = new ArrayList<>(now);
diff.removeAll(before);
Expand Down