问题描述
我与一个单元通信以通过串行连接控制卫星天线.
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();
如果设备不可用,我必须先清除流,然后才能发送新命令,一旦设备再次启动.但是,如果设备关闭,输入/输出流上的这种清除方法会阻塞.同时关闭流,或关闭 SerialDevice 块.
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.
有没有办法在不阻塞的情况下关闭/清除这些流或 SerialDevice?
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
推荐答案
我看到这个帖子有一段时间了,我在使用 gnu.io 包(也称为 RXTX)时关闭串行端口时遇到了同样的问题.
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 有两个问题:
RXTX has two problems in my opinion if not more:
- 根据您的 IDE,您需要为 Mac 放置:
RXTXcomm.jar
和librxtxSerial.jnilib
,对于 PC:RXTXcomm.jar
,rxtxSerial.dll
在您的 IDE 或 Java 代码中项目的根目录,它因 IDE 而异.此处的文档不包括如何做到这一点,并且在 NetBeans、IntelliJ 等不同的 IDE 中,即使我让它在 Eclipse 和 IntelliJ 上都可以工作,但还不能在 NetBeans 上工作.它还有其他痛苦的问题. - 根据您的操作系统,即使您启动并运行此软件包,例如在 Windows 8.1 中,关闭端口也会出现问题.唯一的解决方案是重新启动您的 IDE/控制台并重新连接.每次开发项目时,您都可能会疯狂地重新启动 IDE.
- Depending on your IDE, you need to place for Mac:
RXTXcomm.jar
andlibrxtxSerial.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. - Depending on your OS, even if you get this package up and run, in Windows 8.1 as example, it has problem closing the port. And the only solution is to restart your IDE/console and reconnect. You can get crazy restarting your IDE each time while developing a project.
我花了很多时间寻找解决方案,但对于 Windows 8.1 及更高版本可能没有正确关闭端口的解决方案(不知道其他环境关闭端口问题),因为包是旧的并且支持是有限的.
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.
因此,我建议使用更轻松的包 JSSC.
Therefore I suggest going over to a more headache-less package called JSSC.
这是一个简单的使用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.
- 查看所有 JSSC 代码示例
- 可以在这里下载包.
- JSSC 主页
- See all examples of JSSC codes
- The package can be downloaded here.
- JSSC home page
注意:这是一个开放的答案,如果有人对此有相关经验,请通过编辑答案做出贡献.我看到有人在互联网上提出这个问题,并且通常与 RXTX 几乎相同的问题,并且没有为 RXTX 找到可行的解决方案.
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.
我之前回答过另一个人类似问题 在 Stackoverflow 中.
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.
这篇关于Java 串口关闭块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!