我正在使用RXTX从串口读取数据.读取是在以下列方式生成的线程内完成的:
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port);
CommPort comm = portIdentifier.open("Whatever", 2000);
SerialPort serial = (SerialPort)comm;
...settings
Thread t = new Thread(new SerialReader(serial.getInputStream()));
t.start();
SerialReader类实现Runnable并且只是无限循环,从端口读取并将数据构建到有用的包中,然后再将其发送到其他应用程序.但是,我把它简化为以下简单:
public void run() {
ReadableByteChannel byteChan = Channels.newChannel(in); //in = InputStream passed to SerialReader
ByteBuffer buffer = ByteBuffer.allocate(100);
while (true) {
try {
byteChan.read(buffer);
} catch (Exception e) {
System.out.println(e);
}
}
}
当用户单击停止按钮时,将触发以下功能,理论上应关闭输入流并中断阻塞byteChan.read(缓冲)调用.代码如下:
public void stop() {
t.interrupt();
serial.close();
}
但是,当我运行此代码时,我从未得到ClosedByInterruptException,一旦输入流关闭,它应该触发.此外,执行阻止调用serial.close() – 因为底层输入流仍然在读取调用上阻塞.我已经尝试用byteChan.close()替换中断调用,然后应该导致AsynchronousCloseException,但是,我得到了相同的结果.
对我所缺少的任何帮助将不胜感激.