Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@ public interface ICommunicator {
* Write the byte array data to the device.
*
* @param data
* @param timeout Max waiting time for the end of this communication. [Unit: ms]
* @throws RuntimeException
*/
void write(byte[] data, int timeout) throws RuntimeException;
void write(byte[] data) throws RuntimeException;

/**
* Read the response from the device.
*
* @param length The max length of response wanted to be read.
* @param timeout Max waiting time for the end of this communication. [Unit: ms]
* @return
* @throws RuntimeException
*/
byte[] read(int length, int timeout) throws RuntimeException;
byte[] read(int length) throws RuntimeException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@

public class Ev3Protocol extends ProtocolBase {
private static final String KEY_VALUE = "value";
private static final int TIMEOUT = 1000;
private static final String TAG = "Ev3Protocol";
private static final byte OUTPUT_PORT_OFFSET = 0x10;

Expand Down Expand Up @@ -216,7 +215,7 @@ private float[] getSiValue(int port, int type, int mode, int nvalue) {
byteCode.addGlobalIndex((byte) 0x00);

// Send message
mCommunicator.write(byteCode.byteArray(), TIMEOUT);
mCommunicator.write(byteCode.byteArray());

byte[] reply = readData();

Expand Down Expand Up @@ -257,7 +256,7 @@ private short[] getPercentValue(int port, int type, int mode, int nvalue) {
byteCode.addGlobalIndex((byte) 0x00);

// Send message
mCommunicator.write(byteCode.byteArray(), TIMEOUT);
mCommunicator.write(byteCode.byteArray());

byte[] reply = readData();

Expand Down Expand Up @@ -308,7 +307,7 @@ private void setOutputState(int port, int speed) {
byteCode.addParameter(byteCodePort);

// Send message
mCommunicator.write(byteCode.byteArray(), TIMEOUT);
mCommunicator.write(byteCode.byteArray());
}

/**
Expand All @@ -331,7 +330,7 @@ private void soundTone(int volume, int freq, int duration) {
byteCode.addParameter((short) duration);

// Send message
mCommunicator.write(byteCode.byteArray(), TIMEOUT);
mCommunicator.write(byteCode.byteArray());
}

/**
Expand All @@ -341,11 +340,11 @@ private void soundTone(int volume, int freq, int duration) {
*/
private byte[] readData() {
// Calculate the size of response by reading 2 bytes.
byte[] header = mCommunicator.read(2, TIMEOUT);
byte[] header = mCommunicator.read(2);
int numBytes = ((header[1] & 0x00ff) << 8) | (header[0] & 0x00ff);

// Get result
byte[] result = mCommunicator.read(numBytes, TIMEOUT);
byte[] result = mCommunicator.read(numBytes);
Log.d(TAG, "read: " + result.length + " bytes");

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
*/
public class NxtProtocol extends ProtocolBase {
private static final String KEY_VALUE = "value";
private static final int TIMEOUT = 1000;
private static final String TAG = "NxtProtocol";
private static final int MAX_RES_LENGTH = 66;
private Map<Integer, Byte> mPortTypes;
Expand Down Expand Up @@ -197,7 +196,7 @@ private void sendData(byte[] request) {
System.arraycopy(request, 0, data, 2, request.length);

// Send request
mCommunicator.write(data, TIMEOUT);
mCommunicator.write(data);
}

private InputValues getInputValues(int port) {
Expand All @@ -207,7 +206,7 @@ private InputValues getInputValues(int port) {
(byte) port
};
sendData(request);
byte[] reply = mCommunicator.read(MAX_RES_LENGTH, TIMEOUT);
byte[] reply = mCommunicator.read(MAX_RES_LENGTH);
InputValues inputValues = new InputValues();
inputValues.inputPort = reply[3];
// 0 is false, 1 is true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

public class PileProtocol extends ProtocolBase {
private static final String KEY_VALUE = "value";
private static final int TIMEOUT = 1000;
private static final String TAG = "PileProtocol";

public PileProtocol(ICommunicator comm) {
Expand Down Expand Up @@ -113,8 +112,8 @@ private int requestOneByte(int port, PileConstants.CommandTypes type) {
PilePacketFormatter packet = new PilePacketFormatter(type);
packet.setDataByte((byte) port);
packet.calculateChecksum();
mCommunicator.write(packet.byteArray(), TIMEOUT);
byte[] receivedByteArray = mCommunicator.read(4, TIMEOUT);
mCommunicator.write(packet.byteArray());
byte[] receivedByteArray = mCommunicator.read(4);
packet = new PilePacketFormatter(receivedByteArray);
if (!packet.isValid())
return -1;
Expand All @@ -127,8 +126,8 @@ private boolean switchLed(boolean turnOn) {
PileConstants.LedState.ON.value()
: PileConstants.LedState.OFF.value());
packet.calculateChecksum();
mCommunicator.write(packet.byteArray(), TIMEOUT);
byte[] ack = mCommunicator.read(4, TIMEOUT);
mCommunicator.write(packet.byteArray());
byte[] ack = mCommunicator.read(4);
return ((ack[2] & 0x01) == 0x01);
}

Expand All @@ -142,8 +141,8 @@ private boolean setMotor(int port, int speed) {
packet.setDataByte((byte) (((port & 0x0F) << 2) | dir.value())); // Byte 0
packet.setDataByte((byte) (speed & 0xFF)); // Byte 1
packet.calculateChecksum();
mCommunicator.write(packet.byteArray(), TIMEOUT);
byte[] ack = mCommunicator.read(4, TIMEOUT);
mCommunicator.write(packet.byteArray());
byte[] ack = mCommunicator.read(4);
return ((ack[2] & 0x01) == 0x01);
}

Expand All @@ -152,8 +151,8 @@ public boolean apply() {
PilePacketFormatter packet = new PilePacketFormatter(PileConstants.CommandTypes.APPLY);
packet.setDataByte((byte) 0); // any data (1 byte) is OK
packet.calculateChecksum();
mCommunicator.write(packet.byteArray(), TIMEOUT);
byte[] ack = mCommunicator.read(4, TIMEOUT);
mCommunicator.write(packet.byteArray());
byte[] ack = mCommunicator.read(4);
return ((ack[2] & 0x01) == 0x01);
}

Expand All @@ -162,15 +161,15 @@ public byte[] load(int key) {
PilePacketFormatter packet = new PilePacketFormatter(PileConstants.CommandTypes.LOAD);
packet.setDataByte((byte) key); // any data (1 byte) is OK
packet.calculateChecksum();
mCommunicator.write(packet.byteArray(), TIMEOUT);
mCommunicator.write(packet.byteArray());

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte outputLength = mCommunicator.read(1, TIMEOUT)[0]; // read LENGTH info
byte outputLength = mCommunicator.read(1)[0]; // read LENGTH info
outputStream.write(outputLength);
// read the rest data
try {
// -1 means the length of LENGTH data
outputStream.write(mCommunicator.read((int) outputLength - 1, TIMEOUT));
outputStream.write(mCommunicator.read((int) outputLength - 1));
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -189,8 +188,8 @@ public boolean store(int key, byte[] data) {
packet.setDataByte(d);
}
packet.calculateChecksum();
mCommunicator.write(packet.byteArray(), TIMEOUT);
byte[] ack = mCommunicator.read(4, TIMEOUT);
mCommunicator.write(packet.byteArray());
byte[] ack = mCommunicator.read(4);
return ((ack[2] & 0x01) == 0x01);
}
}