Search in sources :

Example 6 with SerialPort

use of gnu.io.SerialPort in project jop by jop-devel.

the class JavaDown method run.

public void run(IProgressMonitor monitor) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, FileNotFoundException, IOException {
    final CommPortIdentifier commPortId = CommPortIdentifier.getPortIdentifier(this.commPortId);
    final SerialPort serialPort = openSerialPort(commPortId);
    download(jopFile, serialPort, usb, monitor);
    serialPort.close();
}
Also used : SerialPort(gnu.io.SerialPort) CommPortIdentifier(gnu.io.CommPortIdentifier)

Example 7 with SerialPort

use of gnu.io.SerialPort in project marine-api by ktuukkan.

the class SerialPortExample method init.

/**
	 * Init serial port and reader.
	 */
private void init() {
    try {
        SerialPort sp = getSerialPort();
        if (sp != null) {
            InputStream is = sp.getInputStream();
            SentenceReader sr = new SentenceReader(is);
            sr.addSentenceListener(this);
            sr.start();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : SentenceReader(net.sf.marineapi.nmea.io.SentenceReader) SerialPort(gnu.io.SerialPort) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 8 with SerialPort

use of gnu.io.SerialPort in project marine-api by ktuukkan.

the class SerialPortExample method getSerialPort.

/**
	 * Scan serial ports for NMEA data.
	 * 
	 * @return SerialPort from which NMEA data was found, or null if data was
	 *         not found in any of the ports.
	 */
private SerialPort getSerialPort() {
    try {
        Enumeration<?> e = CommPortIdentifier.getPortIdentifiers();
        while (e.hasMoreElements()) {
            CommPortIdentifier id = (CommPortIdentifier) e.nextElement();
            if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                SerialPort sp = (SerialPort) id.open("SerialExample", 30);
                sp.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                InputStream is = sp.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader buf = new BufferedReader(isr);
                System.out.println("Scanning port " + sp.getName());
                // try each port few times before giving up
                for (int i = 0; i < 5; i++) {
                    try {
                        String data = buf.readLine();
                        if (SentenceValidator.isValid(data)) {
                            System.out.println("NMEA data found!");
                            return sp;
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                is.close();
                isr.close();
                buf.close();
            }
        }
        System.out.println("NMEA data was not found..");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SerialPort(gnu.io.SerialPort) InputStreamReader(java.io.InputStreamReader) CommPortIdentifier(gnu.io.CommPortIdentifier) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 9 with SerialPort

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

the class Connection method open.

/**
     * Opens the serial port associated with this connection.
     * 
     * @throws IOException
     *             if any kind of error occurs opening the serial port.
     */
public void open() throws IOException {
    CommPortIdentifier portIdentifier;
    try {
        portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
    } catch (NoSuchPortException e) {
        throw new IOException("Serial port with given name does not exist", e);
    }
    if (portIdentifier.isCurrentlyOwned()) {
        throw new IOException("Serial port is currently in use.");
    }
    // fixed issue as rxtx library originally used in j62056 does use
    // different version of rxtx
    // com port in their version is using gnu.io.CommPort
    RXTXPort commPort;
    try {
        commPort = portIdentifier.open(this.getClass().getName(), 2000);
    } catch (PortInUseException e) {
        throw new IOException("Serial port is currently in use.", e);
    }
    if (!(commPort instanceof SerialPort)) {
        commPort.close();
        throw new IOException("The specified CommPort is not a serial port");
    }
    serialPort = commPort;
    try {
        os = new DataOutputStream(serialPort.getOutputStream());
        is = new DataInputStream(serialPort.getInputStream());
    } catch (IOException e) {
        serialPort.close();
        serialPort = null;
        throw new IOException("Error getting input or output or input stream from serial port", e);
    }
}
Also used : RXTXPort(gnu.io.RXTXPort) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) SerialPort(gnu.io.SerialPort) CommPortIdentifier(gnu.io.CommPortIdentifier) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 10 with SerialPort

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

the class SerialIOStream method open.

@Override
public boolean open() {
    try {
        updateSerialProperties(m_devName);
        CommPortIdentifier ci = CommPortIdentifier.getPortIdentifier(m_devName);
        CommPort cp = ci.open(m_appName, 1000);
        if (cp instanceof SerialPort) {
            m_port = (SerialPort) cp;
        } else {
            throw new IllegalStateException("unknown port type");
        }
        m_port.setSerialPortParams(m_speed, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        m_port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
        logger.debug("setting port speed to {}", m_speed);
        m_port.disableReceiveFraming();
        m_port.enableReceiveThreshold(1);
        // m_port.disableReceiveTimeout();
        m_port.enableReceiveTimeout(1000);
        m_in = m_port.getInputStream();
        m_out = m_port.getOutputStream();
        logger.info("successfully opened port {}", m_devName);
        return true;
    } catch (IOException e) {
        logger.error("cannot open port: {}, got IOException ", m_devName, e);
    } catch (PortInUseException e) {
        logger.error("cannot open port: {}, it is in use!", m_devName);
    } catch (UnsupportedCommOperationException e) {
        logger.error("got unsupported operation {} on port {}", e.getMessage(), m_devName);
    } catch (NoSuchPortException e) {
        logger.error("got no such port for {}", m_devName);
    } catch (IllegalStateException e) {
        logger.error("got unknown port type for {}", m_devName);
    }
    return false;
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) PortInUseException(gnu.io.PortInUseException) NoSuchPortException(gnu.io.NoSuchPortException) SerialPort(gnu.io.SerialPort) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) IOException(java.io.IOException)

Aggregations

SerialPort (gnu.io.SerialPort)10 CommPortIdentifier (gnu.io.CommPortIdentifier)8 NoSuchPortException (gnu.io.NoSuchPortException)7 PortInUseException (gnu.io.PortInUseException)6 IOException (java.io.IOException)6 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)5 CommPort (gnu.io.CommPort)3 BufferedReader (java.io.BufferedReader)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 BufferedWriter (java.io.BufferedWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2 TooManyListenersException (java.util.TooManyListenersException)2 RXTXPort (gnu.io.RXTXPort)1 SerialPortEvent (gnu.io.SerialPortEvent)1 SerialPortEventListener (gnu.io.SerialPortEventListener)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1