Search in sources :

Example 1 with CommPortIdentifier

use of gnu.io.CommPortIdentifier in project jop by jop-devel.

the class TALWindow method listPortChoices.

private void listPortChoices() {
    CommPortIdentifier portId;
    Enumeration en = CommPortIdentifier.getPortIdentifiers();
    getJComboBox().addItem("null");
    // iterate through the ports.
    while (en.hasMoreElements()) {
        portId = (CommPortIdentifier) en.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            getJComboBox().addItem(portId.getName());
        }
    }
//		portChoice.select(parameters.getPortName());
}
Also used : Enumeration(java.util.Enumeration) CommPortIdentifier(gnu.io.CommPortIdentifier)

Example 2 with CommPortIdentifier

use of gnu.io.CommPortIdentifier in project jop by jop-devel.

the class RXTXCommUtils method getAvailablePorts.

/**
     * @return A HashSet containing the CommPortIdentifier for all ports
     *         of type <i>commPortIdentifier</i> that are not currently
     *         being used.
     */
public static Set<CommPortIdentifier> getAvailablePorts(int commPortIdentifier) {
    Set<CommPortIdentifier> identifiers = new HashSet<CommPortIdentifier>();
    Enumeration<CommPortIdentifier> portIdentifiers = CommPortIdentifier.getPortIdentifiers();
    while (portIdentifiers.hasMoreElements()) {
        CommPortIdentifier com = portIdentifiers.nextElement();
        if (com.getPortType() == commPortIdentifier) {
            try {
                com.open("CommUtil", 50).close();
                identifiers.add(com);
            } catch (PortInUseException e) {
            } catch (Exception e) {
            }
        }
    }
    return identifiers;
}
Also used : PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) PortInUseException(gnu.io.PortInUseException) HashSet(java.util.HashSet)

Example 3 with CommPortIdentifier

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

the class SerialPortConnector method connectPort.

@Override
protected void connectPort() throws Exception {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(device);
    CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
    serialPort = (SerialPort) commPort;
    setSerialPortParameters(baudrate);
    in = serialPort.getInputStream();
    out = new DataOutputStream(serialPort.getOutputStream());
}
Also used : CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) DataOutputStream(java.io.DataOutputStream)

Example 4 with CommPortIdentifier

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

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

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