I communicate with a unit to control a satellite antenna via a serial connection.
Opening a connection with the serial device:
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
portIdentifier = CommPortIdentifier.getPortIdentifier(device);
serialPort = (SerialPort) portIdentifier.open(name,
serialPort.setSerialPortParams(baudrate, databits, stopbits, parity);
serialPort.setFlowControlMode(flowMode);
bufferedReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
outputStream = serialPort.getOutputStream();
If the unit is becomes unavailable, I have to clear the stream before being able to send new commands, once the device is up again. But this clear method on the input/output stream blocks if the device is down. Also closing the streams, or closing the SerialDevice blocks.
Is there a way to close/clear these streams or SerialDevice without blocking?
bufferedRead.close(); // blocks until device is up again
outputStream.close(); // blocks until device is up again
serialPort.close(); // blocks until device is up again
I have seen this post for while, and I have had the same issue closing serialport when using gnu.io package, which is also called RXTX.
This is not a final answer, but a suggestion to alternative solution I found.
RXTX has two problems in my opinion if not more:
RXTXcomm.jar
and librxtxSerial.jnilib
and for PC: RXTXcomm.jar
, rxtxSerial.dll
on the root of the project in your IDE or Java code, it varies from IDE to IDE. The documentation here does not cover how to do it, and in different IDE like NetBeans, IntelliJ even if I got it to work on both Eclipse and IntelliJ, but not NetBeans yet. It still have other painful issues.I have spend a lot of hours finding a solution and there is no solution for closing the port correctly perhaps for Windows 8.1 and later (don't know about other environment closing port problem), as the package is old and the support is limited.
Therefore I suggest going over to a more headache-less package called JSSC.
Here is a simple reading data from serial port using JSSC:
public class Main {
public static void main(String[] args) {
SerialPort serialPort = new SerialPort("COM1");
try {
serialPort.openPort();//Open serial port
serialPort.setParams(9600, 8, 1, 0);//Set params.
byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port
serialPort.closePort();//Close serial port
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
}
And ya, it closes the port with out problem.
Note: This is an open answer, if any one have relevant experience regarding this please contribute by editing the answer. I have seen people asking this question around on the internet and having almost same problem with RXTX in general, and have not found a workable solution for RXTX.
I have answered another guy with similar previously question in Stackoverflow.
I wanted to share the knowledge I have, it could be useful for those facing same issue. It could make your day less painful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With