Skip to content

Commit

Permalink
Added better error messages, sleepTime in RTUDataEncoder is now custo…
Browse files Browse the repository at this point in the history
…mizable
  • Loading branch information
retrodaredevil committed Dec 20, 2019
1 parent 366b2b2 commit 0f969c6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/main/java/me/retrodaredevil/io/IOBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public interface IOBundle extends AutoCloseable {
* This should be overridden by subclasses to close the input and output streams
* @throws Exception If this cannot be closed
*/
@SuppressWarnings("RedundantThrows")
@Override
default void close() throws Exception {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ public static ModbusMessage fromAscii(int expectedAddress, byte[] bytes){
}
int actualLrc = RedundancyUtil.calculateLRC(data);
if(expectedLrc != actualLrc){
System.err.println(Arrays.toString(bytes));
System.err.println(Arrays.toString(data));
throw new RedundancyException("LRC", expectedLrc, actualLrc);
throw new RedundancyException("LRC", expectedLrc, actualLrc, "bytes: " + Arrays.toString(bytes));
}
return ModbusMessages.createMessage(functionCode, data);
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/me/retrodaredevil/io/modbus/RTUDataEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
public class RTUDataEncoder implements IODataEncoder {
private final long initialTimeout;
private final long endMillis;
public RTUDataEncoder(long initialTimeout, long endMillis){
private final long sleepTime;
public RTUDataEncoder(long initialTimeout, long endMillis, long sleepTime){
this.initialTimeout = initialTimeout;
this.endMillis = endMillis;
this.sleepTime = sleepTime;
}
public RTUDataEncoder(long initialTimeout, long endMillis){
this(initialTimeout, endMillis, 3);
}
public RTUDataEncoder(){
this(1000, 10);
Expand All @@ -37,9 +42,6 @@ public static byte[] toBytes(int address, ModbusMessage message){
bytes[1] = code;

System.arraycopy(data, 0, bytes, 2, data.length);
// for(int i = 0; i < data.length; i++){
// bytes[i + 2] = data[i];
// }
bytes[data.length + 2] = lowCrc;
bytes[data.length + 3] = highCrc;
return bytes;
Expand Down Expand Up @@ -69,9 +71,7 @@ public static ModbusMessage fromBytes(int expectedAddress, byte[] bytes){
int expectedCrc = 0xFFFF & ((highCrc << 8) | lowCrc);
int actualCrc = RedundancyUtil.calculateCRC(getCrcBytes(address, code, data));
if(expectedCrc != actualCrc){
System.out.println(Arrays.toString(bytes));
System.out.println(Arrays.toString(data));
throw new RedundancyException("CRC", expectedCrc, actualCrc);
throw new RedundancyException("CRC", expectedCrc, actualCrc, "bytes: " + Arrays.toString(bytes));
}

return ModbusMessages.createMessage(code, data);
Expand Down Expand Up @@ -102,7 +102,7 @@ private byte[] readBytes(InputStream inputStream) throws IOException {
long currentTime = System.currentTimeMillis();
if(lastData == null){ // not started
if(startTime + initialTimeout < currentTime){
throw new ModbusTimeoutException("Timed out!");
throw new ModbusTimeoutException("Timed out! startTime=" + startTime + " currentTime=" + currentTime + " initialTimeout=" + initialTimeout);
}
} else {
if(lastData + endMillis < currentTime){
Expand All @@ -116,7 +116,7 @@ private byte[] readBytes(InputStream inputStream) throws IOException {
}
}
try{
Thread.sleep(3);
Thread.sleep(sleepTime);
} catch(InterruptedException ex){
throw new RuntimeException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package me.retrodaredevil.io.modbus;

public class RedundancyException extends ModbusRuntimeException {

public RedundancyException(String redundancyType, int expected, int actual){
this("Incorrect " + redundancyType + " checksum. Expected: " + expected + " but got: " + actual);
this(redundancyType, expected, actual, "");
}

public RedundancyException(String redundancyType, int expected, int actual, String extra){
this("Incorrect " + redundancyType + " checksum. Expected: " + expected + " but got: " + actual + (extra.isEmpty() ? "" : " " + extra));
}

public RedundancyException() {
Expand Down

0 comments on commit 0f969c6

Please sign in to comment.