RXTX serial connection - read lock problem ()

I am trying to use the RXTX library to block serial communication on Windows (XP and 7). I tested the connection to Hyperterminal at both ends and it works flawlessly.

I established a connection with the following code: (exception handling and security 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())); 

When i use

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

the message receives a penalty at the other end, but causes

 inReader.readLine() 

imidiately returns "java.io.IOException: the underlying input stream returns null bytes".

Then I decided to try and implement my own lock reading logic and wrote the following:

 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; } } } 

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

Any help is greatly appreciated.

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

* edit2 - since my code works flawlessly with the SUN javax.comm lib and win32com.dll, which I received from a friend, I stopped trying to get it to work with RXTX. I'm not interested in unlocking communications, which seems to be the only way other people can use RXTX.

+2
java serial-port rxtx
Mar 07 '11 at 12:00
source share
4 answers

Use RXTX-2.2pre2, previous versions had an error that prevented the correct operation of blocking I / O.

And don't forget to set the port to lock mode:

 serialPort.disableReceiveTimeout(); serialPort.enableReceiveThreshold(1); 
+12
May 09 '11 at 9:32 a.m.
source share

I think the code you wrote in your own readLine implementation is a mistake. nextByte [0] is never restored to -1 after reading the first character. You should try to use the value returned by inStream.read (nextByte) to indicate the number of bytes read from the stream, instead of the value of your byte array.

In any case, I think you should go for an event-based method to read the inputs using SerialPortEventListener:

 serialPort.addEventListener(new SerialPortEventListener() { public void serialEvent(SerialPortEvent evt) { switch (evt.getEventType()) { case SerialPortEvent.DATA_AVAILABLE: dataReceived(); break; default: break; } } }); serialPort.notifyOnDataAvailable(true); 
+2
Mar 07 2018-11-11T00:
source share

it may not block, but when the stream is empty, just catch the IOE and keep reading from it. This is what I do with RXTX-2.1-7 and it works great, I use it to read and write in 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 just whining Thread.sleep(200); } catch (InterruptedException ex) { // something interrupted our sleep, exit ... keepRunning = false; } } } } } 
+1
Aug 24 2018-11-21T00:
source share

I decided this way

 try { if(input.ready()==true) { String inputLine=input.readLine(); System.out.println(inputLine); } } catch (Exception e) 
0
Nov 12 '14 at 21:35
source share



All Articles