本文介绍了RXTX 串行连接 - 阻塞 read() 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RXTX 库来阻止 Windows(XP 和 7)上的串行通信.我已经在两端测试了与 Hyperterminal 的连接,并且可以完美运行.

I am trying to use the RXTX library for blocking serial communication on Windows (XP and 7). I have tested the connection with Hyperterminal in both ends, and it works flawlessly.

我使用以下代码设置连接:(为清楚起见,省略了异常处理和防御检查)

I set up the connection with the following code: (exception handling and defensive checks omitted for clarity)

private InputStream inStream;
private OutputStream outStream;
private BufferedReader inReader;
private PrintWriter outWriter;
private SerialPort serialPort;
private final String serialPortName;

public StreamComSerial(String serialPortName) {
this.serialPortName = serialPortName;
CommPortIdentifier portIdentifier;
portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
CommPort commPort = null;
commPort = portIdentifier.open(this.getClass().getName(),500);
serialPort = (SerialPort) commPort;    serialPort.setSerialPortParams(4800,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
inStream = serialPort.getInputStream();
outStream = serialPort.getOutputStream();
inReader = new BufferedReader(new InputStreamReader(inStream, Settings.getCharset()));
outWriter = new PrintWriter(new OutputStreamWriter(outStream, Settings.getCharset()));

当我使用时

outWriter.println("test message");
flush();

消息在另一端接收良好,但呼叫

the message is recieved fine on the other end, but calling

inReader.readLine()

立即返回java.io.IOException:基础输入流返回零字节".

imidiately returns "java.io.IOException: Underlying input stream returned zero bytes".

然后我决定尝试实现我自己的阻塞读取逻辑并写了这个:

I then decided to try and implement my own blocking read logic and wrote this:

public String readLine() throws IOException {
    String line = new String();
    byte[] nextByte = {-1};
    while (true) {
        nextByte[0] = (byte)inStream.read();
        logger.debug("int read: " + nextByte[0]);
        if (nextByte[0] == (byte)-1) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            continue;
        }
        logger.debug("byte read: " + nextByte[0]);

        line = line + new String(nextByte);
        if (nextByte[0] == (byte)13) {  // 13 is carriage return in ASCII
            return line;
        }
    }
}

但是这段代码进入了一个无限循环并且nextByte[0] = (byte)inStream.read();"无论通过串行连接发送什么,都分配 -1.此外,另一端的口吃很严重,只让我每 1-3 秒发送一个字符.如果我尝试在短时间内发送许多字符,则会挂起很长时间.

But this code goes in an infinite loop and "nextByte[0] = (byte)inStream.read();" assigns -1 no matter what is sent over the serial connection. In addition, the other end stutters quite badly and only lets me send a character every 1-3 sec. and hangs for a long time if I try to send many characters in a short burst.

非常感谢任何帮助.

*edit - 使用 inStream.read(nextByte) 而不是 "nextByte[0] = (byte)inStream.read();"不会写入 nextByte 变量,无论我通过串行连接发送什么.

*edit - using inStream.read(nextByte) instead of "nextByte[0] = (byte)inStream.read();" does not write to the nextByte variable, no matter what I send to it through the serial connection.

*edit2 - 由于我的代码可以完美地与 SUN javax.comm 库和我从朋友那里得到的 win32com.dll 一起工作,我已经停止尝试让它与 RXTX 一起工作.我对解锁通信不感兴趣,这似乎是其他人可以使 RXTX 工作的唯一方法.

*edit2 - as my code works flawlessly with the SUN javax.comm lib and a win32com.dll I got from a friend, I have ceased trying to make it work with RXTX. I am not interested in unblocking communication, which seems to be the only way other people can make RXTX work.

推荐答案

使用 RXTX-2.2pre2,之前的版本有一个 bug 导致阻塞 I/O 无法正常工作.

Use RXTX-2.2pre2, previous versions have had a bug which prevented blocking I/O from working correctly.

并且不要忘记将端口设置为阻塞模式:

And do not forget to set port to blocking mode:

serialPort.disableReceiveTimeout();
serialPort.enableReceiveThreshold(1);

这篇关于RXTX 串行连接 - 阻塞 read() 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-19 02:31