Skip to content

Fix bogus port disconnection during serial event #9862

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 2 commits into from
Mar 23, 2020
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
5 changes: 5 additions & 0 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,11 @@ static public boolean isUploading() {
private void resumeOrCloseSerialMonitor() {
// Return the serial monitor window to its initial state
if (serialMonitor != null) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// noop
}
BoardPort boardPort = BaseNoGui.getDiscoveryManager().find(PreferencesData.get("serial.port"));
long sleptFor = 0;
while (boardPort == null && sleptFor < MAX_TIME_AWAITING_FOR_RESUMING_SERIAL_MONITOR) {
Expand Down
4 changes: 4 additions & 0 deletions arduino-core/src/cc/arduino/packages/BoardPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public String toString() {
return this.address;
}

public String toCompleteString() {
return this.address + "_" + this.getPrefs().get("vid") + "_" + this.getPrefs().get("pid");
}

// Search for the board which matches identificationPrefs.
// If found, boardName is set to the name from boards.txt
// and the board is returned. If not found, null is returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class SerialDiscovery implements Discovery, Runnable {
private final List<String> oldPorts = new ArrayList<>();
public boolean uploadInProgress = false;
public boolean pausePolling = false;
private BoardPort oldUploadBoardPort = null;
private final BoardCloudResolver boardCloudResolver = new BoardCloudResolver();


Expand All @@ -56,7 +55,7 @@ public List<BoardPort> listDiscoveredBoards() {
}

@Override
public List<BoardPort> listDiscoveredBoards(boolean complete) {
public synchronized List<BoardPort> listDiscoveredBoards(boolean complete) {
if (complete) {
return new ArrayList<>(serialBoardPorts);
}
Expand All @@ -69,7 +68,7 @@ public List<BoardPort> listDiscoveredBoards(boolean complete) {
return onlineBoardPorts;
}

public void setSerialBoardPorts(List<BoardPort> newSerialBoardPorts) {
public synchronized void setSerialBoardPorts(List<BoardPort> newSerialBoardPorts) {
serialBoardPorts.clear();
serialBoardPorts.addAll(newSerialBoardPorts);
}
Expand Down Expand Up @@ -116,27 +115,17 @@ public synchronized void forceRefresh() {
return;
}

// if (updating) {}
// a port will disappear, another will appear
// use this information to "merge" the boards
// updating must be signaled by SerialUpload class

oldPorts.clear();
oldPorts.addAll(ports);

// set unreachable ports offline
for (BoardPort board : boardPorts) {
if (ports.contains(board.toString())) {
if (board.isOnline()) {
ports.remove(ports.indexOf(board.toString()));
}
} else {
if (uploadInProgress && board.isOnline()) {
oldUploadBoardPort = board;
}
if (!ports.contains(board.toCompleteString())) {
board.setOnlineStatus(false);
}
}

// add information for newly added ports
for (String newPort : ports) {

String[] parts = newPort.split("_");
Expand All @@ -161,35 +150,35 @@ public synchronized void forceRefresh() {

BoardPort boardPort = null;
int i = 0;
// create new board or update existing

// create new board if in ports but not in boardPorts
for (BoardPort board : boardPorts) {
if (board.toString().equals(newPort)) {
if (board.toCompleteString().equalsIgnoreCase(newPort)) {
boardPort = boardPorts.get(i);
boardPorts.get(i).setOnlineStatus(true);
break;
}
i++;
}
if (boardPort == null) {
boardPort = new BoardPort();
boardPorts.add(boardPort);

if (boardPort != null) {
continue;
}

boardPort = new BoardPort();
boardPorts.add(boardPort);
boardPort.setAddress(port);
boardPort.setProtocol("serial");
boardPort.setOnlineStatus(true);

String label = port;
boardPort.setLabel(port);

if (boardData != null) {
boardPort.getPrefs().put("vid", boardData.get("vid").toString());
boardPort.getPrefs().put("pid", boardData.get("pid").toString());

String iserial = boardData.get("iserial").toString();
if (iserial.length() >= 10) {
boardPort.getPrefs().put("iserial", iserial);
}
if (uploadInProgress && oldUploadBoardPort!=null) {
oldUploadBoardPort.getPrefs().put("iserial", iserial);
}
boardPort.getPrefs().put("iserial", iserial);

TargetBoard board = (TargetBoard) boardData.get("board");
if (board != null) {
Expand All @@ -208,8 +197,6 @@ public synchronized void forceRefresh() {
boardPort.getPrefs().put("iserial", "");
}
}

boardPort.setLabel(label);
}
setSerialBoardPorts(boardPorts);
}
Expand Down
5 changes: 3 additions & 2 deletions arduino-core/src/processing/app/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ public synchronized Map<String, Object> resolveDeviceByVendorIdProductId(String
}
Map<String, Object> boardData = new HashMap<>();
boardData.put("board", board);
boardData.put("vid", vids.get(i));
boardData.put("pid", pids.get(i));
// remove 0x from VID / PID to keep them as reported by liblistserial
boardData.put("vid", vids.get(i).replaceAll("0x", ""));
boardData.put("pid", pids.get(i).replaceAll("0x", ""));
String extrafields = vid_pid_iSerial.substring(vidPid.length() + 1);
String[] parts = extrafields.split("_");
boardData.put("iserial", parts[0]);
Expand Down