Skip to content

Commit 9548f6e

Browse files
committed
Merge pull request #6 from myusak/develop
Remove timeout from ICommunicator#write and ICommunicator#read
2 parents 5d8db6b + aa1bce1 commit 9548f6e

4 files changed

Lines changed: 23 additions & 28 deletions

File tree

src/main/java/com/pileproject/drivecommand/model/com/ICommunicator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,16 @@ public interface ICommunicator {
3636
* Write the byte array data to the device.
3737
*
3838
* @param data
39-
* @param timeout Max waiting time for the end of this communication. [Unit: ms]
4039
* @throws RuntimeException
4140
*/
42-
void write(byte[] data, int timeout) throws RuntimeException;
41+
void write(byte[] data) throws RuntimeException;
4342

4443
/**
4544
* Read the response from the device.
4645
*
4746
* @param length The max length of response wanted to be read.
48-
* @param timeout Max waiting time for the end of this communication. [Unit: ms]
4947
* @return
5048
* @throws RuntimeException
5149
*/
52-
byte[] read(int length, int timeout) throws RuntimeException;
50+
byte[] read(int length) throws RuntimeException;
5351
}

src/main/java/com/pileproject/drivecommand/model/ev3/Ev3Protocol.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363

6464
public class Ev3Protocol extends ProtocolBase {
6565
private static final String KEY_VALUE = "value";
66-
private static final int TIMEOUT = 1000;
6766
private static final String TAG = "Ev3Protocol";
6867
private static final byte OUTPUT_PORT_OFFSET = 0x10;
6968

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

218217
// Send message
219-
mCommunicator.write(byteCode.byteArray(), TIMEOUT);
218+
mCommunicator.write(byteCode.byteArray());
220219

221220
byte[] reply = readData();
222221

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

259258
// Send message
260-
mCommunicator.write(byteCode.byteArray(), TIMEOUT);
259+
mCommunicator.write(byteCode.byteArray());
261260

262261
byte[] reply = readData();
263262

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

310309
// Send message
311-
mCommunicator.write(byteCode.byteArray(), TIMEOUT);
310+
mCommunicator.write(byteCode.byteArray());
312311
}
313312

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

333332
// Send message
334-
mCommunicator.write(byteCode.byteArray(), TIMEOUT);
333+
mCommunicator.write(byteCode.byteArray());
335334
}
336335

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

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

351350
return result;

src/main/java/com/pileproject/drivecommand/model/nxt/NxtProtocol.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
*/
4949
public class NxtProtocol extends ProtocolBase {
5050
private static final String KEY_VALUE = "value";
51-
private static final int TIMEOUT = 1000;
5251
private static final String TAG = "NxtProtocol";
5352
private static final int MAX_RES_LENGTH = 66;
5453
private Map<Integer, Byte> mPortTypes;
@@ -197,7 +196,7 @@ private void sendData(byte[] request) {
197196
System.arraycopy(request, 0, data, 2, request.length);
198197

199198
// Send request
200-
mCommunicator.write(data, TIMEOUT);
199+
mCommunicator.write(data);
201200
}
202201

203202
private InputValues getInputValues(int port) {
@@ -207,7 +206,7 @@ private InputValues getInputValues(int port) {
207206
(byte) port
208207
};
209208
sendData(request);
210-
byte[] reply = mCommunicator.read(MAX_RES_LENGTH, TIMEOUT);
209+
byte[] reply = mCommunicator.read(MAX_RES_LENGTH);
211210
InputValues inputValues = new InputValues();
212211
inputValues.inputPort = reply[3];
213212
// 0 is false, 1 is true.

src/main/java/com/pileproject/drivecommand/model/pile/PileProtocol.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
public class PileProtocol extends ProtocolBase {
3131
private static final String KEY_VALUE = "value";
32-
private static final int TIMEOUT = 1000;
3332
private static final String TAG = "PileProtocol";
3433

3534
public PileProtocol(ICommunicator comm) {
@@ -113,8 +112,8 @@ private int requestOneByte(int port, PileConstants.CommandTypes type) {
113112
PilePacketFormatter packet = new PilePacketFormatter(type);
114113
packet.setDataByte((byte) port);
115114
packet.calculateChecksum();
116-
mCommunicator.write(packet.byteArray(), TIMEOUT);
117-
byte[] receivedByteArray = mCommunicator.read(4, TIMEOUT);
115+
mCommunicator.write(packet.byteArray());
116+
byte[] receivedByteArray = mCommunicator.read(4);
118117
packet = new PilePacketFormatter(receivedByteArray);
119118
if (!packet.isValid())
120119
return -1;
@@ -127,8 +126,8 @@ private boolean switchLed(boolean turnOn) {
127126
PileConstants.LedState.ON.value()
128127
: PileConstants.LedState.OFF.value());
129128
packet.calculateChecksum();
130-
mCommunicator.write(packet.byteArray(), TIMEOUT);
131-
byte[] ack = mCommunicator.read(4, TIMEOUT);
129+
mCommunicator.write(packet.byteArray());
130+
byte[] ack = mCommunicator.read(4);
132131
return ((ack[2] & 0x01) == 0x01);
133132
}
134133

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

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

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

167166
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
168-
byte outputLength = mCommunicator.read(1, TIMEOUT)[0]; // read LENGTH info
167+
byte outputLength = mCommunicator.read(1)[0]; // read LENGTH info
169168
outputStream.write(outputLength);
170169
// read the rest data
171170
try {
172171
// -1 means the length of LENGTH data
173-
outputStream.write(mCommunicator.read((int) outputLength - 1, TIMEOUT));
172+
outputStream.write(mCommunicator.read((int) outputLength - 1));
174173
} catch (IOException e) {
175174
e.printStackTrace();
176175
}
@@ -189,8 +188,8 @@ public boolean store(int key, byte[] data) {
189188
packet.setDataByte(d);
190189
}
191190
packet.calculateChecksum();
192-
mCommunicator.write(packet.byteArray(), TIMEOUT);
193-
byte[] ack = mCommunicator.read(4, TIMEOUT);
191+
mCommunicator.write(packet.byteArray());
192+
byte[] ack = mCommunicator.read(4);
194193
return ((ack[2] & 0x01) == 0x01);
195194
}
196195
}

0 commit comments

Comments
 (0)