RXTX串行连接 – 阻塞read()的问题

我试图使用RXTX库来阻止Windows上的串行通信(XP和7)。 我已经在两端测试了与Hyperterminal的连接,并且它完美无缺。

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

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(); 

在另一端收到的消息很好,但是打电话

 inReader.readLine() 

imidiately返回“java.io.IOException:底层输入流返回零字节”。

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

 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秒发送一个角色。 如果我尝试在短暂的爆发中发送许多字符,并且会挂起很长时间。

任何帮助非常感谢。

* edit – 使用inStream.read(nextByte)而不是“nextByte [0] =(byte)inStream.read();” 不写入nextByte变量,无论我通过串行连接发送给它。

* edit2 – 由于我的代码与SUN javax.comm lib以及我从朋友那里获得的win32com.dll完美运行,我已经停止尝试使其与RXTX一起使用。 我对解锁通信不感兴趣,这似乎是其他人可以使RXTX工作的唯一方式。

使用RXTX-2.2pre2,以前的版本有一个错误,阻止I / O正常工作。

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

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

我认为你在自己的readLine实现中编写的代码是错误的。 在读取第一个字符后,nextByte [0]永远不会恢复为-1。 您应该尝试使用inStream.read(nextByte)返回的值来表示从流中读取的字节数,而不是字节数组的值。

无论如何,我认为您应该使用SerialPortEventListener来读取输入的基于事件的方法:

 serialPort.addEventListener(new SerialPortEventListener() { public void serialEvent(SerialPortEvent evt) { switch (evt.getEventType()) { case SerialPortEvent.DATA_AVAILABLE: dataReceived(); break; default: break; } } }); serialPort.notifyOnDataAvailable(true); 

它可能没有阻塞,但是当流为空时,只需捕获IOE并继续读取它。 这是我用RXTX-2.1-7做的,它工作正常,我用它来读写arduino:

 public static class SerialReader implements Runnable { InputStream in; public SerialReader(InputStream in) { this.in = in; } public void run() { Boolean keepRunning = true; BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; while (keepRunning) { try { while ((line = br.readLine()) != null) { //DO YOUR STUFF HERE } } catch (IOException e) { try { //ignore it, the stream is temporarily empty,RXTX's just whining Thread.sleep(200); } catch (InterruptedException ex) { // something interrupted our sleep, exit ... keepRunning = false; } } } } } 

我已经解决了这个问题

 try { if(input.ready()==true) { String inputLine=input.readLine(); System.out.println(inputLine); } } catch (Exception e)