Skip to content

Commit e9a00eb

Browse files
committed
close SerialMonitor when port goes away suddenly (as when user presses Leonardo reset button)
1 parent ae5d5c0 commit e9a00eb

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

app/src/processing/app/SerialMonitor.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
import javax.swing.border.*;
2929
import javax.swing.event.*;
3030
import javax.swing.text.*;
31+
import gnu.io.CommPortIdentifier;
32+
import java.util.Enumeration;
3133

32-
public class SerialMonitor extends JFrame implements MessageConsumer {
34+
public class SerialMonitor extends JFrame implements MessageConsumer, Runnable {
3335
private Serial serial;
3436
private String port;
3537
private JTextArea textArea;
@@ -171,6 +173,9 @@ public void actionPerformed(ActionEvent event) {
171173
}
172174
}
173175
}
176+
177+
Thread thread = new Thread(this);
178+
thread.start();
174179
}
175180

176181
protected void setPlacement(int[] location) {
@@ -228,4 +233,26 @@ public void run() {
228233
}
229234
}});
230235
}
236+
237+
public void run() {
238+
while (!Thread.interrupted()) {
239+
if (serial != null) {
240+
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
241+
boolean portStillHere = false;
242+
while (portList.hasMoreElements()) {
243+
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
244+
if (portId.getName().equals(port)) {
245+
portStillHere = true;
246+
}
247+
}
248+
if (!portStillHere) {
249+
closeSerialPort();
250+
setVisible(false);
251+
}
252+
try {
253+
Thread.sleep(100);
254+
} catch (InterruptedException ex) { }
255+
}
256+
}
257+
}
231258
}

0 commit comments

Comments
 (0)