RXTX 接收问题
我已经使用 RXTX 大约一年了,没有太多问题.我刚刚启动了一个新程序来与一个新硬件进行交互,所以我重用了我在其他项目中使用过的 connect() 方法,但是我遇到了一个我以前从未见过的奇怪问题.
I've been using RXTX for about a year now, without too many problems. I just started a new program to interact with a new piece of hardware, so I reused the connect() method I've used on my other projects, but I have a weird problem I've never seen before.
问题
设备工作正常,因为当我连接超级终端时,我发送并接收我期望的内容,并且 串行端口监视器(SPM) 反映了这一点.
The device works fine, because when I connect with HyperTerminal, I send things and receive what I expect, and Serial Port Monitor(SPM) reflects this.
但是,当我运行我编写的用于诊断我的主应用程序问题的简单超级终端克隆时,根据 SPM,发送了字节,但没有收到任何内容,并且我的 SerialPortEventListener 永远不会触发.即使我在主循环中检查可用数据,reader.ready()
也会返回 false
.如果我忽略此检查,则会出现异常,详情如下.
However, when I run the simple HyperTerminal-clone I wrote to diagnose the problem I'm having with my main app, bytes are sent, according to SPM, but nothing is received, and my SerialPortEventListener never fires. Even when I check for available data in the main loop, reader.ready()
returns false
. If I ignore this check, then I get an exception, details below.
connect()方法的相关部分
// Configure and open port
port = (SerialPort) CommPortIdentifier.getPortIdentifier(name)
.open(owner,1000)
port.setSerialPortParams(baud, databits, stopbits, parity);
port.setFlowControlMode(fc_mode);
final BufferedReader br = new BufferedReader(
new InputStreamReader(
port.getInputStream(),
"US-ASCII"));
// Add listener to print received characters to screen
port.addEventListener(new SerialPortEventListener(){
public void serialEvent(SerialPortEvent ev) {
try {
System.out.println("Received: "+br.readLine());
} catch (IOException e) { e.printStackTrace(); }
}
});
port.notifyOnDataAvailable();
例外
java.io.IOException: Underlying input stream returned zero bytes
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:268)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.read(BufferedReader.java:157)
at <my code>
大问题(再次)
我认为我已经消除了所有可能的硬件问题,那么我的代码或 RXTX 库可能有什么问题?
I think I've eliminated all possible hardware problems, so what could be wrong with my code, or the RXTX library?
有趣的事情
当我从 java 发送了一堆本应得到响应的命令后打开超级终端时,所有响应都会立即出现,就好像它们已被放入缓冲区中的某个地方,但不可用.
When I open HyperTerminal after sending a bunch of commands from java that should have gotten responses, all of the responses appear immediately, as if they had been put in the buffer somewhere, but unavailable.
编辑 2:尝试了新的东西,结果相同
我运行 here 的代码示例,结果相同.没有数据进来,但是当我切换到一个新程序时,它一下子就进来了.
I ran the code example found here, with the same results. No data came in, but when I switched to a new program, it came all at once.
编辑 3
硬件很好,即使是不同的计算机也有同样的问题.我没有使用任何类型的 USB 适配器.
The hardware is fine, and even a different computer has the same problem. I am not using any sort of USB adapter.
我也开始使用 PortMon,它是给我一些有趣的结果.超级终端和 RXTX 没有使用相同的设置,并且 RXTX 总是轮询端口,这与超级终端不同,但我仍然看不到什么设置会影响这一点.一旦我可以将配置与持续轮询隔离开来,我就会发布我的 PortMon 日志.
I've started using PortMon, too, and it's giving me some interesting results. HyperTerminal and RXTX are not using the same settings, and RXTX always polls the port, unlike HyperTerminal, but I still can't see what settings would affect this. As soon as I can isolate the configuration from the constant polling, I'll post my PortMon logs.
编辑 4
是否有可能是过去 3 个月内的某种 Windows 更新导致了这种情况?它曾经把我的一个基于 MATLAB mex 的程序搞砸了.
Is it possible that some sort of Windows update in the last 3 months could have caused this? It has screwed up one of my MATLAB mex-based programs once.
编辑 5
我还注意到超级终端、RXTX 和我发现的与设备通信的单独程序之间的一些不同之处(但没有做我想要的,这就是我推出自己的程序的原因)
I've also noticed some things that are different between HyperTerminal, RXTX, and a separate program I found that communicates with the device (but doesn't do what I want, which is why I'm rolling my own program)
- 超级终端 - 设置为无流量控制,但 Serial Port Monitor 的 RTS 和 DTR 指示灯为绿色
- 其他程序 - 不确定它认为它正在使用什么设置,但只有 SPM 的 RTS 指示灯是绿色的
- RXTX - 无论我设置什么流控,只有 SPM 的 CTS 和 DTR 指示灯亮.
来自 Serial Port Monitor 的帮助文件(释义):
From Serial Port Monitor's help files (paraphrased):
the indicators display the state of the serial control lines
RTS - Request To Send
CTS - Clear To Send
DTR - Data Terminal Ready
推荐答案
好的,抱歉,我花了这么长时间才回到这个问题.以下是我如何让事情发挥作用的方法.
OK, sorry it's taken me so long to come back to this question. Here's how I got things working.
注意:此方法并非适用于所有人,请在复制/粘贴到您自己的代码之前阅读以下内容
Note: This method will NOT work for everyone, please read below before copy/pasting into your own code
public void connect(CommPortIdentifier portId) throws Failure {
if (portId == null)
throw new Failure("No port set");
try { port = (SerialPort) portId.open(getClass().getName(), 10000); }
catch (PortInUseException e) {
throw new Failure("Port in use by " + e.currentOwner,e); }
try {
port.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN
| SerialPort.FLOWCONTROL_RTSCTS_OUT);
} catch (UnsupportedCommOperationException e) { throw new Failure(e); }
port.setRTS(true);
// More setup
}
因此,就我而言,问题在于我的特定设备需要 RTS 流控制.其他设备可能需要不同的东西(CTS、XON/XOFF),因此请查看该设备的手册.默认情况下,RXTX 禁用所有流控制机制(与 Hypertrm 或其他程序不同).启用每一个都是一个两步过程.
So, in my case, the problem was that my particular device requires RTS flow control. Other devices may require different things (CTS, XON/XOFF), so check that device's manual. By default, RXTX disables all flow control mechanisms (unlike Hypertrm or other programs). Enabling each one is a two-step process.
- 一旦你有一个 SerialPort 对象,调用
setFlowControlMode()
方法,并按位或 ('|
') 必要的SerialPort.FLOWCONTROL_代码> 常量
- 将适当的流控制设置为 true 或 false(就像我对
port.setRTS(true)
所做的那样)
- Once you have a SerialPort object, call the
setFlowControlMode()
method, and bitwise-OR ('|
') the necessarySerialPort.FLOWCONTROL_
constants - Set the appropriate flow control to true or false (like I did with
port.setRTS(true)
)
对于其他有类似问题的人,如果这不起作用,我建议
For the others with similar problems, if this doesn't work, I suggest
- 使用串行端口监控程序,如 Serial Port Monitor 和/或 PortMon(两个 Windows)查看实际情况.
- 通过 rxtx@qbang.org 向 RXTX 开发人员发送电子邮件(他们非常有帮助)
- Using a serial port monitoring program like Serial Port Monitor and/or PortMon (both Windows) to see what is actually going on.
- Emailing the RXTX developers at rxtx@qbang.org (they are very helpful)
相关文章