private void getRidOfResidueData()
      throws FTDWrapperNotOpenException, FTDWrapperProcessNullException,
          FTDWrapperBadQueueStatusException, FTDWrapperBadReadStatusException {
    /* Get rid of residue data*/

    byte[] data;
    int residueData = ft_device.getQueueStatus();
    // Logger.log("Residue = %d", residueData);
    while (residueData > 0) {
      data = new byte[residueData];
      ft_device.read(data);
      residueData = ft_device.getQueueStatus();
    }
  }
  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;
  }
  public SerialPortCommunicator(Context context)
      throws D2xxManager.D2xxException, FTDWrapperBadReadStatusException,
          FTDWrapperProcessNullException, FTDWrapperBadQueueStatusException,
          FTDWrapperNotOpenException {

    // Logger.log("Opening serial port");
    D2xxManager ftD2xx;

    ftD2xx = D2xxManager.getInstance(context);
    if (ftD2xx.createDeviceInfoList(context) > 0) {
      ft_device = new FTDeviceWrapper(ftD2xx.openByIndex(context, 0));
      /*if (ft_device.getFt_device().isOpen())
          Logger.log("Serial port opened!");
      else
          Logger.log("Serial port not opened!");*/
    } else
      throw new D2xxManager.D2xxException(
          "Cannot open the device. Make sure the hardware is connected and turned on.");

    /*
    Set the following settings
    	baudRate = 9600;
    	stopBit = 1;
    	dataBit = 8;
    	parity = 0;
    	flowControl = 0;
    */

    ft_device.setBitMode((byte) 0, D2xxManager.FT_BITMODE_RESET);
    ft_device.setBaudRate(BAUDRATE);
    ft_device.setDataCharacteristics(
        D2xxManager.FT_DATA_BITS_8, D2xxManager.FT_STOP_BITS_1, D2xxManager.FT_PARITY_NONE);
    ft_device.setFlowControl(D2xxManager.FT_FLOW_NONE, XON, XOFF);

    /* ft_device is set and ready */
    // Logger.log("ft_device is set and ready");

    getRidOfResidueData();
  }