Search in sources :

Example 16 with SerialPortEvent

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

the class LI100Adapter 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)

Example 17 with SerialPortEvent

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

the class ZTC640Adapter 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
        activeSerialPort.enableReceiveTimeout(10);
        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()) {
            // 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 ex) {
        log.error("IO exception while opening port " + portName + " trace follows: " + ex);
        ex.printStackTrace();
        return "IO Exception while opening port " + portName + ": " + ex;
    } catch (java.util.TooManyListenersException tmlex) {
        log.error("Too Many Listeners exception while opening port " + portName + " trace follows: " + tmlex);
        tmlex.printStackTrace();
        return "Too Many Listeners Exception while opening port " + portName + ": " + tmlex;
    } catch (gnu.io.UnsupportedCommOperationException ucex) {
        log.error("unsupported Comm Operation exception while opening port " + portName + " trace follows: " + ucex);
        ucex.printStackTrace();
        return "Unsupported Comm Exception while opening port " + portName + ": " + ucex;
    }
    // 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)

Example 18 with SerialPortEvent

use of gnu.io.SerialPortEvent 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 Maple 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 19 with SerialPortEvent

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

the class SpecificDriverAdapter 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();
        }
        // set framing (end) character
        try {
            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 20 with SerialPortEvent

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

the class SpecificDriverAdapter 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();
        }
        // set framing (end) character
        try {
            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)

Aggregations

PortInUseException (gnu.io.PortInUseException)26 SerialPortEvent (gnu.io.SerialPortEvent)26 CommPortIdentifier (gnu.io.CommPortIdentifier)25 SerialPortEventListener (gnu.io.SerialPortEventListener)24 IOException (java.io.IOException)12 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)4 TooManyListenersException (java.util.TooManyListenersException)4 NoSuchPortException (gnu.io.NoSuchPortException)2 SerialPort (gnu.io.SerialPort)1 ArrayList (java.util.ArrayList)1