You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2019/01/16 15:16:56 UTC
[incubator-plc4x] branch develop updated: S7: fix of array
acquirement, fix of String aquirement, added additional scraper yml
This is an automated email from the ASF dual-hosted git repository.
jfeinauer pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new 4b3a57d S7: fix of array acquirement, fix of String aquirement, added additional scraper yml
4b3a57d is described below
commit 4b3a57d218b24aa1e601a0382b300e65715a509a
Author: Tim Mitsch <ti...@tmbeng.com>
AuthorDate: Mon Jan 14 17:19:21 2019 +0100
S7: fix of array acquirement, fix of String aquirement, added additional scraper yml
---
.../java/base/messages/DefaultPlcReadResponse.java | 13 ++++---
.../org/apache/plc4x/java/s7/model/S7Field.java | 21 +++++++++++
.../org/apache/plc4x/java/s7/netty/S7Protocol.java | 8 ++---
.../s7/netty/model/types/DataTransportSize.java | 4 +--
.../java/s7/netty/model/types/TransportSize.java | 6 ++--
.../strategies/DefaultS7MessageProcessor.java | 2 +-
.../src/test/resources/example_with_strings.yml | 41 ++++++++++++++++++++++
7 files changed, 81 insertions(+), 14 deletions(-)
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 6048e55..bb76759 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 class DefaultPlcReadResponse implements InternalPlcReadResponse {
@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 class DefaultPlcReadResponse implements InternalPlcReadResponse {
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 e3c0fa9..a460d6a 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 class S7Field implements PlcField {
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 class S7Field implements PlcField {
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 class S7Field implements PlcField {
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 4b17779..d52d4cc 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 @@ public class S7Protocol extends ChannelDuplexHandler {
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 0138179..ed4e23d 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 @@ import java.util.Map;
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 f9b03ee..6de428d 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 @@ public enum TransportSize {
// 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 4b1a090..439199a 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 @@ public class DefaultS7MessageProcessor implements S7MessageProcessor {
// 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 0000000..37309f0
--- /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]'
+