Search in sources :

Example 1 with TooManyListenersException

use of java.util.TooManyListenersException in project openhab1-addons by openhab.

the class SerialConnection method open.

// getModbusTransport
/**
     * Opens the communication port.
     *
     * @throws Exception if an error occurs.
     */
public void open() throws Exception {
    if (isOpen()) {
        return;
    }
    // It is ugly patch but it is too late...
    if (SystemUtils.IS_OS_LINUX) {
        File portDevFile = new File(m_Parameters.getPortName());
        if (!portDevFile.exists()) {
            throw new Exception("Modbus serial device " + m_Parameters.getPortName() + " doesn't exist!");
        }
    }
    // 1. obtain a CommPortIdentifier instance
    try {
        m_PortIdentifyer = CommPortIdentifier.getPortIdentifier(m_Parameters.getPortName());
    } catch (NoSuchPortException e) {
        final String errMsg = "Could not get port identifier, maybe insufficient permissions. " + e.getMessage();
        logger.error(errMsg);
        throw new Exception(errMsg);
    }
    logger.trace("Got Port Identifier");
    // 2. open the port, wait for given timeout
    try {
        m_SerialPort = m_PortIdentifyer.open("Modbus Serial Master", 30000);
    } catch (PortInUseException e) {
        String msg = "open port failed: " + e.getMessage();
        logger.error(msg);
        throw new Exception(msg);
    }
    logger.trace("Got Serial Port");
    // 3. set the parameters
    try {
        setConnectionParameters();
    } catch (Exception e) {
        // ensure it is closed
        m_SerialPort.close();
        logger.error("parameter setup failed: " + e.getMessage());
        throw e;
    }
    setReceiveTimeout(m_Parameters.getReceiveTimeoutMillis());
    if (Modbus.SERIAL_ENCODING_ASCII.equals(m_Parameters.getEncoding())) {
        m_Transport = new ModbusASCIITransport();
    } else if (Modbus.SERIAL_ENCODING_RTU.equals(m_Parameters.getEncoding())) {
        m_Transport = new ModbusRTUTransport();
    } else if (Modbus.SERIAL_ENCODING_BIN.equals(m_Parameters.getEncoding())) {
        m_Transport = new ModbusBINTransport();
    }
    m_Transport.setEcho(m_Parameters.isEcho());
    // open, close the port before throwing an exception.
    try {
        m_SerialIn = m_SerialPort.getInputStream();
        m_Transport.setCommPort(m_SerialPort);
    // m_Transport.prepareStreams(m_SerialIn,
    // m_SerialPort.getOutputStream());
    } catch (IOException e) {
        m_SerialPort.close();
        String msg = "Error opening i/o streams: " + e.getMessage();
        logger.error(msg);
        throw new Exception(msg);
    }
    logger.trace("i/o Streams prepared");
    // Add this object as an event listener for the serial port.
    try {
        m_SerialPort.addEventListener(this);
    } catch (TooManyListenersException e) {
        m_SerialPort.close();
        final String errMsg = "too many listeners added: " + e.getMessage();
        logger.error(errMsg);
        throw new Exception(errMsg);
    }
    // Set notifyOnBreakInterrup to allow event driven break handling.
    m_SerialPort.notifyOnBreakInterrupt(true);
    m_Open = true;
}
Also used : TooManyListenersException(java.util.TooManyListenersException) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) ModbusBINTransport(net.wimpi.modbus.io.ModbusBINTransport) ModbusASCIITransport(net.wimpi.modbus.io.ModbusASCIITransport) ModbusRTUTransport(net.wimpi.modbus.io.ModbusRTUTransport) IOException(java.io.IOException) File(java.io.File) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) IOException(java.io.IOException) NoSuchPortException(gnu.io.NoSuchPortException)

Example 2 with TooManyListenersException

use of java.util.TooManyListenersException 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
     */
@SuppressWarnings("rawtypes")
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", 2000);
        } 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) Enumeration(java.util.Enumeration) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) IOException(java.io.IOException)

Example 3 with TooManyListenersException

use of java.util.TooManyListenersException 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 4 with TooManyListenersException

use of java.util.TooManyListenersException 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 5 with TooManyListenersException

use of java.util.TooManyListenersException 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)

Aggregations

TooManyListenersException (java.util.TooManyListenersException)24 PortInUseException (gnu.io.PortInUseException)15 CommPortIdentifier (gnu.io.CommPortIdentifier)14 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)12 IOException (java.io.IOException)11 NoSuchPortException (gnu.io.NoSuchPortException)6 ImmutableList (com.google.common.collect.ImmutableList)4 AbstractModule (com.google.inject.AbstractModule)4 CreationException (com.google.inject.CreationException)4 SerialPortEvent (gnu.io.SerialPortEvent)4 AccessException (java.rmi.AccessException)4 RemoteException (java.rmi.RemoteException)4 List (java.util.List)4 Enumeration (java.util.Enumeration)3 CommPort (gnu.io.CommPort)2 SerialPort (gnu.io.SerialPort)2 SerialPortEventListener (gnu.io.SerialPortEventListener)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 InputStreamReader (java.io.InputStreamReader)2