/** * Send data top the sensor * * @param register A data register in the I2C sensor. * @param data The byte to send. * @param length the number of bytes */ public int sendData(int register, byte[] data, int length) { byte[] txData = {address, (byte) register}; byte[] sendData = new byte[length + 2]; System.arraycopy(txData, 0, sendData, 0, 2); System.arraycopy(data, 0, sendData, 2, length); try { return nxtCommand.LSWrite(this.port, sendData, (byte) 0); } catch (IOException ioe) { System.out.println(ioe.getMessage()); return -1; } }
/** * 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]; }