Search in sources :

Example 51 with CommPortIdentifier

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

the class DSMRPort method portExists.

/**
     * Checks if the given port name is autodetected by gnu.io or already listed
     * in the system property gnu.io.rxtx.SerialPorts
     *
     * @param portName String containing the port name to lookup
     * @return true if port exists, false otherwise
     */
private boolean portExists(String portName) {
    @SuppressWarnings("unchecked") Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
    boolean portExists = false;
    logger.debug("Searching autodetected ports for: {}", portName);
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier portIdentifier = portEnum.nextElement();
        if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            logger.debug("Found serial port: {}", portIdentifier.getName());
            if (portIdentifier.getName().equals(portName)) {
                portExists = true;
            }
        }
    }
    if (!portExists) {
        Properties properties = System.getProperties();
        String currentPorts = properties.getProperty("gnu.io.rxtx.SerialPorts", "");
        if (currentPorts.indexOf(portName) >= 0) {
            logger.debug("{} is listed in system property gnu.io.rxtx.SerialPorts", portName);
            portExists = true;
        } else {
            logger.debug("{} is not listed in system property gnu.io.rxtx.SerialPorts", portName);
        }
    }
    return portExists;
}
Also used : CommPortIdentifier(gnu.io.CommPortIdentifier) Properties(java.util.Properties)

Example 52 with CommPortIdentifier

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

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

the class SerialIOStream method updateSerialProperties.

private void updateSerialProperties(String devName) {
    /*
         * By default, RXTX searches only devices /dev/ttyS* and
         * /dev/ttyUSB*, and will therefore not find devices that
         * have been symlinked. Adding them however is tricky, see below.
         */
    //
    // first go through the port identifiers to find any that are not in
    // "gnu.io.rxtx.SerialPorts"
    //
    ArrayList<String> allPorts = new ArrayList<String>();
    @SuppressWarnings("rawtypes") Enumeration portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
        if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            allPorts.add(id.getName());
        }
    }
    logger.trace("ports found from identifiers: {}", StringUtils.join(allPorts, ":"));
    //
    if (!allPorts.contains(devName)) {
        allPorts.add(devName);
    }
    //
    // add any that are already in "gnu.io.rxtx.SerialPorts"
    // so we don't accidentally overwrite some of those ports
    String ports = System.getProperty("gnu.io.rxtx.SerialPorts");
    if (ports != null) {
        ArrayList<String> propPorts = new ArrayList<String>(Arrays.asList(ports.split(":")));
        for (String p : propPorts) {
            if (!allPorts.contains(p)) {
                allPorts.add(p);
            }
        }
    }
    String finalPorts = StringUtils.join(allPorts, ":");
    logger.trace("final port list: {}", finalPorts);
    //
    // Finally overwrite the "gnu.io.rxtx.SerialPorts" System property.
    //
    // Note: calling setProperty() is not threadsafe. All bindings run in
    // the same address space, System.setProperty() is globally visible
    // to all bindings.
    // This means if multiple bindings use the serial port there is a
    // race condition where two bindings could be changing the properties
    // at the same time
    //
    System.setProperty("gnu.io.rxtx.SerialPorts", finalPorts);
}
Also used : Enumeration(java.util.Enumeration) CommPortIdentifier(gnu.io.CommPortIdentifier) ArrayList(java.util.ArrayList)

Example 54 with CommPortIdentifier

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

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

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