Search in sources :

Example 1 with PortInUseException

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

the class SerialWR3223Connector method connect.

/**
     * Connect to WR2332 over serial port.
     *
     * @param port
     * @param baud
     * @throws IOException
     */
public void connect(String port, int baud) throws IOException {
    // parse ports and if the default port is found, initialized the reader
    CommPortIdentifier portId = null;
    Enumeration portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
        if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (id.getName().equals(port)) {
                portId = id;
            }
        }
    }
    if (portId != null) {
        // initialize serial port
        try {
            serialPort = portId.open("wr3223", 2000);
        } catch (PortInUseException e) {
            throw new IOException("Serial port '" + port + "' is already in use.", e);
        }
        try {
            // set port parameters
            serialPort.setSerialPortParams(baud, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
        } catch (UnsupportedCommOperationException e) {
            throw new IOException("Serial port '" + port + "' doesn't support the configuration 7 data bit, 1 stop bit and parity even.", e);
        }
        if (!serialPort.isReceiveTimeoutEnabled()) {
            try {
                if (logger.isDebugEnabled()) {
                    logger.debug("Add a receive timeout of 2000ms.");
                }
                serialPort.enableReceiveTimeout(2000);
            } catch (UnsupportedCommOperationException e) {
                logger.warn("Error by adding receive timeout.", e);
            }
        }
        DataInputStream inputStream = new DataInputStream(serialPort.getInputStream());
        DataOutputStream outputStream = new DataOutputStream(serialPort.getOutputStream());
        connect(inputStream, outputStream);
    } else {
        StringBuilder sb = new StringBuilder();
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
            if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                sb.append(id.getName() + "\n");
            }
        }
        throw new IOException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) Enumeration(java.util.Enumeration) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 2 with PortInUseException

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

the class UPBBinding method openSerialPort.

private SerialPort openSerialPort() {
    SerialPort serialPort = null;
    CommPortIdentifier portId;
    try {
        portId = CommPortIdentifier.getPortIdentifier(port);
    } catch (NoSuchPortException e1) {
        throw new RuntimeException("Port does not exist", e1);
    }
    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        if (portId.getName().equals(port)) {
            try {
                serialPort = portId.open("UPB", 1000);
            } catch (PortInUseException e) {
                throw new RuntimeException("Port is in use", e);
            }
            try {
                serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
                serialPort.enableReceiveTimeout(100);
            } catch (UnsupportedCommOperationException e) {
                throw new RuntimeException("Failed to configure serial port");
            }
        }
    }
    return serialPort;
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) SerialPort(gnu.io.SerialPort) CommPortIdentifier(gnu.io.CommPortIdentifier)

Example 3 with PortInUseException

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

the class ZWaveController method connect.

// Controller methods
/**
     * Connects to the comm port and starts send and receive threads.
     *
     * @param serialPortName the port name to open
     * @throws SerialInterfaceException when a connection error occurs.
     */
public void connect(final String serialPortName) throws SerialInterfaceException {
    logger.info("Connecting to serial port {}", serialPortName);
    try {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
        CommPort commPort = portIdentifier.open("org.openhab.binding.zwave", 2000);
        this.serialPort = (SerialPort) commPort;
        this.serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        this.serialPort.enableReceiveThreshold(1);
        this.serialPort.enableReceiveTimeout(ZWAVE_RECEIVE_TIMEOUT);
        this.receiveThread = new ZWaveReceiveThread();
        this.receiveThread.start();
        this.sendThread = new ZWaveSendThread();
        this.sendThread.start();
        this.inputThread = new ZWaveInputThread();
        this.inputThread.start();
        // RXTX serial port library causes high CPU load
        // Start event listener, which will just sleep and slow down event loop
        serialPort.addEventListener(this.receiveThread);
        serialPort.notifyOnDataAvailable(true);
        logger.info("Serial port is initialized");
    } catch (NoSuchPortException e) {
        logger.error("Serial Error: Port {} does not exist", serialPortName);
        throw new SerialInterfaceException(String.format("Port %s does not exist", serialPortName), e);
    } catch (PortInUseException e) {
        logger.error("Serial Error: Port {} in use.", serialPortName);
        throw new SerialInterfaceException(String.format("Port %s in use.", serialPortName), e);
    } catch (UnsupportedCommOperationException e) {
        logger.error("Serial Error: Unsupported comm operation on Port {}.", serialPortName);
        throw new SerialInterfaceException(String.format("Unsupported comm operation on Port %s.", serialPortName), e);
    } catch (TooManyListenersException e) {
        logger.error("Serial Error: Too many listeners on Port {}.", serialPortName);
        e.printStackTrace();
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) TooManyListenersException(java.util.TooManyListenersException) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort)

Example 4 with PortInUseException

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

the class CULSerialHandlerImpl method openHardware.

@Override
protected void openHardware() throws CULDeviceException {
    String deviceName = config.getDeviceAddress();
    logger.debug("Opening serial CUL connection for {}", deviceName);
    try {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(deviceName);
        if (portIdentifier.isCurrentlyOwned()) {
            throw new CULDeviceException("The port " + deviceName + " is currenty used by " + portIdentifier.getCurrentOwner());
        }
        CommPort port = portIdentifier.open(this.getClass().getName(), 2000);
        if (!(port instanceof SerialPort)) {
            throw new CULDeviceException("The device " + deviceName + " is not a serial port");
        }
        serialPort = (SerialPort) port;
        serialPort.setSerialPortParams(config.getBaudRate(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1, config.getParityMode());
        InputStream is = serialPort.getInputStream();
        OutputStream os = serialPort.getOutputStream();
        synchronized (serialPort) {
            br = new BufferedReader(new InputStreamReader(is));
            bw = new BufferedWriter(new OutputStreamWriter(os));
        }
        serialPort.notifyOnDataAvailable(true);
        logger.debug("Adding serial port event listener");
        serialPort.addEventListener(this);
    } catch (NoSuchPortException e) {
        throw new CULDeviceException(e);
    } catch (PortInUseException e) {
        throw new CULDeviceException(e);
    } catch (UnsupportedCommOperationException e) {
        throw new CULDeviceException(e);
    } catch (IOException e) {
        throw new CULDeviceException(e);
    } catch (TooManyListenersException e) {
        throw new CULDeviceException(e);
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) InputStreamReader(java.io.InputStreamReader) CommPortIdentifier(gnu.io.CommPortIdentifier) CULDeviceException(org.openhab.io.transport.cul.CULDeviceException) CommPort(gnu.io.CommPort) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) TooManyListenersException(java.util.TooManyListenersException) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) SerialPort(gnu.io.SerialPort) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter)

Example 5 with PortInUseException

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

the class ComfoAirConnector method open.

/**
     * Open and initialize a serial port.
     *
     * @param portName
     *            e.g. /dev/ttyS0
     * @param listener
     *            the listener which is informed after a successful response
     *            read
     * @throws InitializationException
     */
public void open(String portName) throws InitializationException {
    logger.debug("Open ComfoAir connection");
    port = portName;
    CommPortIdentifier portIdentifier;
    try {
        portIdentifier = CommPortIdentifier.getPortIdentifier(port);
        try {
            serialPort = portIdentifier.open("openhab", 3000);
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            serialPort.enableReceiveThreshold(1);
            serialPort.enableReceiveTimeout(1000);
            // RXTX serial port library causes high CPU load
            // Start event listener, which will just sleep and slow down event loop
            serialPort.addEventListener(new CPUWorkaroundThread());
            serialPort.notifyOnDataAvailable(true);
            inputStream = new DataInputStream(new BufferedInputStream(serialPort.getInputStream()));
            outputStream = serialPort.getOutputStream();
            ComfoAirCommand command = ComfoAirCommandType.getChangeCommand(ComfoAirCommandType.ACTIVATE.key, new DecimalType(1));
            sendCommand(command);
        } catch (PortInUseException e) {
            throw new InitializationException(e);
        } catch (UnsupportedCommOperationException e) {
            throw new InitializationException(e);
        } catch (IOException e) {
            throw new InitializationException(e);
        } catch (TooManyListenersException e) {
            throw new InitializationException(e);
        }
    } catch (NoSuchPortException e) {
        StringBuilder sb = new StringBuilder();
        @SuppressWarnings("rawtypes") Enumeration portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
            if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                sb.append(id.getName() + "\n");
            }
        }
        throw new InitializationException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) Enumeration(java.util.Enumeration) CommPortIdentifier(gnu.io.CommPortIdentifier) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) InitializationException(org.openhab.binding.comfoair.internal.InitializationException) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) NoSuchPortException(gnu.io.NoSuchPortException) BufferedInputStream(java.io.BufferedInputStream) DecimalType(org.openhab.core.library.types.DecimalType)

Aggregations

PortInUseException (gnu.io.PortInUseException)64 CommPortIdentifier (gnu.io.CommPortIdentifier)63 IOException (java.io.IOException)29 SerialPortEvent (gnu.io.SerialPortEvent)25 SerialPortEventListener (gnu.io.SerialPortEventListener)23 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)20 TooManyListenersException (java.util.TooManyListenersException)16 NoSuchPortException (gnu.io.NoSuchPortException)14 DataInputStream (java.io.DataInputStream)8 CommPort (gnu.io.CommPort)7 SerialPort (gnu.io.SerialPort)6 Enumeration (java.util.Enumeration)5 BufferedReader (java.io.BufferedReader)4 InputStreamReader (java.io.InputStreamReader)4 OutputStreamWriter (java.io.OutputStreamWriter)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedWriter (java.io.BufferedWriter)3 DataOutputStream (java.io.DataOutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 RXTXPort (gnu.io.RXTXPort)1