You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jm...@apache.org on 2018/01/10 04:57:36 UTC

[incubator-plc4x] 02/02: Add support for Stings

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

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

commit 9e04f9260a9aa5f4bc6b1ae48e9d5220cae091e7
Author: Justin Mclean <jm...@apache.org>
AuthorDate: Wed Jan 10 15:57:26 2018 +1100

    Add support for Stings
---
 .../org/apache/plc4x/java/s7/netty/Plc4XS7Protocol.java | 17 ++++++++++++++++-
 .../apache/plc4x/java/s7/netty/Plc4XS7ProtocolTest.java |  8 +-------
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/Plc4XS7Protocol.java b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/Plc4XS7Protocol.java
index daaffdb..b3cf244 100644
--- a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/Plc4XS7Protocol.java
+++ b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/Plc4XS7Protocol.java
@@ -334,7 +334,18 @@ public class Plc4XS7Protocol extends MessageToMessageCodec<S7Message, PlcRequest
                 result[(i * 4) + 3] = (byte) (intValue & 0xff);
             }
         } else if (valueType == String.class) {
-            result = new byte[]{};
+            int size = 0;
+            for (Object value : values) {
+                size = size + ((String) value).length();
+            }
+            result = new byte[size];
+            int j = 0;
+            for (Object value : values) {
+                String str = (String) value;
+                for (int i = 0; i < str.length(); i++) {
+                    result[j++] = (byte) str.charAt(i);
+                }
+            }
         }
         return result;
     }
@@ -386,6 +397,10 @@ public class Plc4XS7Protocol extends MessageToMessageCodec<S7Message, PlcRequest
                     ((s7Data[i + 2] & 0xff) << 8) | (s7Data[i + 3] & 0xff);
                 result.add(Float.intBitsToFloat(intValue));
                 i+=4;
+            } else if (datatype == String.class) {
+                String str = new String(s7Data);
+                result.add(str);
+                i += length;
             } else {
                 throw new PlcProtocolException("Unsupported datatype " + datatype.getSimpleName());
             }
diff --git a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/Plc4XS7ProtocolTest.java b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/Plc4XS7ProtocolTest.java
index adafac3..786e14c 100644
--- a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/Plc4XS7ProtocolTest.java
+++ b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/Plc4XS7ProtocolTest.java
@@ -84,11 +84,6 @@ public class Plc4XS7ProtocolTest extends NettyTestBase {
     @MethodSource("typeAndAddressProvider")
     @Tag("fast")
     public void decode(Class<?> type, S7Address address) throws Exception {
-        // TODO: finish me
-        if (type == String.class) {
-            // String seems not yet decodable
-            return;
-        }
         // Read Test
         {
             short fakeTpduReference = (short) 1;
@@ -201,8 +196,7 @@ public class Plc4XS7ProtocolTest extends NettyTestBase {
             data = new byte[]{(byte) 0b0000_0000, (byte) 0b0000_0000, (byte) 0b0000_0000, (byte) 0b0000_0000};
         } else if (type == String.class) {
             size = DataTransportSize.BYTE_WORD_DWORD;
-            // TODO: what size is string?
-            data = new byte[]{(byte) 0b0000_0000};
+            data = new byte[]{(byte) 'S', (byte) 't', (byte) 'r', (byte) 'i' ,(byte) 'n', (byte) 'g'};
         } else {
             throw new IllegalArgumentException("Type t not supported " + type);
         }

-- 
To stop receiving notification emails like this one, please contact
"commits@plc4x.apache.org" <co...@plc4x.apache.org>.

Re: [incubator-plc4x] 02/02: Add support for Stings

Posted by Christofer Dutz <ch...@c-ware.de>.
Hi Justin,

I think in the driver itself we haven't implemented strings at all yet. It's a generally supported type, but I haven't come across being used to now. Guess we should create JIRA issues for accessing variables of all supported types.

Chris

Outlook for Android<https://aka.ms/ghei36> herunterladen

________________________________
From: Justin Mclean <ju...@classsoftware.com>
Sent: Wednesday, January 10, 2018 6:27:50 AM
To: dev@plc4x.apache.org
Subject: Re: [incubator-plc4x] 02/02: Add support for Stings

Hi,

And I just committed an implementation that uses a nul character to seperate the strings. May be a better way of doing this - feedback welcome.

Thanks,
Justin

Re: [incubator-plc4x] 02/02: Add support for Stings

Posted by Justin Mclean <ju...@classsoftware.com>.
Hi,

And I just committed an implementation that uses a nul character to seperate the strings. May be a better way of doing this - feedback welcome.

Thanks,
Justin

Re: [incubator-plc4x] 02/02: Add support for Stings

Posted by Justin Mclean <ju...@classsoftware.com>.
Hi,

Strings not stings :-) But there is an sting in the tail here in that it’s only really going to work for a single string. If multiple strings are passed I think we would need to come up with some sort of terminating character so that the byte array can be broken up into multiple strings.

We could go with the tradition “NUL” character i.e. value if 0. Any other ideas?

Thanks,
Justin