1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-03 15:15:25 +02:00

Serial terminal now allows to specify a port manually (e.g. to access virtual devices that are not found by the port scan)

This commit is contained in:
Michael Scharf 2007-09-26 23:00:19 +00:00
parent a46672968f
commit 53be336cfa
2 changed files with 96 additions and 11 deletions

View file

@ -17,9 +17,13 @@
package org.eclipse.tm.internal.terminal.serial;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import java.util.Arrays;
import java.util.Enumeration;
import org.eclipse.tm.internal.terminal.provisional.api.ITerminalControl;
import org.eclipse.tm.internal.terminal.provisional.api.TerminalState;
@ -38,15 +42,74 @@ public class SerialConnectWorker extends Thread {
fConn = conn;
fControl.setState(TerminalState.CONNECTING);
}
public void run() {
/**
* Adds the named port to the name of known ports to rxtx
* @param name
*/
void addPort(String name) {
// Rxtx either takes the connection from the properties OR using
// the port scan.
// Unfortunately, setting gnu.io.rxtx.SerialPorts only temporarily does not
// work, because rxtx closes connections that are unknown.
// The only solution I could come up with: add the known connections
// to the gnu.io.rxtx.SerialPorts property....
final String GNU_IO_RXTX_SERIAL_PORTS = "gnu.io.rxtx.SerialPorts"; //$NON-NLS-1$
String sep = System.getProperty("path.separator", ":"); //$NON-NLS-1$//$NON-NLS-2$
// get the existing names
String names = System.getProperty(GNU_IO_RXTX_SERIAL_PORTS);
if (names == null) {
StringBuffer buffer=new StringBuffer();
boolean sepNeeded=false;
// When we add a port to this property, rxtx forgets the
// ports it finds by scanning the system.
// iterate over the known ports and add them to the property
Enumeration portIdEnum= CommPortIdentifier.getPortIdentifiers();
while (portIdEnum.hasMoreElements()) {
CommPortIdentifier identifier = (CommPortIdentifier) portIdEnum.nextElement();
if (identifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if(sepNeeded)
buffer.append(sep);
else
sepNeeded=true;
buffer.append(identifier.getName());
}
}
// append our new port
if(sepNeeded)
buffer.append(sep);
buffer.append(name);
System.setProperty(GNU_IO_RXTX_SERIAL_PORTS,buffer.toString());
} else if (!Arrays.asList(names.split(sep)).contains(name)) {
// the list does not contain the name, therefore we add it
// since there is at least one name in the list, we append it
System.setProperty(GNU_IO_RXTX_SERIAL_PORTS, names + sep + name);
} else {
// nothing to do -- should never happen...
return;
}
// Reinitialise the ports because we have changed the list of known ports
CommPortIdentifier.getPortIdentifiers();
}
public void run() {
String portName=null;
try {
fControl.setState(TerminalState.OPENED);
String strID = getClass().getPackage().getName();
ISerialSettings s=fConn.getSerialSettings();
portName=s.getSerialPort();
try {
fConn.setSerialPortIdentifier(CommPortIdentifier.getPortIdentifier(portName));
} catch (NoSuchPortException e) {
// let's try
addPort(portName);
fConn.setSerialPortIdentifier(CommPortIdentifier.getPortIdentifier(portName));
}
fConn.setSerialPortHandler(new SerialPortHandler(fConn,fControl));
fConn.setSerialPortIdentifier(CommPortIdentifier.getPortIdentifier(s.getSerialPort()));
fConn.setSerialPortIdentifier(CommPortIdentifier.getPortIdentifier(portName));
int timeoutInMs = s.getTimeout() * 1000;
SerialPort serialPort=(SerialPort) fConn.getSerialPortIdentifier().open(strID,timeoutInMs);
@ -59,8 +122,14 @@ public class SerialConnectWorker extends Thread {
fControl.setState(TerminalState.CONNECTED);
} catch (PortInUseException portInUseException) {
fControl.setState(TerminalState.CLOSED);
fControl.setMsg("Connection Error!\n" + portInUseException.getMessage()); //$NON-NLS-1$
fControl.displayTextInTerminal("Connection Error!\n" + portInUseException.getMessage()); //$NON-NLS-1$
} catch (NoSuchPortException e) {
fControl.setState(TerminalState.CLOSED);
String msg=e.getMessage();
if(msg==null)
msg=portName;
fControl.displayTextInTerminal("No such port: \"" + msg+"\"\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
} catch (Exception exception) {
fControl.setState(TerminalState.CLOSED);
}

View file

@ -85,16 +85,25 @@ public class SerialSettingsPage implements ISettingsPage {
if(value==null)
return;
int nIndex = combo.indexOf(value);
if (nIndex == -1)
return;
if (nIndex == -1) {
if((combo.getStyle() & SWT.READ_ONLY)==0) {
combo.add(value);
nIndex = combo.indexOf(value);
} else {
return;
}
}
combo.select(nIndex);
}
private String getComboValue(Combo combo) {
int nIndex = combo.getSelectionIndex();
if (nIndex == -1)
return ""; //$NON-NLS-1$
if (nIndex == -1) {
if((combo.getStyle() & SWT.READ_ONLY)!=0)
return ""; //$NON-NLS-1$
return combo.getText();
}
return combo.getItem(nIndex);
@ -109,7 +118,7 @@ public class SerialSettingsPage implements ISettingsPage {
composite.setLayout(gridLayout);
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
fSerialPortCombo=addLabeledCombo(composite, SerialMessages.PORT + ":"); //$NON-NLS-1$
fSerialPortCombo=addLabeledCombo(composite, SerialMessages.PORT + ":",false); //$NON-NLS-1$
fBaudRateCombo=addLabeledCombo(composite, SerialMessages.BAUDRATE + ":"); //$NON-NLS-1$
fDataBitsCombo=addLabeledCombo(composite, SerialMessages.DATABITS + ":"); //$NON-NLS-1$
fStopBitsCombo=addLabeledCombo(composite, SerialMessages.STOPBITS + ":"); //$NON-NLS-1$
@ -123,11 +132,18 @@ public class SerialSettingsPage implements ISettingsPage {
}
private Combo addLabeledCombo(Composite composite, String label) {
return addLabeledCombo(composite, label, true);
}
private Combo addLabeledCombo(Composite composite, String label,boolean readonly) {
new Label(composite, SWT.RIGHT).setText(label);
Combo combo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
int flags=SWT.DROP_DOWN;
if(readonly)
flags|=SWT.READ_ONLY;
Combo combo = new Combo(composite, flags);
combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
return combo;
}
private void loadCombo(Combo ctlCombo, List table) {
for (Iterator iter = table.iterator(); iter.hasNext();) {
String label = (String) iter.next();