示例#1
0
  /**
   * Method for retrieving data values from the sensor. BYTE0 ( is usually the primary data value
   * for the sensor. Data is read from registers in the sensor, usually starting at 0x00 and ending
   * around 0x49. Just supply the register to start reading at, and the length of bytes to read (16
   * maximum). NOTE: The NXT supplies UBYTE (unsigned byte) values but Java converts them into
   * signed bytes (probably more practical to return short/int?)
   *
   * @param register e.g. FACTORY_SCALE_DIVISOR, BYTE0, etc....
   * @param length Length of data to read (minimum 1, maximum 16)
   * @return the status
   */
  public int getData(int register, byte[] buf, int length) {
    byte[] txData = {address, (byte) register};
    try {
      nxtCommand.LSWrite(port, txData, (byte) length);
    } catch (IOException ioe) {
      System.out.println(ioe.getMessage());
    }

    byte[] status = null;
    do {
      try {
        status = nxtCommand.LSGetStatus(port);
      } catch (IOException ioe) {
        System.out.println(ioe.getMessage());
        return -1;
      }
    } while (status[0] == ErrorMessages.PENDING_COMMUNICATION_TRANSACTION_IN_PROGRESS
        | status[0] == ErrorMessages.SPECIFIED_CHANNEL_CONNECTION_NOT_CONFIGURED_OR_BUSY);

    try {
      byte[] ret = nxtCommand.LSRead(port);
      if (ret != null) System.arraycopy(ret, 0, buf, 0, ret.length);
    } catch (IOException ioe) {
      System.out.println(ioe.getMessage());
      return -1;
    }
    return status[0];
  }
示例#2
0
 /** @param s A sensor. e.g. Port.S1 */
 public I2CSensor(I2CPort s, byte sensorType) {
   port = (byte) s.getId();
   s.setTypeAndMode(sensorType, NXTProtocol.RAWMODE);
   // Flushes out any existing data
   try {
     nxtCommand.LSGetStatus(this.port);
     nxtCommand.LSRead(this.port);
   } catch (IOException ioe) {
     System.out.println(ioe.getMessage());
   }
 }