Search in sources :

Example 11 with NoSuchPortException

use of gnu.io.NoSuchPortException in project jop by jop-devel.

the class JavaDown method openSerialPort.

/**
     * Opens and configures a serial port 
     * 
     * @param commPortId
     * @return
     * @throws NoSuchPortException if the identifier does not specify a serial port
     * @throws PortInUseException if the port is in use
     * @throws UnsupportedCommOperationException if the settings are not supported
     */
private SerialPort openSerialPort(CommPortIdentifier commPortId) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
    if (commPortId.getPortType() != CommPortIdentifier.PORT_SERIAL) {
        throw new gnu.io.NoSuchPortException();
    }
    SerialPort serialPort = (SerialPort) commPortId.open(IJOPLaunchConfigurationConstants.COMM_PORT_OWNER_ID, SERIAL_PORT_TIMEOUT);
    serialPort.setSerialPortParams(SERIAL_PORT_BAUDRATE, SERIAL_PORT_DATABITS, SERIAL_PORT_STOPBITS, SERIAL_PORT_PARITY);
    return serialPort;
}
Also used : NoSuchPortException(gnu.io.NoSuchPortException) SerialPort(gnu.io.SerialPort)

Example 12 with NoSuchPortException

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

the class PowerMaxSerialConnector method open.

/**
     * {@inheritDoc}
     **/
@Override
public void open() {
    logger.debug("open(): Opening Serial Connection");
    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();
        setInput(serialPort.getInputStream());
        setOutput(serialPort.getOutputStream());
        getOutput().flush();
        if (getInput().markSupported()) {
            getInput().reset();
        }
        setReaderThread(new SerialReaderThread(getInput(), this));
        getReaderThread().start();
        setConnected(true);
    } catch (NoSuchPortException noSuchPortException) {
        logger.debug("open(): No Such Port Exception: {}", noSuchPortException.getMessage());
        setConnected(false);
    } catch (PortInUseException portInUseException) {
        logger.debug("open(): Port in Use Exception: {}", portInUseException.getMessage());
        setConnected(false);
    } catch (UnsupportedCommOperationException unsupportedCommOperationException) {
        logger.debug("open(): Unsupported Comm Operation Exception: {}", unsupportedCommOperationException.getMessage());
        setConnected(false);
    } catch (UnsupportedEncodingException unsupportedEncodingException) {
        logger.debug("open(): Unsupported Encoding Exception: {}", unsupportedEncodingException.getMessage());
        setConnected(false);
    } catch (IOException ioException) {
        logger.debug("open(): IO Exception: ", ioException.getMessage());
        setConnected(false);
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 13 with NoSuchPortException

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

Example 14 with NoSuchPortException

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

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

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