Issues receiving in RXTX

后端 未结 6 1790
慢半拍i
慢半拍i 2020-12-08 17:47

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

6条回答
  •  孤城傲影
    2020-12-08 18:37

    OK, sorry it's taken me so long to come back to this question. Here's how I got things working.

    Note: This method will NOT work for everyone, please read below before copy/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, in my case, the problem was that my particular device requires RTS flow control. Other devices may require different things (CTS, XON/XOFF), so check that device's manual. By default, RXTX disables all flow control mechanisms (unlike Hypertrm or other programs). Enabling each one is a two-step process.

    1. Once you have a SerialPort object, call the setFlowControlMode() method, and bitwise-OR ('|') the necessary SerialPort.FLOWCONTROL_ constants
    2. Set the appropriate flow control to true or false (like I did with port.setRTS(true))

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

    1. Using a serial port monitoring program like Serial Port Monitor and/or PortMon (both Windows) to see what is actually going on.
    2. Emailing the RXTX developers at rxtx@qbang.org (they are very helpful)

提交回复
热议问题