Skip to content

Commit ee1a245

Browse files
author
jantje
committed
#1394 wait for com port to show up
1 parent 6a00777 commit ee1a245

File tree

5 files changed

+142
-120
lines changed

5 files changed

+142
-120
lines changed

io.sloeber.core/src/io/sloeber/core/api/ISerialUser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ public interface ISerialUser {
44
public boolean PauzePort(String PortName);
55

66
public void ResumePort(String PortName);
7+
8+
public boolean stopPort(String mComPort);
79
}

io.sloeber.core/src/io/sloeber/core/api/Serial.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ public static void errorMessage(String where, Throwable e) {
113113

114114
}
115115

116-
117116
public static List<String> list() {
118117
try {
119118
String[] portNames;
@@ -208,7 +207,7 @@ public void disconnect() {
208207
}
209208

210209
public void dispose() {
211-
notifyConsumersOfEvent("Disconnect of port " + this.port.getPortName() + " executed"); //$NON-NLS-1$ //$NON-NLS-2$
210+
notifyConsumersOfEvent("Disconnect of port " + portName + " executed"); //$NON-NLS-1$ //$NON-NLS-2$
212211
disconnect();
213212

214213
if (this.fServiceRegistration != null) {

io.sloeber.core/src/io/sloeber/core/api/SerialManager.java

Lines changed: 81 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,85 +9,92 @@
99
import cc.arduino.packages.discoverers.SloeberNetworkDiscovery;
1010

1111
public class SerialManager {
12-
static ISerialUser otherSerialUser = null; // If someone else uses the
13-
// serial port he can register
14-
// here so we can
15-
// request him to disconnect
16-
// when we need the serial port
12+
static ISerialUser otherSerialUser = null; // If someone else uses the
13+
// serial port he can register
14+
// here so we can
15+
// request him to disconnect
16+
// when we need the serial port
1717

18-
static Set<IProject> fProjects = new HashSet<>();
18+
static Set<IProject> fProjects = new HashSet<>();
1919

20-
/**
20+
/**
2121
* This method is used to register a serial user. A serial user is alerted
2222
* when the serial port will be disconnected (for instance for a upload) The
2323
* serial user is requested to act appropriately Only 1 serial user can be
2424
* registered at a given time. No check is done.
25-
*
26-
* @param serialUser
27-
*/
28-
public static void registerSerialUser(ISerialUser serialUser) {
29-
otherSerialUser = serialUser;
30-
}
31-
32-
/**
33-
* This method is to unregister a serial user.
34-
*/
35-
public static void UnRegisterSerialUser() {
36-
otherSerialUser = null;
37-
}
38-
39-
public static boolean pauzeSerialMonitor(String mComPort) {
40-
if (otherSerialUser != null) {
41-
return otherSerialUser.PauzePort(mComPort);
42-
}
43-
return false;
44-
}
45-
46-
public static void resumeSerialMonitor(String mComPort) {
47-
if (otherSerialUser != null) {
48-
otherSerialUser.ResumePort(mComPort);
49-
}
50-
51-
}
52-
53-
public static String[] listComPorts() {
54-
List<String> serialList = Serial.list();
55-
String[] outgoing = new String[serialList.size()];
56-
serialList.toArray(outgoing);
57-
return outgoing;
58-
}
59-
60-
@SuppressWarnings("nls")
61-
public static String[] listBaudRates() {
62-
63-
String[] outgoing = { "921600", "460800", "230400", "115200", "76800", "57600", "38400", "31250", "28800",
64-
"19200", "14400", "9600", "4800", "2400", "1200", "300" };
65-
return outgoing;
66-
}
67-
68-
@SuppressWarnings("nls")
69-
public static String[] listLineEndings() {
70-
String[] outgoing = { "none", "CR", "NL", "CR/NL" };
71-
return outgoing;
72-
}
73-
74-
@SuppressWarnings("nls")
75-
public static String getLineEnding(int selectionIndex) {
76-
switch (selectionIndex) {
77-
default:
78-
case 0:
79-
return "";
80-
case 1:
81-
return "\r";
82-
case 2:
83-
return "\n";
84-
case 3:
85-
return "\r\n";
86-
}
87-
}
88-
89-
public static String[] listNetworkPorts() {
90-
return SloeberNetworkDiscovery.getList();
91-
}
25+
*
26+
* @param serialUser
27+
*/
28+
public static void registerSerialUser(ISerialUser serialUser) {
29+
otherSerialUser = serialUser;
30+
}
31+
32+
/**
33+
* This method is to unregister a serial user.
34+
*/
35+
public static void UnRegisterSerialUser() {
36+
otherSerialUser = null;
37+
}
38+
39+
public static boolean pauzeSerialMonitor(String mComPort) {
40+
if (otherSerialUser != null) {
41+
return otherSerialUser.PauzePort(mComPort);
42+
}
43+
return false;
44+
}
45+
46+
public static boolean stopSerialMonitor(String mComPort) {
47+
if (otherSerialUser != null) {
48+
return otherSerialUser.stopPort(mComPort);
49+
}
50+
return false;
51+
}
52+
53+
public static void resumeSerialMonitor(String mComPort) {
54+
if (otherSerialUser != null) {
55+
otherSerialUser.ResumePort(mComPort);
56+
}
57+
58+
}
59+
60+
public static String[] listComPorts() {
61+
List<String> serialList = Serial.list();
62+
String[] outgoing = new String[serialList.size()];
63+
serialList.toArray(outgoing);
64+
return outgoing;
65+
}
66+
67+
@SuppressWarnings("nls")
68+
public static String[] listBaudRates() {
69+
70+
String[] outgoing = { "921600", "460800", "230400", "115200", "76800", "57600", "38400", "31250", "28800",
71+
"19200", "14400", "9600", "4800", "2400", "1200", "300" };
72+
return outgoing;
73+
}
74+
75+
@SuppressWarnings("nls")
76+
public static String[] listLineEndings() {
77+
String[] outgoing = { "none", "CR", "NL", "CR/NL" };
78+
return outgoing;
79+
}
80+
81+
@SuppressWarnings("nls")
82+
public static String getLineEnding(int selectionIndex) {
83+
switch (selectionIndex) {
84+
default:
85+
case 0:
86+
return "";
87+
case 1:
88+
return "\r";
89+
case 2:
90+
return "\n";
91+
case 3:
92+
return "\r\n";
93+
}
94+
}
95+
96+
public static String[] listNetworkPorts() {
97+
return SloeberNetworkDiscovery.getList();
98+
}
9299

93100
}

io.sloeber.core/src/io/sloeber/core/tools/uploaders/UploadSketchWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void run() {
199199
if (portFound) {
200200
SerialManager.resumeSerialMonitor(myProvidedUploadPort);
201201
} else {
202-
SerialManager.pauzeSerialMonitor(myProvidedUploadPort);
202+
SerialManager.stopSerialMonitor(myProvidedUploadPort);
203203
}
204204
}
205205
} catch (Exception e) {

io.sloeber.ui/src/io/sloeber/ui/monitor/views/SerialMonitor.java

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
* serial connection.
7575
*
7676
*/
77-
@SuppressWarnings({"unused"})
77+
@SuppressWarnings({ "unused" })
7878
public class SerialMonitor extends ViewPart implements ISerialUser {
7979

8080
/**
@@ -97,7 +97,7 @@ public class SerialMonitor extends ViewPart implements ISerialUser {
9797
static private final URL IMG_LOCK;
9898
static private final URL IMG_FILTER;
9999
static private final URL IMG_TIMESTAMP;
100-
static private final String newLine=System.getProperty("line.separator"); //$NON-NLS-1$
100+
static private final String newLine = System.getProperty("line.separator"); //$NON-NLS-1$
101101

102102
static {
103103
IMG_CLEAR = getDefault().getBundle().getEntry("icons/clear_console.png"); //$NON-NLS-1$
@@ -122,7 +122,7 @@ public class SerialMonitor extends ViewPart implements ISerialUser {
122122

123123
// The string to send to the serial port
124124
protected Text sendString;
125-
// control contains the output of the serial port
125+
// control contains the output of the serial port
126126
static protected StyledText monitorOutput;
127127
// Port used when doing actions
128128
protected ComboViewer serialPorts;
@@ -330,41 +330,41 @@ public void widgetDefaultSelected(SelectionEvent e) {
330330
monitorOutput.setText(Messages.serialMonitorNoInput + newLine);
331331
monitorOutput.addMouseListener(new MouseListener() {
332332

333-
@Override
334-
public void mouseUp(MouseEvent e) {
335-
// ignore
336-
}
337-
338-
@Override
339-
public void mouseDown(MouseEvent e) {
340-
// If right button get selected text save it and start external tool
341-
if (e.button==3) {
342-
String selectedText=monitorOutput.getSelectionText();
343-
if(!selectedText.isEmpty()) {
344-
IProject selectedProject = ProjectExplorerListener.getSelectedProject();
345-
if (selectedProject!=null) {
346-
347-
try {
333+
@Override
334+
public void mouseUp(MouseEvent e) {
335+
// ignore
336+
}
337+
338+
@Override
339+
public void mouseDown(MouseEvent e) {
340+
// If right button get selected text save it and start external tool
341+
if (e.button == 3) {
342+
String selectedText = monitorOutput.getSelectionText();
343+
if (!selectedText.isEmpty()) {
344+
IProject selectedProject = ProjectExplorerListener.getSelectedProject();
345+
if (selectedProject != null) {
346+
347+
try {
348348
ICConfigurationDescription activeCfg=CoreModel.getDefault().getProjectDescription(selectedProject).getActiveConfiguration();
349-
String activeConfigName= activeCfg.getName();
350-
IPath buildFolder=selectedProject.findMember(activeConfigName).getLocation();
351-
File dumpFile=buildFolder.append("serialdump.txt").toFile(); //$NON-NLS-1$
352-
FileUtils.writeStringToFile(dumpFile, selectedText, Charset.defaultCharset());
353-
} catch (Exception e1) {
354-
// ignore
355-
e1.printStackTrace();
356-
}
357-
}
358-
359-
}
360-
}
361-
}
362-
363-
@Override
364-
public void mouseDoubleClick(MouseEvent e) {
365-
// ignore
366-
}
367-
});
349+
String activeConfigName = activeCfg.getName();
350+
IPath buildFolder = selectedProject.findMember(activeConfigName).getLocation();
351+
File dumpFile = buildFolder.append("serialdump.txt").toFile(); //$NON-NLS-1$
352+
FileUtils.writeStringToFile(dumpFile, selectedText, Charset.defaultCharset());
353+
} catch (Exception e1) {
354+
// ignore
355+
e1.printStackTrace();
356+
}
357+
}
358+
359+
}
360+
}
361+
}
362+
363+
@Override
364+
public void mouseDoubleClick(MouseEvent e) {
365+
// ignore
366+
}
367+
});
368368

369369
parent.getShell().setDefaultButton(send);
370370
makeActions();
@@ -595,7 +595,7 @@ public void connectSerial(String comPort, int baudRate) {
595595
if (serialConnections.size() < MY_MAX_SERIAL_PORTS) {
596596
int colorindex = -1;
597597
for (int idx = 0; idx < serialPortAllocated.length; idx++) {
598-
if ( ! serialPortAllocated[idx]) {
598+
if (!serialPortAllocated[idx]) {
599599
colorindex = idx;
600600
break;
601601
}
@@ -653,8 +653,8 @@ public void disConnectSerialPort(String comPort) {
653653

654654
newSerial.dispose();
655655
theListener.dispose();
656-
SerialPortsUpdated();
657656
}
657+
SerialPortsUpdated();
658658
}
659659

660660
/**
@@ -704,14 +704,28 @@ public void run() {
704704
}
705705

706706
public static List<String> getMonitorContent() {
707-
int numLines =monitorOutput.getContent().getLineCount();
708-
List<String>ret=new ArrayList<>();
709-
for(int curLine=1;curLine<numLines;curLine++) {
710-
ret.add(monitorOutput.getContent().getLine(curLine-1));
707+
int numLines = monitorOutput.getContent().getLineCount();
708+
List<String> ret = new ArrayList<>();
709+
for (int curLine = 1; curLine < numLines; curLine++) {
710+
ret.add(monitorOutput.getContent().getLine(curLine - 1));
711711
}
712712
return ret;
713713
}
714+
714715
public static void clearMonitor() {
715716
monitorOutput.setText(new String());
716717
}
718+
719+
@Override
720+
public boolean stopPort(String mComPort) {
721+
// run this in the gui thread
722+
Display.getDefault().asyncExec(new Runnable() {
723+
@Override
724+
public void run() {
725+
disConnectSerialPort(mComPort);
726+
}
727+
});
728+
729+
return true;
730+
}
717731
}

0 commit comments

Comments
 (0)