Search in sources :

Example 1 with NoSuchPortException

use of gnu.io.NoSuchPortException 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 2 with NoSuchPortException

use of gnu.io.NoSuchPortException 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 3 with NoSuchPortException

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

the class PrimareSerialConnector method connectSerial.

private void connectSerial() throws Exception {
    logger.debug("Initializing serial port {}", serialPortName);
    try {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        serialPort = (SerialPort) commPort;
        try {
            serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            serialPort.enableReceiveThreshold(1);
            serialPort.disableReceiveTimeout();
        } catch (UnsupportedCommOperationException unimportant) {
        // We might have a perfectly usable PTY even if above operations are unsupported
        }
        ;
        inStream = new DataInputStream(serialPort.getInputStream());
        outStream = serialPort.getOutputStream();
        outStream.flush();
        if (inStream.markSupported()) {
            inStream.reset();
        }
        logger.debug("Starting DataListener for {}", PrimareSerialConnector.this.toString());
        dataListener = new DataListener();
        dataListener.start();
        logger.debug("Starting DataListener for {}", PrimareSerialConnector.this.toString());
        sendInitMessages();
    } catch (NoSuchPortException e) {
        logger.error("No such port: {}", serialPortName);
        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
        if (portList.hasMoreElements()) {
            StringBuilder sb = new StringBuilder();
            while (portList.hasMoreElements()) {
                CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
                sb.append(String.format("%s ", portId.getName()));
            }
            logger.error("The following communications ports are available: {}", sb.toString().trim());
        } else {
            logger.error("There are no communications ports available");
        }
        logger.error("You may consider OpenHAB startup parameter [ -Dgnu.io.rxtx.SerialPorts={} ]", serialPortName);
        throw e;
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) NoSuchPortException(gnu.io.NoSuchPortException) Enumeration(java.util.Enumeration) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) DataInputStream(java.io.DataInputStream)

Example 4 with NoSuchPortException

use of gnu.io.NoSuchPortException 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 NoSuchPortException

use of gnu.io.NoSuchPortException 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

NoSuchPortException (gnu.io.NoSuchPortException)17 CommPortIdentifier (gnu.io.CommPortIdentifier)15 PortInUseException (gnu.io.PortInUseException)14 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)14 IOException (java.io.IOException)11 CommPort (gnu.io.CommPort)8 SerialPort (gnu.io.SerialPort)7 TooManyListenersException (java.util.TooManyListenersException)6 DataInputStream (java.io.DataInputStream)4 Enumeration (java.util.Enumeration)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 OutputStreamWriter (java.io.OutputStreamWriter)3 BufferedWriter (java.io.BufferedWriter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 RXTXPort (gnu.io.RXTXPort)1 SerialPortEvent (gnu.io.SerialPortEvent)1 SerialPortEventListener (gnu.io.SerialPortEventListener)1 DataOutputStream (java.io.DataOutputStream)1