1 /* 2 * Copyright © 2016 Greg Chabala 3 * 4 * This file is part of brick-control-lab. 5 * 6 * brick-control-lab is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU Lesser General Public License as 8 * published by the Free Software Foundation, either version 3 of the 9 * License, or (at your option) any later version. 10 * 11 * brick-control-lab is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public License 17 * along with brick-control-lab. If not, see http://www.gnu.org/licenses/. 18 */ 19 package org.chabala.brick.controllab; 20 21 import java.io.Closeable; 22 import java.io.IOException; 23 24 /** 25 * Interface to separate serial port clients from implementation. 26 */ 27 interface SerialPort extends Closeable { 28 29 /** 30 * The platform specific identifier for this port. 31 * @return the port name 32 */ 33 String getPortName(); 34 35 /** 36 * Open the port to start a connection. 37 * @throws IOException if any number of possible communication issues occurs 38 */ 39 void openPort() throws IOException; 40 41 /** 42 * Returns true if the port is open. 43 * @return true if the port is open 44 */ 45 boolean isOpen(); 46 47 /** 48 * Writes a single byte to the serial port, if it's open. 49 * @param b the byte to write 50 * @return true if the byte was successfully written 51 * @throws IOException if any number of possible communication issues occurs 52 */ 53 boolean write(byte b) throws IOException; 54 55 /** 56 * Writes a byte array to the serial port, if it's open. 57 * @param bytes the byte array to write 58 * @return true if the bytes were successfully written 59 * @throws IOException if any number of possible communication issues occurs 60 */ 61 boolean write(byte[] bytes) throws IOException; 62 63 /** 64 * Reads an array of bytes from the serial port. 65 * @param byteCount the number of bytes to read 66 * @return an array of bytes read from the serial port 67 * @throws IOException if any number of possible communication issues occurs 68 */ 69 byte[] readBytes(int byteCount) throws IOException; 70 71 /** 72 * Adds a listener to the serial port to handle incoming data and other events. 73 * @param listener the listener to add 74 * @throws IOException if any number of possible communication issues occurs 75 */ 76 void addEventListener(SerialPortEventListener listener) throws IOException; 77 78 /** 79 * Disconnects from the serial port and releases any resources. 80 * @throws IOException if any number of possible communication issues occurs 81 */ 82 @Override 83 void close() throws IOException; 84 }