Search in sources :

Example 11 with CommPort

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

the class EiscpSerial method connect.

/**
     * {@inheritDoc}
     */
public boolean connect(String serialPortName) {
    try {
        logger.debug("Open connection to serial port '{}'", serialPortName);
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.enableReceiveThreshold(1);
        serialPort.disableReceiveTimeout();
        in = serialPort.getInputStream();
        out = serialPort.getOutputStream();
        out.flush();
        if (in.markSupported()) {
            in.reset();
        }
        // RXTX serial port library causes high CPU load
        // Start event listener, which will just sleep and slow down event
        // loop
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
        return true;
    } catch (Exception e) {
        logger.error("serial port connect error", e);
        e.printStackTrace();
        return false;
    }
}
Also used : CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) IOException(java.io.IOException)

Example 12 with CommPort

use of gnu.io.CommPort in project netty by netty.

the class RxtxChannel method doConnect.

@Override
protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
    RxtxDeviceAddress remote = (RxtxDeviceAddress) remoteAddress;
    final CommPortIdentifier cpi = CommPortIdentifier.getPortIdentifier(remote.value());
    final CommPort commPort = cpi.open(getClass().getName(), 1000);
    commPort.enableReceiveTimeout(config().getOption(READ_TIMEOUT));
    deviceAddress = remote;
    serialPort = (SerialPort) commPort;
}
Also used : CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort)

Example 13 with CommPort

use of gnu.io.CommPort 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 14 with CommPort

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

the class DSMRPort method open.

/**
     * Opens the Operation System Serial Port
     * <p>
     * This method opens the port and set Serial Port parameters according to
     * the DSMR specification. Since the specification is clear about these
     * parameters there are not configurable.
     * <p>
     * If there are problem while opening the port, it is the responsibility of
     * the calling method to handle this situation (and for example close the
     * port again).
     * <p>
     * Opening an already open port is harmless. The method will return
     * immediately
     *
     * @return true if opening was successful (or port was already open), false
     *         otherwise
     */
private boolean open() {
    synchronized (portLock) {
        // Sanity check
        if (portState != PortState.CLOSED) {
            return true;
        }
        try {
            // Add non standard port names if not exists (fixes part of #4175)
            if (!portExists(portName)) {
                logger.warn("Port {} does not exists according to the system, we will still try to open it", portName);
            }
            // Opening Operating System Serial Port
            logger.debug("Creating CommPortIdentifier");
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
            logger.debug("Opening CommPortIdentifier");
            CommPort commPort = portIdentifier.open("org.openhab.binding.dsmr", readTimeoutMSec);
            logger.debug("Configure serial port");
            serialPort = (SerialPort) commPort;
            serialPort.enableReceiveThreshold(1);
            serialPort.enableReceiveTimeout(readTimeoutMSec);
            // Configure Serial Port based on specified port speed
            logger.debug("Configure serial port parameters: {}", portSettings);
            if (portSettings != null) {
                serialPort.setSerialPortParams(portSettings.getBaudrate(), portSettings.getDataBits(), portSettings.getStopbits(), portSettings.getParity());
                /* special settings for low speed port (checking reference here) */
                if (portSettings == DSMRPortSettings.LOW_SPEED_SETTINGS) {
                    serialPort.setDTR(false);
                    serialPort.setRTS(true);
                }
            } else {
                logger.error("Invalid port parameters, closing port:{}", portSettings);
                return false;
            }
        } catch (NoSuchPortException nspe) {
            logger.error("Could not open port: {}", portName, nspe);
            return false;
        } catch (PortInUseException piue) {
            logger.error("Port already in use: {}", portName, piue);
            return false;
        } catch (UnsupportedCommOperationException ucoe) {
            logger.error("Port does not support requested port settings " + "(invalid dsmr:portsettings parameter?): {}", portName, ucoe);
            return false;
        }
        // SerialPort is ready, open the reader
        logger.info("SerialPort opened successful");
        try {
            bis = new BufferedInputStream(serialPort.getInputStream());
        } catch (IOException ioe) {
            logger.error("Failed to get inputstream for serialPort. Closing port", ioe);
            return false;
        }
        logger.info("DSMR Port opened successful");
        return true;
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException)

Example 15 with CommPort

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

the class EpsonProjectorSerialConnector method connect.

/**
     * {@inheritDoc}
     */
@Override
public void connect() throws EpsonProjectorException {
    try {
        logger.debug("Open connection to serial port '{}'", serialPortName);
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.enableReceiveThreshold(1);
        serialPort.disableReceiveTimeout();
        in = serialPort.getInputStream();
        out = serialPort.getOutputStream();
        out.flush();
        if (in.markSupported()) {
            in.reset();
        }
        // RXTX serial port library causes high CPU load
        // Start event listener, which will just sleep and slow down event loop
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
        throw new EpsonProjectorException(e);
    }
}
Also used : CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) EpsonProjectorException(org.openhab.binding.epsonprojector.internal.EpsonProjectorException) IOException(java.io.IOException) EpsonProjectorException(org.openhab.binding.epsonprojector.internal.EpsonProjectorException)

Aggregations

CommPort (gnu.io.CommPort)15 CommPortIdentifier (gnu.io.CommPortIdentifier)15 IOException (java.io.IOException)10 NoSuchPortException (gnu.io.NoSuchPortException)8 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)8 PortInUseException (gnu.io.PortInUseException)7 SerialPort (gnu.io.SerialPort)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 TooManyListenersException (java.util.TooManyListenersException)2 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 UnknownHostException (java.net.UnknownHostException)1