added static touchPort() method to Serial class.

Used by Leonardo to quickly tap the comm port to initiate a reset without the potential problems of doing a full Serial object construct/dispose with all listeners, etc.
This commit is contained in:
Zach Eveland 2012-02-13 00:37:21 -05:00
parent 25a4fe8607
commit 5c53796cec
1 changed files with 26 additions and 0 deletions

View File

@ -101,6 +101,32 @@ public class Serial implements SerialPortEventListener {
new Float(Preferences.get("serial.stopbits")).floatValue());
}
public static boolean touchPort(String iname, int irate) throws SerialException {
SerialPort port;
boolean result = false;
try {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if ((CommPortIdentifier.PORT_SERIAL == portId.getPortType()) && (portId.getName().equals(iname))) {
port = (SerialPort) portId.open("tap", 2000);
port.setSerialPortParams(irate, 8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
port.close();
result = true;
}
}
} catch (PortInUseException e) {
throw new SerialException(
I18n.format(_("Serial port ''{0}'' already in use. Try quiting any programs that may be using it."), iname)
);
} catch (Exception e) {
throw new SerialException(
I18n.format(_("Error touching serial port ''{0}''."), iname), e
);
}
return result;
}
public Serial(String iname, int irate,
char iparity, int idatabits, float istopbits)
throws SerialException {