private static String _guessWindowsPortName(ArrayList<String> portNames) { /* // if the for loop below this comment block does not work right, // try using the code in this comment block instead String portName = null; for(int n=1; n<10; n++) { portName = "COM" + n; SerialPort port = new SerialPort(portName); try { port.open(); port.close(); break; } catch(Exception exn) {} } return portName;*/ for (String portName : portNames) { SerialPort port = new SerialPort(portName); try { port.open(); port.close(); return portName; } catch (Exception exn) { } } return null; }
public void schliesseSerialPort() { if (serialPortGeoeffnet == true) { System.out.println("Schließe Serialport"); serialPort.close(); serialPortGeoeffnet = false; } else { System.out.println("Serialport bereits geschlossen"); } }
public void connectionclose() { try { is.close(); os.close(); dis.close(); dos.close(); sPort.close(); } catch (Exception e) { System.out.println("close" + e); } }
/** Close the serial port. */ public void delete() { // Close the serial port try { if (inputStream != null) inputStream.close(); outputStream.close(); } catch (IOException e) { } mbedSerialPort.removeEventListener(); mbedSerialPort.close(); mbedPort.close(); }
public void connect() { _serialPort.setBaud(115200); _serialPort.setParity(gnu.io.SerialPort.PARITY_NONE); _serialPort.setDataBits(gnu.io.SerialPort.DATABITS_8); _serialPort.setStopBits(gnu.io.SerialPort.STOPBITS_1); _serialPort.setFlowControl(gnu.io.SerialPort.FLOWCONTROL_NONE); _serialPort.open(); try { for (int n = 0; n < 20; n++) { _serialPort.writeLn("99"); // writeLn flushes automatically Thread.sleep(250); if (_serialPort.available() > 0) { String response = _serialPort.readLine(); if (response.equals("11")) { Thread.sleep(250); while (_serialPort.available() > 0) _serialPort.readChar(); _isConnected = true; return; } _serialPort.close(); throw new RuntimeException( "Arduino.connect: Arduino board present but running incorrect firmware on port " + _serialPort.getName()); } Thread.sleep(250); } } catch (InterruptedException e) { throw new RuntimeException( "Arduino.connect: interrupted while waiting for response from Arduino on port" + _serialPort.getName()); } _serialPort.close(); throw new RuntimeException( "Arduino.connect: Arduino board present but running incorrect firmware on port " + _serialPort.getName()); }
private void init( SerialPort serialPort, SerialParameters serialParams, SerialTestResultsDisplay results) throws SerialConnectionException { _parameters = serialParams; _sPort = serialPort; _results = results; _sendThread = new Thread(this); _chunkSize = 4096; _savedStream = new ByteArrayOutputStream(); try { _os = _sPort.getOutputStream(); } catch (IOException e) { _sPort.close(); throw new SerialConnectionException("Error opening i/o streams"); } }
// disconnect the serial port // pre: an open serial port // post: clsoed serial port public void disconnect() { // close the serial port try { // writeData(0, 0); serialPort.removeEventListener(); serialPort.close(); input.close(); output.close(); setConnected(false); logText = "Disconnected."; System.out.println(logText + "\n"); } catch (Exception e) { logText = "Failed to close " + serialPort.getName() + "(" + e.toString() + ")"; System.out.println(logText + "\n"); } }
public void openStreams() throws IOException { try { serialPort = new TOSSerial(portName); } catch (Exception e) { throw new IOException("Could not open " + portName + ": " + e.getMessage()); } try { // serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); serialPort.setSerialPortParams(baudRate, 8, SerialPort.STOPBITS_1, false); serialPort.addListener(this); serialPort.notifyOn(SerialPortEvent.DATA_AVAILABLE, true); } catch (Exception e) { serialPort.close(); throw new IOException("Could not configure " + portName + ": " + e.getMessage()); } is = serialPort.getInputStream(); os = serialPort.getOutputStream(); }
/* * Code cribbed from RXTX example */ public static void closePort(SerialPort serialPort) { if (serialPort != null) { serialPort.notifyOnDataAvailable(false); serialPort.removeEventListener(); if (inputStream != null) { try { inputStream.close(); inputStream = null; } catch (IOException e) { } } if (outputStream != null) { try { outputStream.close(); outputStream = null; } catch (IOException e) { } } serialPort.close(); serialPort = null; } }
public void ClosePort() { if (portOpened) { if (serialPort != null) { try { // Close the I/O streams. out.close(); in.close(); // Close the port. serialPort.removeEventListener(); serialPort.close(); } catch (IOException e) { // Don't care } } ClearLog(); portOpened = false; portConfirmed = false; previewPane.setConnected(false); UpdateMenuBar(); } }
/** * This should be called when you stop using the port. This will prevent port locking on platforms * like Linux. */ public synchronized void closeSerial() { if (serialPort != null) { serialPort.removeEventListener(); serialPort.close(); } }
public void closeStreams() throws IOException { serialPort.close(); }
protected void finalize() { System.out.println("SerialByteSource finalize"); serialPort.close(); }
public void disconnect() { _serialPort.close(); _isConnected = false; }
/** * Opens the communication port. * * @throws Exception if an error occurs. */ public void open() throws Exception { // If this is Linux then first of all we need to check that // device file exists. Otherwise call to m_PortIdentifyer.open() // method will crash JVM. // It is ugly patch but it is too late... if (SystemUtils.IS_OS_LINUX) { File portDevFile = new File(m_Parameters.getPortName()); if (!portDevFile.exists()) { throw new Exception( "Modbus serial device " + m_Parameters.getPortName() + " doesn't exist!"); } } // 1. obtain a CommPortIdentifier instance try { m_PortIdentifyer = CommPortIdentifier.getPortIdentifier(m_Parameters.getPortName()); } catch (NoSuchPortException e) { final String errMsg = "Could not get port identifier, maybe insufficient permissions. " + e.getMessage(); logger.debug(errMsg); throw new Exception(errMsg); } logger.trace("Got Port Identifier"); // 2. open the port, wait for given timeout try { m_SerialPort = (SerialPort) m_PortIdentifyer.open("Modbus Serial Master", 30000); } catch (PortInUseException e) { logger.debug("open port failed: " + e.getMessage()); throw new Exception(e.getMessage()); } logger.trace("Got Serial Port"); // 3. set the parameters try { setConnectionParameters(); } catch (Exception e) { // ensure it is closed m_SerialPort.close(); logger.debug("parameter setup failed: " + e.getMessage()); throw e; } if (Modbus.SERIAL_ENCODING_ASCII.equals(m_Parameters.getEncoding())) { m_Transport = new ModbusASCIITransport(); } else if (Modbus.SERIAL_ENCODING_RTU.equals(m_Parameters.getEncoding())) { m_Transport = new ModbusRTUTransport(); setReceiveTimeout(m_Parameters.getReceiveTimeout()); // just here for the moment. } else if (Modbus.SERIAL_ENCODING_BIN.equals(m_Parameters.getEncoding())) { m_Transport = new ModbusBINTransport(); } m_Transport.setEcho(m_Parameters.isEcho()); // Open the input and output streams for the connection. If they won't // open, close the port before throwing an exception. try { m_SerialIn = m_SerialPort.getInputStream(); m_Transport.setCommPort(m_SerialPort); // m_Transport.prepareStreams(m_SerialIn, // m_SerialPort.getOutputStream()); } catch (IOException e) { m_SerialPort.close(); logger.debug(e.getMessage()); throw new Exception("Error opening i/o streams"); } logger.trace("i/o Streams prepared"); // Add this object as an event listener for the serial port. try { m_SerialPort.addEventListener(this); } catch (TooManyListenersException e) { m_SerialPort.close(); final String errMsg = "too many listeners added"; logger.debug("{}: {}", errMsg, e.getMessage()); throw new Exception(errMsg); } // Set notifyOnBreakInterrup to allow event driven break handling. m_SerialPort.notifyOnBreakInterrupt(true); m_Open = true; } // open
public void close() { serialPort.close(); // this seems to bail on my machine (d3) }