Search in sources :

Example 16 with UnsupportedCommOperationException

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

the class SerialIOStream method open.

@Override
public boolean open() {
    try {
        updateSerialProperties(m_devName);
        CommPortIdentifier ci = CommPortIdentifier.getPortIdentifier(m_devName);
        CommPort cp = ci.open(m_appName, 1000);
        if (cp instanceof SerialPort) {
            m_port = (SerialPort) cp;
        } else {
            throw new IllegalStateException("unknown port type");
        }
        m_port.setSerialPortParams(m_speed, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        m_port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
        logger.debug("setting port speed to {}", m_speed);
        m_port.disableReceiveFraming();
        m_port.enableReceiveThreshold(1);
        // m_port.disableReceiveTimeout();
        m_port.enableReceiveTimeout(1000);
        m_in = m_port.getInputStream();
        m_out = m_port.getOutputStream();
        logger.info("successfully opened port {}", m_devName);
        return true;
    } catch (IOException e) {
        logger.error("cannot open port: {}, got IOException ", m_devName, e);
    } catch (PortInUseException e) {
        logger.error("cannot open port: {}, it is in use!", m_devName);
    } catch (UnsupportedCommOperationException e) {
        logger.error("got unsupported operation {} on port {}", e.getMessage(), m_devName);
    } catch (NoSuchPortException e) {
        logger.error("got no such port for {}", m_devName);
    } catch (IllegalStateException e) {
        logger.error("got unknown port type for {}", m_devName);
    }
    return false;
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) PortInUseException(gnu.io.PortInUseException) NoSuchPortException(gnu.io.NoSuchPortException) SerialPort(gnu.io.SerialPort) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) IOException(java.io.IOException)

Example 17 with UnsupportedCommOperationException

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

the class SerialConnection method setConnectionParameters.

/**
     * Sets the connection parameters to the setting in the parameters object.
     * If set fails return the parameters object to origional settings and
     * throw exception.
     *
     * @throws Exception if the configured parameters cannot be set properly
     *             on the port.
     */
protected void setConnectionParameters() throws Exception {
    // Save state of parameters before trying a set.
    int oldBaudRate = m_SerialPort.getBaudRate();
    int oldDatabits = m_SerialPort.getDataBits();
    int oldStopbits = m_SerialPort.getStopBits();
    int oldParity = m_SerialPort.getParity();
    int oldFlowControl = m_SerialPort.getFlowControlMode();
    // to original state.
    try {
        m_SerialPort.setSerialPortParams(m_Parameters.getBaudRate(), m_Parameters.getDatabits(), m_Parameters.getStopbits(), m_Parameters.getParity());
    } catch (UnsupportedCommOperationException e) {
        m_Parameters.setBaudRate(oldBaudRate);
        m_Parameters.setDatabits(oldDatabits);
        m_Parameters.setStopbits(oldStopbits);
        m_Parameters.setParity(oldParity);
        final String errMsg = "Unsupported parameter";
        logger.debug("{} failed to set up one of [baudRate, dataBits, stopBits, parity]: {}", errMsg, e.getMessage());
        throw new Exception(errMsg);
    }
    // Set flow control.
    try {
        m_SerialPort.setFlowControlMode(m_Parameters.getFlowControlIn() | m_Parameters.getFlowControlOut());
    } catch (UnsupportedCommOperationException e) {
        final String errMsg = "Unsupported flow control";
        logger.debug("{}: {}", errMsg, e.getMessage());
        throw new Exception(errMsg);
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) IOException(java.io.IOException) NoSuchPortException(gnu.io.NoSuchPortException)

Example 18 with UnsupportedCommOperationException

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

the class SerialDevice method initialize.

/**
     * Initialize this device and open the serial port
     * 
     * @throws InitializationException
     *             if port can not be opened
     */
public void initialize() throws InitializationException {
    // parse ports and if the default port is found, initialized the reader
    Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
        if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (id.getName().equals(port)) {
                logger.debug("Serial port '{}' has been found.", port);
                portId = id;
            }
        }
    }
    if (portId != null) {
        // initialize serial port
        try {
            serialPort = portId.open("openHAB-Smarthomatic", DEFAULT_PORT);
        } catch (PortInUseException e) {
            throw new InitializationException(e);
        }
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {
            throw new InitializationException(e);
        }
        try {
            serialPort.addEventListener(this);
        } catch (TooManyListenersException e) {
            throw new InitializationException(e);
        }
        // activate the DATA_AVAILABLE notifier
        serialPort.notifyOnDataAvailable(true);
        try {
            // set port parameters
            serialPort.setSerialPortParams(baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
            throw new InitializationException(e);
        }
        try {
            // get the output stream
            outputStream = serialPort.getOutputStream();
        } catch (IOException e) {
            throw new InitializationException(e);
        }
    } 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 InitializationException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
    }
}
Also used : TooManyListenersException(java.util.TooManyListenersException) UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) IOException(java.io.IOException)

Example 19 with UnsupportedCommOperationException

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

the class SerialConnector method open.

/**
     * {@inheritDoc}
     **/
public void open() {
    try {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.enableReceiveThreshold(1);
        serialPort.disableReceiveTimeout();
        serialOutput = new OutputStreamWriter(serialPort.getOutputStream(), "US-ASCII");
        serialInput = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
        setSerialEventHandler(this);
        connected = true;
    } catch (NoSuchPortException noSuchPortException) {
        logger.error("open(): No Such Port Exception: ", noSuchPortException);
        connected = false;
    } catch (PortInUseException portInUseException) {
        logger.error("open(): Port in Use Exception: ", portInUseException);
        connected = false;
    } catch (UnsupportedCommOperationException unsupportedCommOperationException) {
        logger.error("open(): Unsupported Comm Operation Exception: ", unsupportedCommOperationException);
        connected = false;
    } catch (UnsupportedEncodingException unsupportedEncodingException) {
        logger.error("open(): Unsupported Encoding Exception: ", unsupportedEncodingException);
        connected = false;
    } catch (IOException ioException) {
        logger.error("open(): IO Exception: ", ioException);
        connected = false;
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) InputStreamReader(java.io.InputStreamReader) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) BufferedReader(java.io.BufferedReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 20 with UnsupportedCommOperationException

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

the class DavisBinding method openPort.

public void openPort() throws InitializationException {
    CommPortIdentifier portIdentifier;
    try {
        portIdentifier = CommPortIdentifier.getPortIdentifier(port);
        try {
            serialPort = portIdentifier.open("openhab", 3000);
            serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            // serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
            serialPort.enableReceiveTimeout(100);
            serialPort.enableReceiveThreshold(1);
            inputStream = new DataInputStream(new BufferedInputStream(serialPort.getInputStream()));
            outputStream = serialPort.getOutputStream();
            logger.debug("port opened: " + port);
        } catch (PortInUseException e) {
            throw new InitializationException(e);
        } catch (UnsupportedCommOperationException e) {
            throw new InitializationException(e);
        } catch (IOException e) {
            throw new InitializationException(e);
        }
    } catch (NoSuchPortException e) {
        StringBuilder sb = new StringBuilder();
        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) PortInUseException(gnu.io.PortInUseException) NoSuchPortException(gnu.io.NoSuchPortException) Enumeration(java.util.Enumeration) CommPortIdentifier(gnu.io.CommPortIdentifier) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Aggregations

UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)22 CommPortIdentifier (gnu.io.CommPortIdentifier)20 PortInUseException (gnu.io.PortInUseException)20 IOException (java.io.IOException)17 NoSuchPortException (gnu.io.NoSuchPortException)14 TooManyListenersException (java.util.TooManyListenersException)12 CommPort (gnu.io.CommPort)8 Enumeration (java.util.Enumeration)6 SerialPort (gnu.io.SerialPort)5 BufferedReader (java.io.BufferedReader)4 DataInputStream (java.io.DataInputStream)4 InputStreamReader (java.io.InputStreamReader)4 OutputStreamWriter (java.io.OutputStreamWriter)4 SerialPortEvent (gnu.io.SerialPortEvent)3 BufferedInputStream (java.io.BufferedInputStream)3 BufferedWriter (java.io.BufferedWriter)3 SerialPortEventListener (gnu.io.SerialPortEventListener)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 DataOutputStream (java.io.DataOutputStream)1 InputStream (java.io.InputStream)1