在RXTX中收到的问题 [英] Issues receiving in 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.
问题
设备工作正常,因为当我连接HyperTerminal时,我发送东西并收到我的期望,并且串行端口监视器(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.
然而,当我运行简单的HyperTerminal-clone时,我编写了诊断我的主应用程序的问题,根据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?
编辑:有趣的东西
当我在发送一堆后打开HyperTerminal时来自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 :尝试了一些新的,相同的结果
我运行了代码示例这里,结果相同。没有数据进入,但当我切换到新程序时,它立刻就出现了。
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 ,它给了我一些有趣的结果。 HyperTerminal和RXTX没有使用相同的设置,并且RXTX总是轮询端口,与HyperTerminal不同,但我仍然无法看到哪些设置会影响这一点。一旦我可以将配置与常量轮询隔离,我就会发布我的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
我还注意到HyperTerminal,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)
- 超级终端 - 设置为无流量控制,但串口监视器的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.
注意:此方法 NOT 适用于所有人,请在复制/粘贴之前阅读以下内容你自己的代码
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()
方法,并按位OR('|
')必要的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
- 使用串口监控程序,如串口监控器和/或< a href =http://technet.microsoft.com/en-us/sysinternals/bb896644.aspx =noreferrer> 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)
这篇关于在RXTX中收到的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!