Search in sources :

Example 56 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
     */
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-Smarthomatic", DEFAULT_PORT);
        } 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) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) IOException(java.io.IOException)

Example 57 with CommPortIdentifier

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

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

Example 59 with CommPortIdentifier

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

the class SerialDriverAdapter method openPort.

@Override
public String openPort(String portName, String appName) {
    // open the port, 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 comunication via SerialDriver
        try {
            activeSerialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } 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 RTS high, DTR high
        // not connected in some serial ports and adapters
        activeSerialPort.setRTS(true);
        // pin 1 in DIN8; on main connector, this is DTR
        activeSerialPort.setDTR(true);
        // disable flow control; hardware lines used for signaling, XON/XOFF might appear in data
        //AJB: Removed Jan 2010 - 
        //Setting flow control mode to zero kills comms - SPROG doesn't send data
        //Concern is that will disabling this affect other SPROGs? Serial ones? 
        //activeSerialPort.setFlowControlMode(0);
        // set timeout
        // activeSerialPort.enableReceiveTimeout(1000);
        log.debug("Serial timeout was observed as: " + activeSerialPort.getReceiveTimeout() + " " + activeSerialPort.isReceiveTimeoutEnabled());
        // get and save stream
        serialStream = activeSerialPort.getInputStream();
        // purge contents, if any
        purgeStream(serialStream);
        // report status?
        if (log.isInfoEnabled()) {
            log.info(portName + " port opened at " + activeSerialPort.getBaudRate() + " baud, sees " + " DTR: " + activeSerialPort.isDTR() + " RTS: " + activeSerialPort.isRTS() + " DSR: " + activeSerialPort.isDSR() + " CTS: " + activeSerialPort.isCTS() + "  CD: " + activeSerialPort.isCD());
        }
        //AJB - add Sprog Traffic Controller as event listener
        try {
            activeSerialPort.addEventListener(this.getSystemConnectionMemo().getTrafficController());
        } catch (TooManyListenersException e) {
        }
        setManufacturer(SpeedoConnectionTypeList.BACHRUS);
        // AJB - activate the DATA_AVAILABLE notifier
        activeSerialPort.notifyOnDataAvailable(true);
        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;
    }
    // indicates OK return
    return null;
}
Also used : TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException)

Example 60 with CommPortIdentifier

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

the class EliteAdapter method openPort.

@Override
public String openPort(String portName, String appName) {
    // open the port in XPressNet 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 XNet
        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 timeout
        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"));
        }
        // arrange to notify later
        activeSerialPort.addEventListener(new SerialPortEventListener() {

            @Override
            public void serialEvent(SerialPortEvent e) {
                int type = e.getEventType();
                switch(type) {
                    case SerialPortEvent.DATA_AVAILABLE:
                        if (log.isDebugEnabled()) {
                            log.debug("SerialEvent: DATA_AVAILABLE is " + e.getNewValue());
                        }
                        return;
                    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                        if (log.isDebugEnabled()) {
                            log.debug("SerialEvent: OUTPUT_BUFFER_EMPTY is " + e.getNewValue());
                        }
                        setOutputBufferEmpty(true);
                        return;
                    case SerialPortEvent.CTS:
                        if (log.isDebugEnabled()) {
                            log.debug("SerialEvent: CTS is " + e.getNewValue());
                        }
                        return;
                    case SerialPortEvent.DSR:
                        if (log.isDebugEnabled()) {
                            log.debug("SerialEvent: DSR is " + e.getNewValue());
                        }
                        return;
                    case SerialPortEvent.RI:
                        if (log.isDebugEnabled()) {
                            log.debug("SerialEvent: RI is " + e.getNewValue());
                        }
                        return;
                    case SerialPortEvent.CD:
                        if (log.isDebugEnabled()) {
                            log.debug("SerialEvent: CD is " + e.getNewValue());
                        }
                        return;
                    case SerialPortEvent.OE:
                        if (log.isDebugEnabled()) {
                            log.debug("SerialEvent: OE (overrun error) is " + e.getNewValue());
                        }
                        return;
                    case SerialPortEvent.PE:
                        if (log.isDebugEnabled()) {
                            log.debug("SerialEvent: PE (parity error) is " + e.getNewValue());
                        }
                        return;
                    case SerialPortEvent.FE:
                        if (log.isDebugEnabled()) {
                            log.debug("SerialEvent: FE (framing error) is " + e.getNewValue());
                        }
                        return;
                    case SerialPortEvent.BI:
                        if (log.isDebugEnabled()) {
                            log.debug("SerialEvent: BI (break interrupt) is " + e.getNewValue());
                        }
                        return;
                    default:
                        if (log.isDebugEnabled()) {
                            log.debug("SerialEvent of unknown type: " + type + " value: " + e.getNewValue());
                        }
                        return;
                }
            }
        });
        try {
            activeSerialPort.notifyOnFramingError(true);
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Could not notifyOnFramingError: " + e);
            }
        }
        try {
            activeSerialPort.notifyOnBreakInterrupt(true);
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Could not notifyOnBreakInterrupt: " + e);
            }
        }
        try {
            activeSerialPort.notifyOnParityError(true);
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Could not notifyOnParityError: " + e);
            }
        }
        try {
            activeSerialPort.notifyOnOutputEmpty(true);
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Could not notifyOnOutputEmpty: " + e);
            }
        }
        try {
            activeSerialPort.notifyOnOverrunError(true);
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Could not notifyOnOverrunError: " + e);
            }
        }
        opened = true;
    } catch (gnu.io.NoSuchPortException p) {
        return handlePortNotFound(p, portName, log);
    } catch (IOException ioe) {
        log.error("IOException exception while opening port " + portName + " trace follows: " + ioe);
        ioe.printStackTrace();
        return "IO exception while opening port " + portName + ": " + ioe;
    } catch (java.util.TooManyListenersException tmle) {
        log.error("TooManyListenersException while opening port " + portName + " trace follows: " + tmle);
        tmle.printStackTrace();
        return "Too Many Listeners exception while opening port " + portName + ": " + tmle;
    }
    // normal operation
    return null;
}
Also used : PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) SerialPortEventListener(gnu.io.SerialPortEventListener) SerialPortEvent(gnu.io.SerialPortEvent) IOException(java.io.IOException) PortInUseException(gnu.io.PortInUseException) 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