Problem receving in RXTX

Posted by drhorrible on Stack Overflow See other posts from Stack Overflow or by drhorrible
Published on 2009-09-08T00:23:55Z Indexed on 2010/04/09 8:43 UTC
Read the original article Hit count: 634

Filed under:
|
|
|

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.

The Problem

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.

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.

Relevant section of connect() method

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

Exception

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>

The big question (again)

I think I've eliminated all possible hardware problems, so what could be wrong with my code, or the RXTX library?

Edit: something interesting

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.

Edit 2: Tried something new, same results

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.

Edit 3

The hardware is fine, and even a different computer has the same problem. I am not using any sort of USB adapter.

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.

Edit 4

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.

Edit 5

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)

  • HyperTerminal - set to no flow control, but Serial Port Monitor's RTS and DTR indicators are green
  • Other program - not sure what settings it thinks it's using, but only SPM's RTS indicator is green
  • RXTX - no matter what flow control I set, only SPM's CTS and DTR indicators are on.

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

© Stack Overflow or respective owner

Related posts about java

Related posts about serial