Search in sources :

Example 1 with RXTXPort

use of gnu.io.RXTXPort in project openhab1-addons by openhab.

the class Connection method open.

/**
     * Opens the serial port associated with this connection.
     * 
     * @throws IOException
     *             if any kind of error occurs opening the serial port.
     */
public void open() throws IOException {
    CommPortIdentifier portIdentifier;
    try {
        portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
    } catch (NoSuchPortException e) {
        throw new IOException("Serial port with given name does not exist", e);
    }
    if (portIdentifier.isCurrentlyOwned()) {
        throw new IOException("Serial port is currently in use.");
    }
    // fixed issue as rxtx library originally used in j62056 does use
    // different version of rxtx
    // com port in their version is using gnu.io.CommPort
    RXTXPort commPort;
    try {
        commPort = portIdentifier.open(this.getClass().getName(), 2000);
    } catch (PortInUseException e) {
        throw new IOException("Serial port is currently in use.", e);
    }
    if (!(commPort instanceof SerialPort)) {
        commPort.close();
        throw new IOException("The specified CommPort is not a serial port");
    }
    serialPort = commPort;
    try {
        os = new DataOutputStream(serialPort.getOutputStream());
        is = new DataInputStream(serialPort.getInputStream());
    } catch (IOException e) {
        serialPort.close();
        serialPort = null;
        throw new IOException("Error getting input or output or input stream from serial port", e);
    }
}
Also used : RXTXPort(gnu.io.RXTXPort) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) SerialPort(gnu.io.SerialPort) CommPortIdentifier(gnu.io.CommPortIdentifier) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Aggregations

CommPortIdentifier (gnu.io.CommPortIdentifier)1 NoSuchPortException (gnu.io.NoSuchPortException)1 PortInUseException (gnu.io.PortInUseException)1 RXTXPort (gnu.io.RXTXPort)1 SerialPort (gnu.io.SerialPort)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1