central
diff --git a/src/main/java/com/ghgande/j2mod/modbus/io/BytesOutputStream.java b/src/main/java/com/ghgande/j2mod/modbus/io/BytesOutputStream.java
index 65af0fc4..068b9b56 100644
--- a/src/main/java/com/ghgande/j2mod/modbus/io/BytesOutputStream.java
+++ b/src/main/java/com/ghgande/j2mod/modbus/io/BytesOutputStream.java
@@ -70,6 +70,17 @@ public void reset() {
count = 0;
}
+ /**
+ * Resets this BytesInputStream using the given
+ * byte[] as new input buffer.
+ *
+ * @param data a byte array with data to be read.
+ */
+ public void reset(byte[] data) {
+ buf = data;
+ count = 0;
+ }
+
@Override
public void writeBoolean(boolean v) throws IOException {
dataOutputStream.writeBoolean(v);
diff --git a/src/main/java/com/ghgande/j2mod/modbus/io/ModbusRTUTransport.java b/src/main/java/com/ghgande/j2mod/modbus/io/ModbusRTUTransport.java
index 3d513215..a36a3d2b 100644
--- a/src/main/java/com/ghgande/j2mod/modbus/io/ModbusRTUTransport.java
+++ b/src/main/java/com/ghgande/j2mod/modbus/io/ModbusRTUTransport.java
@@ -321,7 +321,8 @@ protected ModbusRequest readRequestIn(AbstractModbusListener listener) throws Mo
int[] crc = ModbusUtil.calculateCRC(inBuffer, 0, dlength); // does not include CRC
if (ModbusUtil.unsignedByteToInt(inBuffer[dlength]) != crc[0] || ModbusUtil.unsignedByteToInt(inBuffer[dlength + 1]) != crc[1]) {
if (logger.isDebugEnabled()) {
- logger.debug("CRC should be {}, {}", Integer.toHexString(crc[0]), Integer.toHexString(crc[1]));
+ logger.debug("CRC should be {}, {} inBuffer={}", Integer.toHexString(crc[0]), Integer.toHexString(crc[1]),
+ ModbusUtil.toHex(inBuffer, 0, dlength + 2));
}
// Drain the input in case the frame was misread and more
@@ -380,6 +381,8 @@ protected ModbusRequest readRequestIn(AbstractModbusListener listener) throws Mo
logger.debug("Read message not meant for us: {}", ModbusUtil.toHex(byteInputOutputStream.getBuffer(), 0, byteInputOutputStream.size()));
}
}
+ // as the buffer might have been bumped due to more than 256 bytes input, we reset it to the original inBuffer
+ byteInputOutputStream.reset(inBuffer);
}
}
}
diff --git a/src/main/java/com/ghgande/j2mod/modbus/io/ModbusSerialTransport.java b/src/main/java/com/ghgande/j2mod/modbus/io/ModbusSerialTransport.java
index fb20f333..317beb81 100644
--- a/src/main/java/com/ghgande/j2mod/modbus/io/ModbusSerialTransport.java
+++ b/src/main/java/com/ghgande/j2mod/modbus/io/ModbusSerialTransport.java
@@ -637,7 +637,8 @@ int getInterFrameDelay() {
return 1750;
}
else {
- return Math.max(getCharInterval(Modbus.INTER_MESSAGE_GAP), Modbus.MINIMUM_TRANSMIT_DELAY);
+ long delay = Math.max(getCharIntervalMicro(Modbus.INTER_MESSAGE_GAP), Modbus.MINIMUM_TRANSMIT_DELAY * 1000L);
+ return delay > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) delay;
}
}
@@ -660,17 +661,6 @@ long getMaxCharDelay() {
* Used for message timings.
*
* @param chars Number of characters
- * @return char interval in milliseconds
- */
- int getCharInterval(double chars) {
- return (int) (getCharIntervalMicro(chars) / 1000);
- }
-
- /**
- * Calculates an interval based on a set number of characters.
- * Used for message timings.
- *
- * @param chars Number of caracters
* @return microseconds
*/
long getCharIntervalMicro(double chars) {
diff --git a/src/main/java/com/ghgande/j2mod/modbus/msg/ReadFileRecordRequest.java b/src/main/java/com/ghgande/j2mod/modbus/msg/ReadFileRecordRequest.java
index 32bfbdeb..dece9283 100644
--- a/src/main/java/com/ghgande/j2mod/modbus/msg/ReadFileRecordRequest.java
+++ b/src/main/java/com/ghgande/j2mod/modbus/msg/ReadFileRecordRequest.java
@@ -19,7 +19,6 @@
import com.ghgande.j2mod.modbus.msg.ReadFileRecordResponse.RecordResponse;
import com.ghgande.j2mod.modbus.net.AbstractModbusListener;
import com.ghgande.j2mod.modbus.procimg.*;
-import com.ghgande.j2mod.modbus.procimg.Record;
import java.io.DataInput;
import java.io.DataOutput;
diff --git a/src/main/java/com/ghgande/j2mod/modbus/msg/WriteFileRecordRequest.java b/src/main/java/com/ghgande/j2mod/modbus/msg/WriteFileRecordRequest.java
index ec6f9dc5..912f2679 100644
--- a/src/main/java/com/ghgande/j2mod/modbus/msg/WriteFileRecordRequest.java
+++ b/src/main/java/com/ghgande/j2mod/modbus/msg/WriteFileRecordRequest.java
@@ -19,7 +19,6 @@
import com.ghgande.j2mod.modbus.msg.WriteFileRecordResponse.RecordResponse;
import com.ghgande.j2mod.modbus.net.AbstractModbusListener;
import com.ghgande.j2mod.modbus.procimg.*;
-import com.ghgande.j2mod.modbus.procimg.Record;
import java.io.DataInput;
import java.io.DataOutput;
diff --git a/src/main/java/com/ghgande/j2mod/modbus/net/TCPMasterConnection.java b/src/main/java/com/ghgande/j2mod/modbus/net/TCPMasterConnection.java
index 3720dc84..e6d34b3e 100644
--- a/src/main/java/com/ghgande/j2mod/modbus/net/TCPMasterConnection.java
+++ b/src/main/java/com/ghgande/j2mod/modbus/net/TCPMasterConnection.java
@@ -322,6 +322,7 @@ public void setNetworkInterface(NetworkInterface networkInterface) {
/**
* Sets the Proxy that this socket uses. If null (the default), no proxy is used.
+ * @param proxy the proxy as Proxy.
*/
public void setProxy(Proxy proxy) {
if(socket != null && proxy != null) {
diff --git a/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveFactory.java b/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveFactory.java
index 2c7d6483..381fb984 100644
--- a/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveFactory.java
+++ b/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveFactory.java
@@ -197,6 +197,7 @@ public static synchronized void close() {
/**
* Returns the running slave listening on the given port
*
+ * @param type Type of slave
* @param port Port to check for running slave
* @return Null or ModbusSlave
*/
@@ -207,6 +208,7 @@ public static synchronized ModbusSlave getSlave(ModbusSlaveType type, int port)
/**
* Returns the running slave listening on the given port
*
+ * @param type Type of slave
* @param port Port to check for running slave
* @return Null or ModbusSlave
*/
diff --git a/src/main/java/com/ghgande/j2mod/modbus/util/SerialParameters.java b/src/main/java/com/ghgande/j2mod/modbus/util/SerialParameters.java
index 83d28f2d..ca5601ac 100644
--- a/src/main/java/com/ghgande/j2mod/modbus/util/SerialParameters.java
+++ b/src/main/java/com/ghgande/j2mod/modbus/util/SerialParameters.java
@@ -395,29 +395,6 @@ public void setDatabits(String databits) {
}
}
- /**
- * Disables RS-485 control signals management.
- *
- * Please note that this is only effective on Linux.
- *
- * This function forces RS-485 mode to stay untouched,
- * regardless of the current setting of RS-485 mode.
- *
- * This function is necessary for Linux drivers that don't handle TIOCGRS485 properly.
- */
- public void disableRs485Control() {
- this.rs485DisableControl = true;
- }
-
- /**
- * Returns whether RS-485 control signals management is disabled.
- *
- * @return true if RS-485 control signals management is disabled.
- */
- public boolean isRs485ControlDisabled() {
- return this.rs485DisableControl;
- }
-
/**
* Returns the number of data bits as String.
*
@@ -924,6 +901,29 @@ public void setRs485DelayAfterTxMicroseconds(String microseconds) {
setRs485DelayAfterTxMicroseconds(Integer.parseInt(microseconds));
}
+ /**
+ * Disables RS-485 control signals management.
+ *
+ * Please note that this is only effective on Linux.
+ *
+ * This function forces RS-485 mode to stay untouched,
+ * regardless of the current setting of RS-485 mode.
+ *
+ * This function is necessary for Linux drivers that don't handle TIOCGRS485 properly.
+ */
+ public void disableRs485Control() {
+ this.rs485DisableControl = true;
+ }
+
+ /**
+ * Returns whether RS-485 control signals management is disabled.
+ *
+ * @return true if RS-485 control signals management is disabled.
+ */
+ public boolean isRs485ControlDisabled() {
+ return this.rs485DisableControl;
+ }
+
@Override
public String toString() {
return "SerialParameters{" +
diff --git a/src/test/java/com/ghgande/j2mod/modbus/TestModbusTCPMasterEquality.java b/src/test/java/com/ghgande/j2mod/modbus/TestModbusTCPMasterEquality.java
index 424e2837..2b15b9c0 100644
--- a/src/test/java/com/ghgande/j2mod/modbus/TestModbusTCPMasterEquality.java
+++ b/src/test/java/com/ghgande/j2mod/modbus/TestModbusTCPMasterEquality.java
@@ -9,7 +9,6 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.*;
/**
* Testing class for {@link ModbusTCPMaster#equals} & {@link ModbusTCPMaster#hashCode} methods
diff --git a/src/test/java/com/ghgande/j2mod/modbus/cmd/TCPSlaveTest.java b/src/test/java/com/ghgande/j2mod/modbus/cmd/TCPSlaveTest.java
index 27769029..964bb628 100644
--- a/src/test/java/com/ghgande/j2mod/modbus/cmd/TCPSlaveTest.java
+++ b/src/test/java/com/ghgande/j2mod/modbus/cmd/TCPSlaveTest.java
@@ -16,7 +16,6 @@
package com.ghgande.j2mod.modbus.cmd;
import com.ghgande.j2mod.modbus.Modbus;
-import com.ghgande.j2mod.modbus.procimg.Record;
import com.ghgande.j2mod.modbus.procimg.*;
import com.ghgande.j2mod.modbus.slave.ModbusSlave;
import com.ghgande.j2mod.modbus.slave.ModbusSlaveFactory;
diff --git a/src/test/java/com/ghgande/j2mod/modbus/cmd/UDPSlaveTest.java b/src/test/java/com/ghgande/j2mod/modbus/cmd/UDPSlaveTest.java
index ee0d759b..9910a0a1 100644
--- a/src/test/java/com/ghgande/j2mod/modbus/cmd/UDPSlaveTest.java
+++ b/src/test/java/com/ghgande/j2mod/modbus/cmd/UDPSlaveTest.java
@@ -17,7 +17,6 @@
import com.ghgande.j2mod.modbus.Modbus;
import com.ghgande.j2mod.modbus.procimg.*;
-import com.ghgande.j2mod.modbus.procimg.Record;
import com.ghgande.j2mod.modbus.slave.ModbusSlave;
import com.ghgande.j2mod.modbus.slave.ModbusSlaveFactory;
import org.slf4j.Logger;
diff --git a/src/test/java/com/ghgande/j2mod/modbus/utils/AbstractTestModbus.java b/src/test/java/com/ghgande/j2mod/modbus/utils/AbstractTestModbus.java
index 2e223c70..f9d7e655 100644
--- a/src/test/java/com/ghgande/j2mod/modbus/utils/AbstractTestModbus.java
+++ b/src/test/java/com/ghgande/j2mod/modbus/utils/AbstractTestModbus.java
@@ -16,7 +16,6 @@
package com.ghgande.j2mod.modbus.utils;
import com.ghgande.j2mod.modbus.procimg.*;
-import com.ghgande.j2mod.modbus.procimg.Record;
import com.ghgande.j2mod.modbus.slave.ModbusSlave;
import com.ghgande.j2mod.modbus.util.Observable;
import com.ghgande.j2mod.modbus.util.Observer;