You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by GitBox <gi...@apache.org> on 2019/01/15 20:39:29 UTC

[incubator-plc4x] Diff for: [GitHub] JulianFeinauer merged pull request #43: S7 fix of array and String acquirement

diff --git a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/DefaultPlcReadResponse.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/DefaultPlcReadResponse.java
index 6048e55b3..bb7675988 100644
--- a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/DefaultPlcReadResponse.java
+++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/DefaultPlcReadResponse.java
@@ -78,7 +78,12 @@ public PlcResponseCode getResponseCode(String name) {
 
     @Override
     public Object getObject(String name) {
-        return getObject(name, 0);
+        if(getFieldInternal(name).getNumberOfValues()>1) {
+            return getAllObjects(name);
+        }
+        else{
+            return getObject(name, 0);
+        }
     }
 
     @Override
@@ -91,11 +96,11 @@ public Object getObject(String name, int index) {
     public Collection<Object> getAllObjects(String name) {
         BaseDefaultFieldItem fieldInternal = getFieldInternal(name);
         int num = fieldInternal.getNumberOfValues();
-        List<Object> values = new ArrayList<>(num);
+        List<Object> objectList = new ArrayList<>(num);
         for (int i = 0; i < num; i++) {
-            values.add(fieldInternal.getObject(i));
+            objectList.add(fieldInternal.getObject(i));
         }
-        return values;
+        return objectList;
     }
 
     @Override
diff --git a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/model/S7Field.java b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/model/S7Field.java
index e3c0fa919..a460d6aa6 100644
--- a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/model/S7Field.java
+++ b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/model/S7Field.java
@@ -104,6 +104,7 @@ public static S7Field of(String fieldString) {
             if(matcher.group(NUM_ELEMENTS) != null) {
                 numElements = Integer.parseInt(matcher.group(NUM_ELEMENTS));
             }
+            numElements = calcNumberOfElementsForStringTypes(numElements,dataType);
             if(!transferSizeCode.isEmpty() && !dataType.getSizeCode().equals(transferSizeCode)) {
                 throw new PlcInvalidFieldException("Transfer size code '" + transferSizeCode +
                     "' doesn't match specified data type '" + dataType.name() + "'");
@@ -126,6 +127,7 @@ public static S7Field of(String fieldString) {
                 if(matcher.group(NUM_ELEMENTS) != null) {
                     numElements = Integer.parseInt(matcher.group(NUM_ELEMENTS));
                 }
+                numElements = calcNumberOfElementsForStringTypes(numElements,dataType);
                 if(!transferSizeCode.isEmpty() && !dataType.getSizeCode().equals(transferSizeCode)) {
                     throw new PlcInvalidFieldException("Transfer size code '" + transferSizeCode +
                         "' doesn't match specified data type '" + dataType.name() + "'");
@@ -136,4 +138,23 @@ public static S7Field of(String fieldString) {
         throw new PlcInvalidFieldException("Unable to parse address: " + fieldString);
     }
 
+    /**
+     * correct the storage of "array"-like variables like STRING
+     * @param numElements auto-detected numElements (1 if no numElements in brackets has been given, x if a specific number has been given)
+     * @param dataType detected Transport-Size that represents the data-type
+     * @return corrected numElements if nessesary
+     */
+    private static int calcNumberOfElementsForStringTypes(int numElements,TransportSize dataType){
+        //if no String nothing has to be done
+        if(!dataType.equals(TransportSize.STRING)){
+            return numElements;
+        }
+        //on valid String-length add two byte because of S7-representation of Strings
+        if(numElements>1 && numElements<=254){
+            return numElements+2;
+        }
+        //connection String usage with "STRING" only --> numElements=1 --> enter default value
+        return 256;
+    }
+
 }
diff --git a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/S7Protocol.java b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/S7Protocol.java
index 4b17779da..d52d4cc05 100644
--- a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/S7Protocol.java
+++ b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/S7Protocol.java
@@ -676,15 +676,15 @@ private S7Parameter decodeParameter(ByteBuf in, boolean isResponse) {
                 return decodeCpuServicesParameter(in);
             case READ_VAR:
             case WRITE_VAR:
-                List<VarParameterItem> varParamameter;
+                List<VarParameterItem> varParameterItems;
                 byte numItems = in.readByte();
                 if (!isResponse) {
-                    varParamameter = decodeReadWriteVarParameter(in, numItems);
+                    varParameterItems = decodeReadWriteVarParameter(in, numItems);
                 } else {
-                    varParamameter = Collections.singletonList(
+                    varParameterItems = Collections.singletonList(
                         new S7AnyVarParameterItem(null, null, null, numItems, (short) 0, (short) 0, (byte) 0));
                 }
-                return new VarParameter(parameterType, varParamameter);
+                return new VarParameter(parameterType, varParameterItems);
             case SETUP_COMMUNICATION:
                 // Reserved (is always constant 0x00)
                 in.readByte();
diff --git a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/model/types/DataTransportSize.java b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/model/types/DataTransportSize.java
index 0138179c5..ed4e23d36 100644
--- a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/model/types/DataTransportSize.java
+++ b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/model/types/DataTransportSize.java
@@ -27,11 +27,11 @@ Licensed to the Apache Software Foundation (ASF) under one
 public enum DataTransportSize {
     NULL((byte) 0x00, false, false),
     BIT((byte) 0x03, true, true),
-    BYTE_WORD_DWORD((byte) 0x04, true, true),
+    BYTE_WORD_DWORD((byte) 0x04, true, false),
     INTEGER((byte) 0x05, true, false),
     DINTEGER((byte) 0x06, false, false),
     REAL((byte) 0x07, false, false),
-    OCTET_STRING((byte) 0x09, false, true);
+    OCTET_STRING((byte) 0x09, false, false);
 
     private static final Map<Byte, DataTransportSize> map;
     static {
diff --git a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/model/types/TransportSize.java b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/model/types/TransportSize.java
index f9b03ee1c..6de428d92 100644
--- a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/model/types/TransportSize.java
+++ b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/model/types/TransportSize.java
@@ -103,13 +103,13 @@ Licensed to the Apache Software Foundation (ASF) under one
     // Single-byte character
     CHAR(0x03, "B", 1, null, DataTransportSize.BYTE_WORD_DWORD, S7ControllerType.ANY),
     // Double-byte character
+    // TODO: Find the code (Eventually 0x13)
     WCHAR(0x13, "X", 2, null, null, S7ControllerType.S7_1200, S7ControllerType.S7_1500),
     // Variable-length single-byte character string
-    // TODO: Find the code (Eventually 0x03)
-    STRING(0x00, "X", -1, null, null, S7ControllerType.ANY),
+    STRING(0x03, "X", 1, null, DataTransportSize.BYTE_WORD_DWORD, S7ControllerType.ANY),
     // Variable-length double-byte character string
     // TODO: Find the code (Eventually 0x13)
-    WSTRING(0x00, "X", -1, null, null, S7ControllerType.S7_1200, S7ControllerType.S7_1500);
+    WSTRING(0x00, "X", 1, null, null, S7ControllerType.S7_1200, S7ControllerType.S7_1500);
 
     /* TO BE CONTINUED */
 
diff --git a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/strategies/DefaultS7MessageProcessor.java b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/strategies/DefaultS7MessageProcessor.java
index 4b1a090fe..439199a70 100644
--- a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/strategies/DefaultS7MessageProcessor.java
+++ b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/strategies/DefaultS7MessageProcessor.java
@@ -413,7 +413,7 @@ private S7ResponseMessage getMergedResponseMessage(S7RequestMessage requestMessa
 
                     // Initialize the current size, this will be lower than the original, as the only
                     // way to have different count, is if the request was split up.
-                    int curSizeInBytes = responseParameterItem.getNumElements() * itemSizeInBytes;
+                    int curSizeInBytes = requestItem.getNumElements() * itemSizeInBytes;
 
                     // Now iterate over the succeeding pairs of parameters and payloads till we have
                     // found the original number of elements.
diff --git a/plc4j/utils/scraper/src/test/resources/example_with_strings.yml b/plc4j/utils/scraper/src/test/resources/example_with_strings.yml
new file mode 100644
index 000000000..37309f02e
--- /dev/null
+++ b/plc4j/utils/scraper/src/test/resources/example_with_strings.yml
@@ -0,0 +1,41 @@
+# ----------------------------------------------------------------------------
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# ----------------------------------------------------------------------------
+---
+sources:
+  S7_TIM: s7://192.168.167.210/0/1
+
+jobs:
+  - name: pressure-job
+    scrapeRate: 1000
+    sources:
+    - S7_TIM
+    fields:
+      test1: '%DB810:DBW0:UINT'
+      test2: '%DB810:DBX4:STRING'
+      test3: '%DB810:DBX264:STRING'
+      test4: '%DB810:DBX524:STRING'
+      test5: '%DB810:DBX784:STRING'
+      test6: '%DB810:DBX1044:STRING'
+      test7: '%DB810:DBD0:REAL'
+      test8: '%DB811:DBX12:STRING'
+      test9: '%DB811:DBX280:STRING'
+      test10: '%DB811:DBB1000:BYTE[8]'
+      test11: '%DB811:DBX268.3:BOOL'
+      test12: '%DB811:DBB270:BYTE[8]'
+


With regards,
Apache Git Services