Skip to content

Commit

Permalink
fix(JRxTxPort): close port when setting parameters fails. Fixes "port…
Browse files Browse the repository at this point in the history
… is in use" exception following that.
  • Loading branch information
cfstras committed Feb 15, 2019
1 parent b42e816 commit 454877e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions configuration.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project.ext {

cfgJavaVersion = "1.8"

cfgVersion = "1.2.0"
cfgVersion = "1.3.0"

cfgGroup = "de.ninjaneers"

Expand Down Expand Up @@ -286,7 +286,7 @@ sourceSets {
}

task itest(type: Test){
systemProperty "java.library.path", "/usr/lib/jni/"
systemProperty "java.library.path", "/usr/lib/jni/:."
testClassesDir = sourceSets.itest.output.classesDir
classpath = sourceSets.itest.runtimeClasspath
}
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/org/openmuc/jrxtx/JRxTxPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ class JRxTxPort implements SerialPort {
private FlowControl flowControl;

public static JRxTxPort openSerialPort(String portName, int baudRate, Parity parity, DataBits dataBits,
StopBits stopBits, FlowControl flowControl) throws SerialPortException {
try {
StopBits stopBits, FlowControl flowControl) throws SerialPortException
{
CommPort comPort = null;
try
{
System.setProperty("gnu.io.rxtx.SerialPorts", portName);

CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);

String theOwner = JRxTxPort.class.getCanonicalName() + System.currentTimeMillis();

CommPort comPort = portIdentifier.open(theOwner, 0);
comPort = portIdentifier.open(theOwner, 0);

if (!(comPort instanceof RXTXPort)) {
throw new SerialPortException("Unable to open the serial port. Port is not RXTX.");
Expand All @@ -60,11 +63,11 @@ public static JRxTxPort openSerialPort(String portName, int baudRate, Parity par
RXTXPort rxtxPort = (RXTXPort) comPort;

try {
rxtxPort.setSerialPortParams(baudRate, dataBits.getOldValue(), stopBits.getOldValue(),
parity.getOldValue());
rxtxPort.setSerialPortParams(baudRate, dataBits.getOldValue(), stopBits.getOldValue(), parity.getOldValue());

setFlowControl(flowControl, rxtxPort);
} catch (UnsupportedCommOperationException e) {
}
catch (UnsupportedCommOperationException e) {
String message = format("Unable to apply config on serial port.\n{0}", e.getMessage());
throw new SerialPortException(message);
}
Expand All @@ -76,8 +79,11 @@ public static JRxTxPort openSerialPort(String portName, int baudRate, Parity par
} catch (PortInUseException e) {
String errMessage = format("Serial port {0} is already in use.", portName);
throw new PortNotFoundException(errMessage);
} finally {
if (comPort != null) {
comPort.close();
}
}

}

private static void setFlowControl(FlowControl flowControl, RXTXPort rxtxPort) {
Expand Down

0 comments on commit 454877e

Please sign in to comment.