Skip to content

Commit

Permalink
fixed upper bounds for registers.
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Jul 26, 2018
1 parent 947579f commit dbc6812
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ private byte[] produceCoilValues(List<?> values) throws PlcProtocolException {

private byte[] produceRegisterValue(List<?> values) throws PlcProtocolException {
ByteBuf buffer = Unpooled.buffer();
long upperRegisterValue = 0xFFFFL;
for (Object value : values) {
if (value.getClass() == Boolean.class) {
buffer.writeByte(0);
Expand All @@ -361,7 +362,7 @@ private byte[] produceRegisterValue(List<?> values) throws PlcProtocolException
}
buffer.writeShort((short) value);
} else if (value.getClass() == Integer.class) {
if ((int) value > Integer.MAX_VALUE) {
if ((int) value > upperRegisterValue) {
throw new PlcProtocolException("Value to high to fit into register for Integer: " + value);
}
if ((int) value < 0) {
Expand All @@ -376,7 +377,7 @@ private byte[] produceRegisterValue(List<?> values) throws PlcProtocolException
throw new PlcProtocolException("Value to high to fit into register for BigInteger: " + value);
}
// TODO: for now we can't support big values as we only write one register at once
if (((BigInteger) value).compareTo(BigInteger.valueOf(0XFFFFL)) > 0) {
if (((BigInteger) value).compareTo(BigInteger.valueOf(upperRegisterValue)) > 0) {
throw new PlcProtocolException("Value to high to fit into register for BigInteger: " + value);
}
// TODO: Register has 2 bytes so we trim to 2 instead of 4 like the second if above
Expand All @@ -395,15 +396,15 @@ private byte[] produceRegisterValue(List<?> values) throws PlcProtocolException
if (((float) value) < 0) {
throw new PlcProtocolException("Only positive values are supported for Float: " + value);
}
if (((float) value) > Integer.MAX_VALUE) {
if (((float) value) > upperRegisterValue) {
throw new PlcProtocolException("Value to high to fit into register for Float: " + value);
}
buffer.writeShort(Math.round((float) value));
} else if (value.getClass() == Double.class) {
if (((double) value) < 0) {
throw new PlcProtocolException("Only positive values are supported for Double: " + value);
}
if (((double) value) > Integer.MAX_VALUE) {
if (((double) value) > upperRegisterValue) {
throw new PlcProtocolException("Value to high to fit into register for Double: " + value);
}
buffer.writeShort((int) Math.round((double) value));
Expand Down

0 comments on commit dbc6812

Please sign in to comment.