Java – problems received in rxtx

I've been using rxtx for about a year without too many problems I just started a new program to interact with a new hardware, so I reused the connect () method I used in other projects, but I had a strange problem I had never seen before

problem

The device works normally because when I connect to HyperTerminal, I send something and receive what I expect, which is reflected in the serial port monitor (SPM)

However, when I run the simple HyperTerminal clone, I write a program to diagnose the problem of my main application. I send bytes according to SPM, but I don't receive anything. My serialporteventlistener will never trigger Even if I check the available data in the main loop, reader Ready () will also return false If I ignore this check, I will get an exception, as follows

Relevant parts of the 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 most important question (again)

I think I have eliminated all possible hardware problems, so what problems may occur in my code or rxtx library?

Editor: interesting things

When I open HyperTerminal after sending a bunch of commands from Java that should receive a response, all the responses will appear immediately, as if they had been placed in a buffer somewhere, but they are not available

Edit 2: tried some new, same results

I ran the code example here and the results were the same No data entered, but when I switched to a new program, it immediately appeared

Edit 3

The hardware is very good, and even different computers have the same problem I don't use any type of USB adapter

I also started using portmon, which gave me some interesting results HyperTerminal and rxtx do not use the same settings, and rxtx always polls ports, which is different from HyperTerminal, but I still can't see which settings affect this As long as I can isolate the configuration from constant polling, I will publish my portmon log

Edit 4

Is it possible that some type of windows update may have caused this problem in the past 3 months? It used to do my program based on MATLAB mex

Edit 5

I also noticed that there were some differences between HyperTerminal, rxtx and the individual programs I found communicating with the device (but I didn't do what I wanted, which is why I was writing my own programs)

>HyperTerminal – set to no flow control, but the RTS and DTR indicators of the serial port monitor are green > other programs – not sure what settings it thinks it is using, but only the RTS indicator of SPM is green > rxtx – no matter what flow control I set, only the CTS and DTR indicators of SPM are on

From the help file of serial port monitor (paraphrase):

the indicators display the state of the serial control lines

  RTS - Request To Send
  CTS - Clear To Send
  DTR - Data Terminal Ready

Solution

OK, sorry, it took me a long time to get back to this question This is the way I make things work

Note: this method does not apply to everyone, please read the following before copying / pasting into your own code

public void connect(CommPortIdentifier portId) throws Failure {
    if (portId == null)
        throw new Failure("No port set");

    try { port = (SerialPort) portId.open(getClass().getName(),10000); } 
    catch (PortInUseException e) {
        throw new Failure("Port in use by " + e.currentOwner,e); }

    try {
        port.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
        port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN
                              | SerialPort.FLOWCONTROL_RTSCTS_OUT);
    } catch (UnsupportedCommOperationException e) { throw new Failure(e); }

    port.setRTS(true);

    // More setup
}

So, as far as I'm concerned, the problem is that my specific device needs RTS flow control Other equipment may require different things (CTS, Xon / XOFF), so please check the manual of the equipment By default, rxtx disables all flow control mechanisms (unlike hypertrm or other programs) Enabling each process is divided into two steps

>Once you have the serialport object, you call the setflowcontrolmode () method and call the necessary serialport by bit or ('|') FLOWCONTROL_ Constant > set the appropriate flow control to true or false (just as I used port. Setrts (true))

For others with similar problems, if this doesn't work, I suggest

>Use serial port monitor and / or portmon (both windows) and other serial port monitoring programs to view the actual situation. > adopt rxtx@qbang.org Email rxtx developers (they are very helpful)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>