Search in sources :

Example 11 with PortInUseException

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

the class SerialWR3223Connector method connect.

/**
     * Connect to WR2332 over serial port.
     *
     * @param port
     * @param baud
     * @throws IOException
     */
public void connect(String port, int baud) throws IOException {
    // parse ports and if the default port is found, initialized the reader
    CommPortIdentifier portId = null;
    Enumeration portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
        if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (id.getName().equals(port)) {
                portId = id;
            }
        }
    }
    if (portId != null) {
        // initialize serial port
        try {
            serialPort = portId.open("wr3223", 2000);
        } catch (PortInUseException e) {
            throw new IOException("Serial port '" + port + "' is already in use.", e);
        }
        try {
            // set port parameters
            serialPort.setSerialPortParams(baud, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
        } catch (UnsupportedCommOperationException e) {
            throw new IOException("Serial port '" + port + "' doesn't support the configuration 7 data bit, 1 stop bit and parity even.", e);
        }
        if (!serialPort.isReceiveTimeoutEnabled()) {
            try {
                if (logger.isDebugEnabled()) {
                    logger.debug("Add a receive timeout of 2000ms.");
                }
                serialPort.enableReceiveTimeout(2000);
            } catch (UnsupportedCommOperationException e) {
                logger.warn("Error by adding receive timeout.", e);
            }
        }
        DataInputStream inputStream = new DataInputStream(serialPort.getInputStream());
        DataOutputStream outputStream = new DataOutputStream(serialPort.getOutputStream());
        connect(inputStream, outputStream);
    } 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 IOException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) Enumeration(java.util.Enumeration) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 12 with PortInUseException

use of gnu.io.PortInUseException 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 13 with PortInUseException

use of gnu.io.PortInUseException in project JMRI by JMRI.

the class SerialDriverAdapter method openPort.

@Override
public String openPort(String portName, String appName) {
    try {
        // get and open the primary port
        CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(portName);
        try {
            // name of program, msec to wait
            activeSerialPort = (SerialPort) portID.open(appName, 2000);
        } catch (PortInUseException p) {
            return handlePortBusy(p, portName, log);
        }
        // try to set it for CMRI serial
        try {
            setSerialPort();
        } catch (gnu.io.UnsupportedCommOperationException e) {
            log.error("Cannot set serial parameters on port " + portName + ": " + e.getMessage());
            return "Cannot set serial parameters on port " + portName + ": " + e.getMessage();
        }
        // set framing (end) character
        try {
            activeSerialPort.enableReceiveFraming(0x03);
            log.debug("Serial framing was observed as: " + activeSerialPort.isReceiveFramingEnabled() + " " + activeSerialPort.getReceiveFramingByte());
        } catch (Exception ef) {
            log.debug("failed to set serial framing: " + ef);
        }
        // set timeout; framing should work before this anyway
        try {
            activeSerialPort.enableReceiveTimeout(10);
            log.debug("Serial timeout was observed as: " + activeSerialPort.getReceiveTimeout() + " " + activeSerialPort.isReceiveTimeoutEnabled());
        } catch (Exception et) {
            log.info("failed to set serial timeout: " + et);
        }
        // get and save stream
        serialStream = activeSerialPort.getInputStream();
        // purge contents, if any
        purgeStream(serialStream);
        // report status?
        if (log.isInfoEnabled()) {
            // report now
            log.info(portName + " port opened at " + activeSerialPort.getBaudRate() + " baud with" + " DTR: " + activeSerialPort.isDTR() + " RTS: " + activeSerialPort.isRTS() + " DSR: " + activeSerialPort.isDSR() + " CTS: " + activeSerialPort.isCTS() + "  CD: " + activeSerialPort.isCD());
        }
        if (log.isDebugEnabled()) {
            // report additional status
            log.debug(" port flow control shows " + (activeSerialPort.getFlowControlMode() == SerialPort.FLOWCONTROL_RTSCTS_OUT ? "hardware flow control" : "no flow control"));
        }
        if (log.isDebugEnabled()) {
            // arrange to notify later
            activeSerialPort.addEventListener(new SerialPortEventListener() {

                @Override
                public void serialEvent(SerialPortEvent e) {
                    int type = e.getEventType();
                    switch(type) {
                        case SerialPortEvent.DATA_AVAILABLE:
                            log.info("SerialEvent: DATA_AVAILABLE is " + e.getNewValue());
                            return;
                        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                            log.info("SerialEvent: OUTPUT_BUFFER_EMPTY is " + e.getNewValue());
                            return;
                        case SerialPortEvent.CTS:
                            log.info("SerialEvent: CTS is " + e.getNewValue());
                            return;
                        case SerialPortEvent.DSR:
                            log.info("SerialEvent: DSR is " + e.getNewValue());
                            return;
                        case SerialPortEvent.RI:
                            log.info("SerialEvent: RI is " + e.getNewValue());
                            return;
                        case SerialPortEvent.CD:
                            log.info("SerialEvent: CD is " + e.getNewValue());
                            return;
                        case SerialPortEvent.OE:
                            log.info("SerialEvent: OE (overrun error) is " + e.getNewValue());
                            return;
                        case SerialPortEvent.PE:
                            log.info("SerialEvent: PE (parity error) is " + e.getNewValue());
                            return;
                        case SerialPortEvent.FE:
                            log.info("SerialEvent: FE (framing error) is " + e.getNewValue());
                            return;
                        case SerialPortEvent.BI:
                            log.info("SerialEvent: BI (break interrupt) is " + e.getNewValue());
                            return;
                        default:
                            log.info("SerialEvent of unknown type: " + type + " value: " + e.getNewValue());
                            return;
                    }
                }
            });
            try {
                activeSerialPort.notifyOnFramingError(true);
            } catch (Exception e) {
                log.debug("Could not notifyOnFramingError: " + e);
            }
            try {
                activeSerialPort.notifyOnBreakInterrupt(true);
            } catch (Exception e) {
                log.debug("Could not notifyOnBreakInterrupt: " + e);
            }
            try {
                activeSerialPort.notifyOnParityError(true);
            } catch (Exception e) {
                log.debug("Could not notifyOnParityError: " + e);
            }
            try {
                activeSerialPort.notifyOnOverrunError(true);
            } catch (Exception e) {
                log.debug("Could not notifyOnOverrunError: " + e);
            }
        }
        opened = true;
    } catch (gnu.io.NoSuchPortException p) {
        return handlePortNotFound(p, portName, log);
    } catch (Exception ex) {
        log.error("Unexpected exception while opening port " + portName + " trace follows: " + ex);
        ex.printStackTrace();
        return "Unexpected error while opening port " + portName + ": " + ex;
    }
    // normal operation
    return null;
}
Also used : PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) SerialPortEventListener(gnu.io.SerialPortEventListener) SerialPortEvent(gnu.io.SerialPortEvent) PortInUseException(gnu.io.PortInUseException)

Example 14 with PortInUseException

use of gnu.io.PortInUseException in project JMRI by JMRI.

the class XBeeAdapter method openPort.

@Override
public String openPort(String portName, String appName) {
    try {
        // get and open the primary port
        CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(portName);
        try {
            // name of program, msec to wait
            activeSerialPort = (SerialPort) portID.open(appName, 2000);
        } catch (PortInUseException p) {
            return handlePortBusy(p, portName, log);
        }
        // try to set it for serial
        try {
            setSerialPort();
        } catch (gnu.io.UnsupportedCommOperationException e) {
            log.error("Cannot set serial parameters on port " + portName + ": " + e.getMessage());
            return "Cannot set serial parameters on port " + portName + ": " + e.getMessage();
        }
        // get and save stream
        serialStream = activeSerialPort.getInputStream();
        // purge contents, if any
        purgeStream(serialStream);
        // report status?
        if (log.isInfoEnabled()) {
            // report now
            log.info(portName + " port opened at " + activeSerialPort.getBaudRate() + " baud with" + " DTR: " + activeSerialPort.isDTR() + " RTS: " + activeSerialPort.isRTS() + " DSR: " + activeSerialPort.isDSR() + " CTS: " + activeSerialPort.isCTS() + "  CD: " + activeSerialPort.isCD());
        }
        if (log.isDebugEnabled()) {
            // report additional status
            log.debug(" port flow control shows " + (activeSerialPort.getFlowControlMode() == SerialPort.FLOWCONTROL_RTSCTS_OUT ? "hardware flow control" : "no flow control"));
        }
        opened = true;
    } catch (gnu.io.NoSuchPortException p) {
        return handlePortNotFound(p, portName, log);
    } catch (Exception ex) {
        log.error("Unexpected exception while opening port " + portName + " trace follows: " + ex);
        ex.printStackTrace();
        return "Unexpected error while opening port " + portName + ": " + ex;
    }
    // normal operation
    return null;
}
Also used : PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) PortInUseException(gnu.io.PortInUseException)

Example 15 with PortInUseException

use of gnu.io.PortInUseException in project JMRI by JMRI.

the class LocoBufferAdapter method openPort.

@Override
public String openPort(String portName, String appName) {
    // open the primary and secondary ports in LocoNet mode, check ability to set moderators
    try {
        // get and open the primary port
        CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(portName);
        try {
            // name of program, msec to wait
            activeSerialPort = (SerialPort) portID.open(appName, 2000);
        } catch (PortInUseException p) {
            return handlePortBusy(p, portName, log);
        }
        // try to set it for LocoNet via LocoBuffer
        try {
            setSerialPort(activeSerialPort);
        } catch (gnu.io.UnsupportedCommOperationException e) {
            log.error(// NOI18N
            "Cannot set serial parameters on port " + portName + ": " + e.getMessage());
            return // NOI18N
            "Cannot set serial parameters on port " + portName + ": " + e.getMessage();
        }
        // set timeout
        try {
            activeSerialPort.enableReceiveTimeout(10);
            log.debug(// NOI18N
            "Serial timeout was observed as: " + activeSerialPort.getReceiveTimeout() + " " + activeSerialPort.isReceiveTimeoutEnabled());
        } catch (Exception et) {
            // NOI18N
            log.info("failed to set serial timeout: " + et);
        }
        // get and save stream
        serialStream = activeSerialPort.getInputStream();
        // purge contents, if any
        purgeStream(serialStream);
        // report status?
        if (log.isInfoEnabled()) {
            // report now
            log.info(// NOI18N
            portName + " port opened at " + activeSerialPort.getBaudRate() + // NOI18N
            " baud with" + " DTR: " + // NOI18N
            activeSerialPort.isDTR() + " RTS: " + // NOI18N
            activeSerialPort.isRTS() + " DSR: " + // NOI18N
            activeSerialPort.isDSR() + " CTS: " + // NOI18N
            activeSerialPort.isCTS() + "  CD: " + // NOI18N
            activeSerialPort.isCD());
        }
        if (log.isDebugEnabled()) {
            // report additional status
            log.debug(// NOI18N
            " port flow control shows " + // NOI18N
            (activeSerialPort.getFlowControlMode() == SerialPort.FLOWCONTROL_RTSCTS_OUT ? "hardware flow control" : "no flow control"));
        }
        if (log.isDebugEnabled()) {
            // arrange to notify later
            activeSerialPort.addEventListener(new SerialPortEventListener() {

                @Override
                public void serialEvent(SerialPortEvent e) {
                    int type = e.getEventType();
                    switch(type) {
                        case SerialPortEvent.DATA_AVAILABLE:
                            // NOI18N
                            log.info("SerialEvent: DATA_AVAILABLE is " + e.getNewValue());
                            return;
                        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                            // NOI18N
                            log.info("SerialEvent: OUTPUT_BUFFER_EMPTY is " + e.getNewValue());
                            return;
                        case SerialPortEvent.CTS:
                            // NOI18N
                            log.info("SerialEvent: CTS is " + e.getNewValue());
                            return;
                        case SerialPortEvent.DSR:
                            // NOI18N
                            log.info("SerialEvent: DSR is " + e.getNewValue());
                            return;
                        case SerialPortEvent.RI:
                            // NOI18N
                            log.info("SerialEvent: RI is " + e.getNewValue());
                            return;
                        case SerialPortEvent.CD:
                            // NOI18N
                            log.info("SerialEvent: CD is " + e.getNewValue());
                            return;
                        case SerialPortEvent.OE:
                            // NOI18N
                            log.info("SerialEvent: OE (overrun error) is " + e.getNewValue());
                            return;
                        case SerialPortEvent.PE:
                            // NOI18N
                            log.info("SerialEvent: PE (parity error) is " + e.getNewValue());
                            return;
                        case SerialPortEvent.FE:
                            // NOI18N
                            log.info("SerialEvent: FE (framing error) is " + e.getNewValue());
                            return;
                        case SerialPortEvent.BI:
                            // NOI18N
                            log.info("SerialEvent: BI (break interrupt) is " + e.getNewValue());
                            return;
                        default:
                            // NOI18N
                            log.info("SerialEvent of unknown type: " + type + " value: " + e.getNewValue());
                            return;
                    }
                }
            });
            try {
                activeSerialPort.notifyOnFramingError(true);
            } catch (Exception e) {
                // NOI18N
                log.debug("Could not notifyOnFramingError: " + e);
            }
            try {
                activeSerialPort.notifyOnBreakInterrupt(true);
            } catch (Exception e) {
                // NOI18N
                log.debug("Could not notifyOnBreakInterrupt: " + e);
            }
            try {
                activeSerialPort.notifyOnParityError(true);
            } catch (Exception e) {
                // NOI18N
                log.debug("Could not notifyOnParityError: " + e);
            }
            try {
                activeSerialPort.notifyOnOverrunError(true);
            } catch (Exception e) {
                // NOI18N
                log.debug("Could not notifyOnOverrunError: " + e);
            }
        }
        opened = true;
    } catch (gnu.io.NoSuchPortException p) {
        return handlePortNotFound(p, portName, log);
    } catch (Exception ex) {
        // NOI18N
        log.error("Unexpected exception while opening port {} trace follows:", portName, ex);
        return "Unexpected error while opening port " + portName + ": " + ex;
    }
    // normal operation
    return null;
}
Also used : PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) SerialPortEventListener(gnu.io.SerialPortEventListener) SerialPortEvent(gnu.io.SerialPortEvent) PortInUseException(gnu.io.PortInUseException)

Aggregations

PortInUseException (gnu.io.PortInUseException)63 CommPortIdentifier (gnu.io.CommPortIdentifier)62 IOException (java.io.IOException)29 SerialPortEvent (gnu.io.SerialPortEvent)25 SerialPortEventListener (gnu.io.SerialPortEventListener)23 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)20 TooManyListenersException (java.util.TooManyListenersException)15 NoSuchPortException (gnu.io.NoSuchPortException)14 DataInputStream (java.io.DataInputStream)8 CommPort (gnu.io.CommPort)7 SerialPort (gnu.io.SerialPort)6 Enumeration (java.util.Enumeration)5 BufferedReader (java.io.BufferedReader)4 InputStreamReader (java.io.InputStreamReader)4 OutputStreamWriter (java.io.OutputStreamWriter)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedWriter (java.io.BufferedWriter)3 DataOutputStream (java.io.DataOutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 RXTXPort (gnu.io.RXTXPort)1