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 2020/10/21 13:42:21 UTC
[plc4x] 03/08: Fixed "native" parsing and added Tests to fully
support SIMOTION Syntax in S7 Driver.
This is an automated email from the ASF dual-hosted git repository.
jfeinauer pushed a commit to branch rel/0.6
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit edcd9fd4063ec36ae4afb5e6110463e875bf0547
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Mon Aug 10 10:36:58 2020 +0200
Fixed "native" parsing and added Tests to fully support SIMOTION Syntax in S7 Driver.
---
.../org/apache/plc4x/java/s7/model/S7Field.java | 18 ++++++++++------
.../apache/plc4x/java/s7/model/S7FieldTests.java | 25 +++++++++++++++++++++-
2 files changed, 35 insertions(+), 8 deletions(-)
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 d539bdf..ef5dff4 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
@@ -229,13 +229,17 @@ public class S7Field implements PlcField {
byte[] addressData = Hex.decodeHex(fieldString.replaceAll("[-]", ""));
ReadBuffer rb = new ReadBuffer(addressData);
// Read out values according to definition in mspec
- final TransportSize transportSize = TransportSize.valueOf(rb.readByte(8));
- final short numberOfElements = rb.readShort(16);
- final short dbNumber = rb.readShort(16);
- final MemoryArea memoryArea = MemoryArea.valueOf(rb.readByte(8));
- assert 0x00 == rb.readByte(5);
- final short byteAddress = rb.readShort(16);
- final byte bitAddress = rb.readByte(3);
+ final short resvd = rb.readUnsignedShort(8);
+ if (0x10 != resvd) {
+ throw new PlcInvalidFieldException("Unsupported Field Type to parse!");
+ }
+ final TransportSize transportSize = TransportSize.valueOf((byte)rb.readUnsignedShort(8));
+ final short numberOfElements = (short)rb.readUnsignedInt(16);
+ final short dbNumber = (short)rb.readUnsignedInt(16);
+ final MemoryArea memoryArea = MemoryArea.valueOf((byte)rb.readUnsignedShort(8));
+ assert 0x00 == rb.readUnsignedShort(5);
+ final short byteAddress = (short)rb.readUnsignedInt(16);
+ final byte bitAddress = (byte)rb.readUnsignedShort(3);
return new S7Field(transportSize, memoryArea, dbNumber, byteAddress, bitAddress,
numberOfElements);
diff --git a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/model/S7FieldTests.java b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/model/S7FieldTests.java
index 3d4cb35..089dc09 100644
--- a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/model/S7FieldTests.java
+++ b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/model/S7FieldTests.java
@@ -19,12 +19,14 @@ under the License.
package org.apache.plc4x.java.s7.model;
+import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.s7.netty.model.types.MemoryArea;
import org.apache.plc4x.java.s7.netty.model.types.TransportSize;
import org.apache.plc4x.test.FastTests;
import org.junit.experimental.categories.Category;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
@@ -34,7 +36,10 @@ import java.util.stream.Stream;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
class S7FieldTests {
@@ -105,4 +110,22 @@ class S7FieldTests {
assertEquals(25, field.getNumElements());
}
+
+ @Test
+ public void testSimotionAddres() {
+ final S7Field s7Field = S7Field.of("10-01-00-01-00-2D-84-00-00-08");
+ Assertions.assertEquals(TransportSize.BOOL, s7Field.getDataType());
+ Assertions.assertEquals(1, s7Field.getNumElements());
+ Assertions.assertEquals(45, s7Field.getBlockNumber());
+ Assertions.assertEquals(MemoryArea.DATA_BLOCKS, s7Field.getMemoryArea());
+ Assertions.assertEquals(1, s7Field.getByteOffset());
+ Assertions.assertEquals(0, s7Field.getBitOffset());
+ }
+
+ @Test
+ public void testSimotionAddres_wrongMemoryArea_fails() {
+ assertThrows(PlcInvalidFieldException.class, () -> {
+ final S7Field s7Field = S7Field.of("A0-01-00-01-00-2D-84-00-00-08");
+ });
+ }
}
\ No newline at end of file