Skip to content

Commit

Permalink
SerialMonitor suspend/resume: dealing with boards that change serial …
Browse files Browse the repository at this point in the history
…port

between uploads. Fixes #3255
Fixed a missing status management, leading IDE to believe Serial Monitor
was opened while it was not. See #3268
  • Loading branch information
Federico Fissore committed Jun 1, 2015
1 parent 740a14e commit e55d414
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 95 deletions.
85 changes: 46 additions & 39 deletions app/src/processing/app/AbstractMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultCaret;

import cc.arduino.packages.BoardPort;
import processing.app.debug.TextAreaFIFO;
import processing.app.legacy.PApplet;

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

public AbstractMonitor(String title) {
super(title);
private BoardPort boardPort;

public AbstractMonitor(BoardPort boardPort) {
super(boardPort.getLabel());
this.boardPort = boardPort;

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
Expand Down Expand Up @@ -136,10 +140,7 @@ public void actionPerformed(ActionEvent event) {
}
lineEndings.setMaximumSize(lineEndings.getMinimumSize());

String[] serialRateStrings = {
"300", "1200", "2400", "4800", "9600",
"19200", "38400", "57600", "115200", "230400", "250000"
};
String[] serialRateStrings = {"300", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400", "250000"};

serialRates = new JComboBox();
for (String rate : serialRateStrings) {
Expand Down Expand Up @@ -185,8 +186,7 @@ public void actionPerformed(ActionEvent event) {
closed = false;
}

public void enableWindow(boolean enable)
{
public void enableWindow(boolean enable) {
textArea.setEnabled(enable);
scrollPane.setEnabled(enable);
textField.setEnabled(enable);
Expand All @@ -200,33 +200,24 @@ public void enableWindow(boolean enable)

// Puts the window in suspend state, closing the serial port
// to allow other entity (the programmer) to use it
public void suspend()
{
enableWindow(false);

try {
close();
}
catch(Exception e) {
//throw new SerialException("Failed closing the port");
}
public void suspend() throws Exception {
enableWindow(false);

close();
}

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

// Enable the window
enableWindow(true);

// If the window is visible, try to open the serial port
if (isVisible())
try {
open();
}
catch(Exception e) {
throw new SerialException("Failed opening the port");
}
if (!isVisible()) {
return;
}

open();
}

public void onSerialRateChange(ActionListener listener) {
Expand Down Expand Up @@ -275,12 +266,25 @@ public String getAuthorizationKey() {
}

public boolean isClosed() {
return closed;
return closed;
}

public void open() throws Exception {
closed = false;
}

public abstract void open() throws Exception;
public void close() throws Exception {
closed = true;
}

public abstract void close() throws Exception;
public BoardPort getBoardPort() {
return boardPort;
}

public void setBoardPort(BoardPort boardPort) {
setTitle(boardPort.getLabel());
this.boardPort = boardPort;
}

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

public void actionPerformed(ActionEvent e) {
final String s = consumeUpdateBuffer();
if (s.length() > 0) {
//System.out.println("gui append " + s.length());
if (autoscrollBox.isSelected()) {
textArea.appendTrim(s);
textArea.setCaretPosition(textArea.getDocument().getLength());
} else {
textArea.appendNoTrim(s);
}
String s = consumeUpdateBuffer();

if (s.isEmpty()) {
return;
}

//System.out.println("gui append " + s.length());
if (autoscrollBox.isSelected()) {
textArea.appendTrim(s);
textArea.setCaretPosition(textArea.getDocument().getLength());
} else {
textArea.appendNoTrim(s);
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -1173,8 +1173,9 @@ public void onBoardOrPortChange() {
BaseNoGui.onBoardOrPortChange();

// Update editors status bar
for (Editor editor : editors)
for (Editor editor : editors) {
editor.onBoardOrPortChange();
}
}

private void openManageLibrariesDialog() {
Expand Down
54 changes: 32 additions & 22 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ protected void selectSerialPort(String name) {
}
}

onBoardOrPortChange();
base.onBoardOrPortChange();

//System.out.println("set to " + get("serial.port"));
}
Expand Down Expand Up @@ -2533,7 +2533,6 @@ public void run() {
// error message will already be visible
}
} catch (SerialNotFoundException e) {
populatePortMenu();
if (serialMenu.getItemCount() == 0) statusError(e);
else if (serialPrompt()) run();
else statusNotice(_("Upload canceled."));
Expand All @@ -2548,22 +2547,34 @@ public void run() {
statusError(e);
} catch (Exception e) {
e.printStackTrace();
} finally {
populatePortMenu();
}
status.unprogress();
uploading = false;
//toolbar.clear();
toolbar.deactivate(EditorToolbar.EXPORT);

// Return the serial monitor window to its initial state
resumeOrCloseSerialMonitor();
base.onBoardOrPortChange();
}
}

private void resumeOrCloseSerialMonitor() {
// Return the serial monitor window to its initial state
if (serialMonitor != null) {
BoardPort boardPort = BaseNoGui.getDiscoveryManager().find(PreferencesData.get("serial.port"));
try {
if (serialMonitor != null)
serialMonitor.resume();
}
catch (SerialException e) {
statusError(e);
if (boardPort == null) {
serialMonitor.close();
handleSerial();
} else {
serialMonitor.resume(boardPort);
}
} catch (Exception e) {
statusError(e);
}

}
}
}

// DAM: in Arduino, this is upload (with verbose output)
Expand All @@ -2584,7 +2595,6 @@ public void run() {
// error message will already be visible
}
} catch (SerialNotFoundException e) {
populatePortMenu();
if (serialMenu.getItemCount() == 0) statusError(e);
else if (serialPrompt()) run();
else statusNotice(_("Upload canceled."));
Expand All @@ -2599,21 +2609,16 @@ public void run() {
statusError(e);
} catch (Exception e) {
e.printStackTrace();
} finally {
populatePortMenu();
}
status.unprogress();
uploading = false;
//toolbar.clear();
toolbar.deactivate(EditorToolbar.EXPORT);

if (serialMonitor != null) {
try {
if (serialMonitor != null)
serialMonitor.resume();
}
catch (SerialException e) {
statusError(e);
}
}
resumeOrCloseSerialMonitor();
base.onBoardOrPortChange();
}
}

Expand Down Expand Up @@ -2685,8 +2690,13 @@ public void handleSerial() {

// If currently uploading, disable the monitor (it will be later
// enabled when done uploading)
if (uploading)
serialMonitor.suspend();
if (uploading) {
try {
serialMonitor.suspend();
} catch (Exception e) {
statusError(e);
}
}

boolean success = false;
do {
Expand Down
14 changes: 6 additions & 8 deletions app/src/processing/app/NetworkMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,13 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {

private static final int MAX_CONNECTION_ATTEMPTS = 5;

private final BoardPort port;
private final String ipAddress;

private MessageSiphon inputConsumer;
private Session session;
private Channel channel;
private int connectionAttempts;

public NetworkMonitor(BoardPort port) {
super(port.getLabel());
this.port = port;
this.ipAddress = port.getAddress();
super(port);

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

@Override
public String getAuthorizationKey() {
return "runtime.pwd." + ipAddress;
return "runtime.pwd." + getBoardPort().getAddress();
}

@Override
public void open() throws Exception {
super.open();
this.connectionAttempts = 0;

JSch jSch = new JSch();
SSHClientSetupChainRing sshClientSetupChain = new SSHConfigFileSetup(new SSHPwdSetup());
session = sshClientSetupChain.setup(port, jSch);
session = sshClientSetupChain.setup(getBoardPort(), jSch);

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

@Override
public void close() throws Exception {
super.close();

if (channel != null) {
inputConsumer.stop();
channel.disconnect();
Expand Down
10 changes: 5 additions & 5 deletions app/src/processing/app/SerialMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@
@SuppressWarnings("serial")
public class SerialMonitor extends AbstractMonitor {

private final String port;
private Serial serial;
private int serialRate;

public SerialMonitor(BoardPort port) {
super(port.getLabel());

this.port = port.getAddress();
super(port);

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

public void open() throws Exception {
super.open();

if (serial != null) return;

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

public void close() throws Exception {
if (serial != null) {
super.close();
int[] location = getPlacement();
String locationStr = PApplet.join(PApplet.str(location), ",");
PreferencesData.set("last.serial.location", locationStr);
Expand Down

0 comments on commit e55d414

Please sign in to comment.