You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/07/26 13:50:51 UTC

[incubator-plc4x] branch master updated: fixed upper bounds for registers.

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/master by this push:
     new dbc6812  fixed upper bounds for registers.
dbc6812 is described below

commit dbc6812517b065e5a000eee324b8548eb3107249
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Jul 26 15:50:48 2018 +0200

    fixed upper bounds for registers.
---
 .../org/apache/plc4x/java/modbus/netty/Plc4XModbusProtocol.java  | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/netty/Plc4XModbusProtocol.java b/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/netty/Plc4XModbusProtocol.java
index cc9655f..3c9e98b 100644
--- a/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/netty/Plc4XModbusProtocol.java
+++ b/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/netty/Plc4XModbusProtocol.java
@@ -342,6 +342,7 @@ public class Plc4XModbusProtocol extends MessageToMessageCodec<ModbusTcpPayload,
 
     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);
@@ -361,7 +362,7 @@ public class Plc4XModbusProtocol extends MessageToMessageCodec<ModbusTcpPayload,
                 }
                 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) {
@@ -376,7 +377,7 @@ public class Plc4XModbusProtocol extends MessageToMessageCodec<ModbusTcpPayload,
                     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
@@ -395,7 +396,7 @@ public class Plc4XModbusProtocol extends MessageToMessageCodec<ModbusTcpPayload,
                 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));
@@ -403,7 +404,7 @@ public class Plc4XModbusProtocol extends MessageToMessageCodec<ModbusTcpPayload,
                 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));