Note that there are some explanatory texts on larger screens.

plurals
  1. PORXTX serial connection - issue with blocking read()
    primarykey
    data
    text
    <p>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.</p> <p>I set up the connection with the following code: (exception handling and defensive checks omitted for clarity)</p> <pre><code>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())); </code></pre> <p>When I use </p> <pre><code>outWriter.println("test message"); flush(); </code></pre> <p>the message is recieved fine on the other end, but calling</p> <pre><code>inReader.readLine() </code></pre> <p>imidiately returns "java.io.IOException: Underlying input stream returned zero bytes".</p> <p>I then decided to try and implement my own blocking read logic and wrote this:</p> <pre><code>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; } } } </code></pre> <p>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.</p> <p>Any help very appreciated.</p> <p>*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.</p> <p>*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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload