Skip to content

Commit

Permalink
Handle suspend/resume if serial port disappears
Browse files Browse the repository at this point in the history
Similar to minicom behaviour.
Automatically reopens the port only if it takes the same name (could be improved based on vid/pid)
  • Loading branch information
facchinm committed Jul 18, 2019
1 parent e7d85d8 commit 693498f
Showing 1 changed file with 28 additions and 0 deletions.
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) {
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

0 comments on commit 693498f

Please sign in to comment.