Search in sources :

Example 6 with CommPortIdentifier

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

the class EBusSerialConnector method connect.

/*
     * (non-Javadoc)
     * 
     * @see org.openhab.binding.ebus.connection.AbstractEBusConnector#connect()
     */
@Override
protected boolean connect() throws IOException {
    try {
        final CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port);
        if (portIdentifier != null) {
            serialPort = portIdentifier.open("org.openhab.binding.ebus", 2000);
            serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            // set timeout 10 sec.
            serialPort.disableReceiveThreshold();
            serialPort.enableReceiveTimeout(10000);
            // use event to let readByte wait until data is available, optimize cpu usage
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
            outputStream = serialPort.getOutputStream();
            inputStream = serialPort.getInputStream();
            return super.connect();
        }
    } catch (NoSuchPortException e) {
        logger.error("Unable to connect to serial port {}", port);
    } catch (PortInUseException e) {
        logger.error("Serial port {} is already in use", port);
    } catch (UnsupportedCommOperationException e) {
        logger.error(e.toString(), e);
    } catch (TooManyListenersException e) {
        logger.error("Too many listeners error!", e);
    }
    serialPort = null;
    return false;
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) TooManyListenersException(java.util.TooManyListenersException) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier)

Example 7 with CommPortIdentifier

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

the class SerialConnector method connect.

public void connect() throws EHealthException {
    logger.debug("Going to open Serial connection on port '{}'", portName);
    // 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(portName)) {
                logger.debug("Serial port '{}' has been found.", portName);
                portId = id;
            }
        }
    }
    if (portId != null) {
        // initialize serial port
        try {
            serialPort = portId.open("openHAB", 2000);
            inputStream = serialPort.getInputStream();
            outputStream = serialPort.getOutputStream();
            bReader = new BufferedReader(new InputStreamReader(inputStream));
            bWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
            serialPort.setSerialPortParams(BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (IOException e) {
            throw new EHealthException(e);
        } catch (PortInUseException e) {
            throw new EHealthException(e);
        } catch (UnsupportedCommOperationException e) {
            throw new EHealthException(e);
        } catch (TooManyListenersException e) {
            throw new EHealthException(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 EHealthException("Serial port '" + portName + "' could not be found. Available ports are:\n" + sb.toString());
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) InputStreamReader(java.io.InputStreamReader) CommPortIdentifier(gnu.io.CommPortIdentifier) BufferedReader(java.io.BufferedReader) EHealthException(org.openhab.binding.ehealth.internal.EHealthException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 8 with CommPortIdentifier

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

the class OpenEnergyMonitorSerialConnector method connect.

@Override
public void connect() throws OpenEnergyMonitorException {
    try {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(BAUDRATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        in = serialPort.getInputStream();
        logger.debug("Open Energy Monitor Serial Port message listener started");
    } catch (Exception e) {
        throw new OpenEnergyMonitorException(e);
    }
}
Also used : OpenEnergyMonitorException(org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) OpenEnergyMonitorException(org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException) IOException(java.io.IOException)

Example 9 with CommPortIdentifier

use of gnu.io.CommPortIdentifier 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 10 with CommPortIdentifier

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

Aggregations

CommPortIdentifier (gnu.io.CommPortIdentifier)84 PortInUseException (gnu.io.PortInUseException)62 IOException (java.io.IOException)34 SerialPortEvent (gnu.io.SerialPortEvent)25 SerialPortEventListener (gnu.io.SerialPortEventListener)23 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)20 CommPort (gnu.io.CommPort)15 NoSuchPortException (gnu.io.NoSuchPortException)15 TooManyListenersException (java.util.TooManyListenersException)14 Enumeration (java.util.Enumeration)10 DataInputStream (java.io.DataInputStream)9 SerialPort (gnu.io.SerialPort)8 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 OutputStreamWriter (java.io.OutputStreamWriter)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedWriter (java.io.BufferedWriter)3 DataOutputStream (java.io.DataOutputStream)3 ArrayList (java.util.ArrayList)3 InputStream (java.io.InputStream)2