Search in sources :

Example 1 with SerialPort

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

the class IntRSModule method connect.

@Override
protected CommunicationChannel connect() {
    logger.info("Connecting to INT-RS module at {}", this.port);
    try {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(this.port);
        SerialPort serialPort = portIdentifier.open("org.openhab.binding.satel", 2000);
        serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.enableReceiveTimeout(this.getTimeout());
        // RXTX serial port library causes high CPU load
        // Start event listener, which will just sleep and slow down event
        // loop
        serialPort.addEventListener(new SerialPortEventListener() {

            @Override
            public void serialEvent(SerialPortEvent ev) {
                try {
                    logger.trace("RXTX library CPU load workaround, sleep forever");
                    Thread.sleep(Long.MAX_VALUE);
                } catch (InterruptedException e) {
                }
            }
        });
        serialPort.notifyOnDataAvailable(true);
        logger.info("INT-RS module connected successfuly");
        return new SerialCommunicationChannel(serialPort);
    } catch (NoSuchPortException e) {
        logger.error("Port {} does not exist", this.port);
    } catch (PortInUseException e) {
        logger.error("Port {} in use.", this.port);
    } catch (UnsupportedCommOperationException e) {
        logger.error("Unsupported comm operation on port {}.", this.port);
    } catch (TooManyListenersException e) {
        logger.error("Too many listeners on port {}.", this.port);
    }
    return null;
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) TooManyListenersException(java.util.TooManyListenersException) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) SerialPort(gnu.io.SerialPort) CommPortIdentifier(gnu.io.CommPortIdentifier) SerialPortEventListener(gnu.io.SerialPortEventListener) SerialPortEvent(gnu.io.SerialPortEvent)

Example 2 with SerialPort

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

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

the class AlarmDecoderBinding method connect.

private synchronized void connect() {
    try {
        // make sure we have disconnected
        disconnect();
        markAllItemsUnupdated();
        if (m_tcpHostName != null && m_tcpPort > 0) {
            m_socket = new Socket(m_tcpHostName, m_tcpPort);
            m_reader = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
            m_writer = new BufferedWriter(new OutputStreamWriter(m_socket.getOutputStream()));
            logger.info("connected to {}:{}", m_tcpHostName, m_tcpPort);
            startMsgReader();
        } else if (this.m_serialDeviceName != null) {
            /*
                 * by default, RXTX searches only devices /dev/ttyS* and
                 * /dev/ttyUSB*, and will so not find symlinks. The
                 * setProperty() call below helps
                 */
            updateSerialProperties(m_serialDeviceName);
            CommPortIdentifier ci = CommPortIdentifier.getPortIdentifier(m_serialDeviceName);
            CommPort cp = ci.open("openhabalarmdecoder", 10000);
            if (cp == null) {
                throw new IllegalStateException("cannot open serial port!");
            }
            if (cp instanceof SerialPort) {
                m_port = (SerialPort) cp;
            } else {
                throw new IllegalStateException("unknown port type");
            }
            m_port.setSerialPortParams(m_portSpeed, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            m_port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
            m_port.disableReceiveFraming();
            m_port.disableReceiveThreshold();
            m_reader = new BufferedReader(new InputStreamReader(m_port.getInputStream()));
            m_writer = new BufferedWriter(new OutputStreamWriter(m_port.getOutputStream()));
            logger.info("connected to serial port: {}", m_serialDeviceName);
            startMsgReader();
        } else {
            logger.warn("alarmdecoder hostname or port not configured!");
        }
    } catch (PortInUseException e) {
        logger.error("cannot open serial port: {}, it is in use!", m_serialDeviceName);
    } catch (UnsupportedCommOperationException e) {
        logger.error("got unsupported operation {} on port {}", e.getMessage(), m_serialDeviceName);
    } catch (NoSuchPortException e) {
        logger.error("got no such port for {}", m_serialDeviceName);
    } catch (IllegalStateException e) {
        logger.error("got unknown port type for {}", m_serialDeviceName);
    } catch (UnknownHostException e) {
        logger.error("unknown host name :{}: ", m_tcpHostName, e);
    } catch (IOException e) {
        logger.error("cannot open connection to {}", m_connectString);
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) PortInUseException(gnu.io.PortInUseException) NoSuchPortException(gnu.io.NoSuchPortException) SerialPort(gnu.io.SerialPort) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket)

Example 4 with SerialPort

use of gnu.io.SerialPort 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 5 with SerialPort

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

Aggregations

SerialPort (gnu.io.SerialPort)10 CommPortIdentifier (gnu.io.CommPortIdentifier)8 NoSuchPortException (gnu.io.NoSuchPortException)7 PortInUseException (gnu.io.PortInUseException)6 IOException (java.io.IOException)6 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)5 CommPort (gnu.io.CommPort)3 BufferedReader (java.io.BufferedReader)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 BufferedWriter (java.io.BufferedWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2 TooManyListenersException (java.util.TooManyListenersException)2 RXTXPort (gnu.io.RXTXPort)1 SerialPortEvent (gnu.io.SerialPortEvent)1 SerialPortEventListener (gnu.io.SerialPortEventListener)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1