Search in sources :

Example 36 with CommPortIdentifier

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

Example 37 with CommPortIdentifier

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

the class LocoBufferAdapter method getPortNames.

@SuppressWarnings("unchecked")
@Override
public Vector<String> getPortNames() {
    // first, check that the comm package can be opened and ports seen
    portNameVector = new Vector<String>();
    Enumeration<CommPortIdentifier> portIDs = CommPortIdentifier.getPortIdentifiers();
    // find the names of suitable ports
    while (portIDs.hasMoreElements()) {
        CommPortIdentifier id = portIDs.nextElement();
        // filter out line printers 
        if (// accumulate the names in a vector
        id.getPortType() != CommPortIdentifier.PORT_PARALLEL) {
            portNameVector.addElement(id.getName());
        }
    }
    return portNameVector;
}
Also used : CommPortIdentifier(gnu.io.CommPortIdentifier)

Example 38 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(currentBaudNumber(getCurrentBaudRate()), SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_ODD);
        } catch (gnu.io.UnsupportedCommOperationException e) {
            //IN18N
            log.error("Cannot set serial parameters on port " + portName + ": " + e.getMessage());
            //IN18N
            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
        activeSerialPort.setFlowControlMode(0);
        // set timeout
        // activeSerialPort.enableReceiveTimeout(1000);
        log.info("Serial timeout was observed as: " + activeSerialPort.getReceiveTimeout() + " " + //IN18N
        activeSerialPort.isReceiveTimeoutEnabled());
        //IN18N
        log.info("input buffer " + activeSerialPort.getInputBufferSize());
        // 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());
        //IN18N
        }
        opened = true;
    } catch (gnu.io.NoSuchPortException p) {
        return handlePortNotFound(p, portName, log);
    } catch (Exception ex) {
        //IN18N
        log.error("Unexpected exception while opening port " + portName + " trace follows: " + ex);
        ex.printStackTrace();
        //IN18N
        return "Unexpected error while opening port " + portName + ": " + ex;
    }
    // indicates OK return
    return null;
}
Also used : PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) PortInUseException(gnu.io.PortInUseException)

Example 39 with CommPortIdentifier

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

the class NcePacketMonitorPanel method openPort.

public synchronized 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) {
            handlePortBusy(p, portName);
            return "Port " + p + " in use already";
        }
        // try to set it for communication via SerialDriver
        try {
            // Doc says 7 bits, but 8 seems needed
            activeSerialPort.setSerialPortParams(38400, 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
        activeSerialPort.setFlowControlMode(0);
        // set timeout
        log.debug("Serial timeout was observed as: " + activeSerialPort.getReceiveTimeout() + " " + activeSerialPort.isReceiveTimeoutEnabled());
        // get and save stream
        serialStream = new DataInputStream(activeSerialPort.getInputStream());
        ostream = activeSerialPort.getOutputStream();
        // make less verbose
        sendBytes(new byte[] { (byte) 'L', (byte) '-', 10, 13 });
        // purge contents, if any
        int count = serialStream.available();
        log.debug("input stream shows " + count + " bytes available");
        while (count > 0) {
            serialStream.skip(count);
            count = serialStream.available();
        }
        // 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());
        }
    } catch (java.io.IOException ex) {
        log.error("IO error while opening port " + portName, ex);
        return "IO error while opening port " + portName + ": " + ex;
    } catch (gnu.io.UnsupportedCommOperationException ex) {
        log.error("Unsupported communications operation while opening port " + portName, ex);
        return "Unsupported communications operation while opening port " + portName + ": " + ex;
    } catch (gnu.io.NoSuchPortException ex) {
        log.error("No such port: " + portName, ex);
        return "No such port: " + portName + ": " + ex;
    }
    // indicates OK return
    return null;
}
Also used : PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 40 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 communication via SerialDriver
        try {
            // find the baud rate value, configure comm options
            // default, but also defaulted in the initial value of selectedSpeed
            int baud = validSpeedValues[0];
            for (int i = 0; i < validSpeeds.length; i++) {
                if (validSpeeds[i].equals(mBaudRate)) {
                    baud = validSpeedValues[i];
                }
            }
            activeSerialPort.setSerialPortParams(baud, 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
        activeSerialPort.setFlowControlMode(0);
        // 50 mSec timeout before sending chars
        activeSerialPort.enableReceiveTimeout(50);
        // 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("NCE " + portName + " port opened at " + activeSerialPort.getBaudRate() + " baud");
        }
        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 : PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) PortInUseException(gnu.io.PortInUseException)

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