public String send(String command, boolean expectAnswer, long timeout) throws FTDWrapperException { ft_device.write(command.getBytes()); // Logger.log("%s sent", command); // ft_device.write("W".getBytes(StandardCharsets.US_ASCII)); if (!expectAnswer) return null; if (timeout > 0) { /* Try to wait for timeout. Continue silently if unsuccessful. */ try { Thread.sleep(timeout); } catch (InterruptedException e) { e.printStackTrace(); } } // TODO: Change to StringBuilder String out = new String(""); while (true) { int expectedDataSize = ft_device.getQueueStatus(); // Logger.log("Expected = %d", expectedDataSize); if (expectedDataSize > 0) { byte[] receivedData = new byte[expectedDataSize]; // Logger.log("Expected = %d", expectedDataSize); int readSize = ft_device.read(receivedData); if (readSize < expectedDataSize) throw new FTDWrapperException( "Could not read all data. Expected " + Integer.toString(expectedDataSize) + " bytes, read " + Integer.toString(readSize) + " bytes."); out += new String(receivedData); } else break; /*throw new IOException("Could not read any data. Waited for " + Long.getCSVString(timeout) + " but received no data. Check the connection or increase readWait."); */ } // Logger.log("recv = %s", out); return out; }