@Override public boolean isOpen() { return serialPort != null && serialPort.isOpened(); }
@Override public void closePort() throws Exception { if (this.serialPort != null) { try { this.serialPort.removeEventListener(); if (this.serialPort.isOpened()) { this.serialPort.closePort(); } } finally { this.serialPort = null; } } }
@Override public int shutDown() { if (serialPort.isOpened()) { try { serialPort.removeEventListener(); serialPort.closePort(); return 1; } catch (SerialPortException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } return -1; }
@Override public boolean openPort() throws Exception { if (StringUtils.isEmpty(portName) || baudRate == 0) { throw new ConnectionException("Couldn't open port " + portName + " using baud rate " + baudRate); } this.responseMessageHandler = new ResponseMessageHandler(); this.serialPort = new SerialPort(portName); this.serialPort.openPort(); this.serialPort.setParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE, true, true); this.serialPort.addEventListener(this); if (this.serialPort == null) { throw new ConnectionException("Serial port not found."); } return serialPort.isOpened(); }
@Override public void write(int arg0) throws IOException { try { byte b = (byte) arg0; if (LOG.isDebugEnabled()) LOG.debug("Writing byte: " + String.format("%02x", b) ); if ((port != null) && (port.isOpened())) { port.writeByte(b); } } catch (jssc.SerialPortException e) { throw new IOException(e); } }
@Override public void stop() throws IOException { try { if (port.isOpened()) { port.purgePort(SerialPort.PURGE_RXCLEAR | SerialPort.PURGE_TXCLEAR); port.closePort(); } } catch (SerialPortException ex) { throw new IOException("Cannot properly stop firmata device", ex); } }
@Override public void stop() throws IOException { try { if (port.isOpened()) { port.purgePort(SerialPort.PURGE_RXCLEAR | SerialPort.PURGE_TXCLEAR); port.closePort(); } } catch (SerialPortException ex) { throw new IOException("Cannot properly stop firmata device", ex); } }
@Override public int read() throws IOException { while (true) { try { if (!serialPort.isOpened()) return -1; return serialPort.readBytes(1, 100)[0]; } catch (final SerialPortTimeoutException e) { // ignore and try again } catch (final SerialPortException e) { if (e.getMessage().contains("Port not opened")) return -1; throw new IOException(e); } } } };
@Override public int read() throws IOException { while (true) { try { if (!serialPort.isOpened()) return -1; return serialPort.readBytes(1, 100)[0]; } catch (final SerialPortTimeoutException e) { // ignore and try again } catch (final SerialPortException e) { if (e.getMessage().contains("Port not opened")) return -1; throw new IOException(e); } } } };
/** * Closes the jSSC connection and stops the serialPortWriter Thread */ public void closeConnection() throws SerialPortException { if (serialPort.isOpened()) serialPort.closePort(); stop(); }
/** * {@inheritDoc} * * @see java.lang.Object#finalize() */ @Override protected void finalize() throws Throwable { if (serialPort.isOpened()) { serialReader.close(); serialPort.removeEventListener(); serialPort.closePort(); } super.finalize(); }
/** * {@inheritDoc} * * @see java.lang.Object#finalize() */ @Override protected void finalize() throws Throwable { if (serialPort.isOpened()) { serialReader.close(); serialPort.removeEventListener(); serialPort.closePort(); } super.finalize(); }
@Override public void start() throws IOException { if (!port.isOpened()) { try { port.openPort(); port.setParams( SerialPort.BAUDRATE_57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); port.setEventsMask(SerialPort.MASK_RXCHAR); port.addEventListener(this); } catch (SerialPortException ex) { throw new IOException("Cannot start firmata device", ex); } } }
@Override public void start() throws IOException { if (!port.isOpened()) { try { port.openPort(); port.setParams( SerialPort.BAUDRATE_57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); port.setEventsMask(SerialPort.MASK_RXCHAR); port.addEventListener(this); } catch (SerialPortException ex) { throw new IOException("Cannot start firmata device", ex); } } }
public void serialRead() { if (!serialPort.isOpened()) return; try {