@Override public void closePort() throws Exception { if (serialPort != null) { serialPort.removeDataListener(); serialPort.closePort(); } }
@Override public boolean isOpen() { return serialPort.isOpen(); }
@Override public void sendByteImmediately(byte b) throws Exception { serialPort.writeBytes(new byte[]{b}, 1); }
private void initSerialPort(String name, int baud) throws Exception { if (serialPort != null && serialPort.isOpen()) { closePort(); } serialPort = SerialPort.getCommPort(name); serialPort.setParity(SerialPort.NO_PARITY); serialPort.setNumStopBits(SerialPort.ONE_STOP_BIT); serialPort.setNumDataBits(8); serialPort.addDataListener(this); serialPort.setBaudRate(baud); }
@Override public void serialEvent(com.fazecast.jSerialComm.SerialPortEvent event) { if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) { return; } int bytesAvailable = serialPort.bytesAvailable(); if (bytesAvailable <= 0) { return; } int bytesRead = serialPort.readBytes(buffer, Math.min(buffer.length, bytesAvailable)); String response = new String(buffer, 0, bytesRead); responseMessageHandler.handleResponse(response, comm); } }
@Override public byte[] getData() { int bytesAvailable = this.serialPort.bytesAvailable(); byte[] buffer = new byte[bytesAvailable]; this.serialPort.readBytes(buffer, bytesAvailable); return buffer; }
@Override public boolean openPort() throws Exception { responseMessageHandler = new ResponseMessageHandler(); if (serialPort == null) { throw new ConnectionException("The connection wasn't initialized"); } return serialPort.openPort(); }
@Override public List<String> getPortNames() { return Arrays.stream(SerialPort.getCommPorts()) .map(SerialPort::getSystemPortName) .collect(Collectors.toList()); }
/** * Returns the number of bytes available without blocking if {@link #readBytes(byte[], long)} were to be called immediately * after this method returns. * * @return The number of bytes currently available to be read, or -1 if the port is not open. */ public final int bytesAvailable() { return bytesAvailable(portHandle); }
@Override public final void write(int b) throws SerialPortIOException, SerialPortTimeoutException { if (!isOpened) throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); byteBuffer[0] = (byte)(b & 0xFF); int bytesWritten = writeBytes(portHandle, byteBuffer, 1L, 0); if (bytesWritten < 0) throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); else if (bytesWritten == 0) throw new SerialPortTimeoutException("The write operation timed out before all data was written."); }
/** * Clears the state of the RTS line to 0. * @return true if successful, false if not. */ public final boolean clearRTS() { isRtsEnabled = false; return (isOpened ? clearRTS(portHandle) : preclearRTS()); }
@Override public final int available() throws SerialPortIOException { if (!isOpened) throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); return bytesAvailable(portHandle); }
/** * Returns whether the DSR line is currently asserted. * @return Whether or not the DSR line is asserted. */ public final boolean getDSR() { return getDSR(portHandle); }
/** * Clears the BREAK signal from the serial control line. * @return true if successful, false if not. */ public final boolean clearBreak() { return clearBreak(portHandle); }
/** * Returns the number of bytes still waiting to be written in the device's output queue. * <p> * Note that this method is not required or guaranteed to be implemented by the underlying device driver. Use it carefully and test your application to ensure it is working as you expect. * * @return The number of bytes currently waiting to be written, or -1 if the port is not open. */ public final int bytesAwaitingWrite() { return bytesAwaitingWrite(portHandle); }
/** * Opens this serial port for reading and writing. * <p> * This method is equivalent to calling {@link #openPort} with a value of 1000. * <p> * All serial port parameters or timeouts can be changed at any time before or after the port has been opened. * <p> * Note that calling this method on an already opened port will simply reconfigure the port parameters. * * @return Whether the port was successfully opened. */ public final boolean openPort() { return openPort(1000); }
@Override public void sendStringToComm(String command) throws Exception { serialPort.writeBytes(command.getBytes(), command.length()); }
@Override public final long skip(long n) throws SerialPortIOException { if (!isOpened) throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); byte[] buffer = new byte[(int)n]; return readBytes(portHandle, buffer, n, 0); } }