Search in sources :

Example 21 with TooManyListenersException

use of java.util.TooManyListenersException in project JMRI by JMRI.

the class SerialDriverAdapter method openPort.

@Override
@SuppressWarnings("CallToPrintStackTrace")
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 (UnsupportedCommOperationException 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());
                    }
                }
            });
            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 (IOException ex) {
        log.error("Unexpected exception while opening port " + portName + " trace follows: " + ex);
        ex.printStackTrace();
        return "Unexpected error while opening port " + portName + ": " + ex;
    } catch (TooManyListenersException 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 : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) SerialPortEventListener(gnu.io.SerialPortEventListener) UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) SerialPortEvent(gnu.io.SerialPortEvent) IOException(java.io.IOException) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) IOException(java.io.IOException)

Example 22 with TooManyListenersException

use of java.util.TooManyListenersException 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(baudRate, 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().getSprogTrafficController());
        } catch (TooManyListenersException e) {
        }
        // 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 23 with TooManyListenersException

use of java.util.TooManyListenersException 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 24 with TooManyListenersException

use of java.util.TooManyListenersException in project JMRI by JMRI.

the class Mx1Adapter method openPort.

@Override
public String openPort(String portName, String appName) {
    // open the port in MX-1 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 Can Net
        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(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()) {
            // 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((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());
                }
            });
            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 (IOException | TooManyListenersException ex) {
        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 : TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) SerialPortEvent(gnu.io.SerialPortEvent) IOException(java.io.IOException) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) IOException(java.io.IOException)

Aggregations

TooManyListenersException (java.util.TooManyListenersException)24 PortInUseException (gnu.io.PortInUseException)15 CommPortIdentifier (gnu.io.CommPortIdentifier)14 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)12 IOException (java.io.IOException)11 NoSuchPortException (gnu.io.NoSuchPortException)6 ImmutableList (com.google.common.collect.ImmutableList)4 AbstractModule (com.google.inject.AbstractModule)4 CreationException (com.google.inject.CreationException)4 SerialPortEvent (gnu.io.SerialPortEvent)4 AccessException (java.rmi.AccessException)4 RemoteException (java.rmi.RemoteException)4 List (java.util.List)4 Enumeration (java.util.Enumeration)3 CommPort (gnu.io.CommPort)2 SerialPort (gnu.io.SerialPort)2 SerialPortEventListener (gnu.io.SerialPortEventListener)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 InputStreamReader (java.io.InputStreamReader)2