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

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 63525f6efc8e56ef9815cdfb150dc780cdf3e6cc
Author: Christofer Dutz <cd...@apache.org>
AuthorDate: Sun Jun 30 14:34:34 2024 +0200

    feat: Made the S7 driver handle subscription errors a bit better.
---
 .../s7/readwrite/protocol/S7ProtocolLogic.java     | 32 +++++++++++++++++-----
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git 
a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java
 
b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java
index 756c065b13..c2aee3e10f 100644
--- 
a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java
+++ 
b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java
@@ -618,12 +618,19 @@ public class S7ProtocolLogic extends 
Plc4xProtocolBase<TPKTPacket> {
         short errorClass = 0;
         short errorCode = 0;
         if (responseMessage instanceof S7MessageUserData) {
-            // TODO: Payload and messageUserData are ignored?
-            //S7MessageUserData messageUserData = (S7MessageUserData) 
responseMessage;
+            S7MessageUserData messageUserData = (S7MessageUserData) 
responseMessage;
+            S7Parameter s7Parameter = messageUserData.getParameter();
+            if(s7Parameter instanceof S7ParameterUserData) {
+                S7ParameterUserData s7ParameterUserData = 
(S7ParameterUserData) s7Parameter;
+                S7ParameterUserDataItem s7ParameterUserDataItem = 
s7ParameterUserData.getItems().get(0);
+                if(s7ParameterUserDataItem instanceof 
S7ParameterUserDataItemCPUFunctions) {
+                    S7ParameterUserDataItemCPUFunctions 
s7ParameterUserDataItemCPUFunctions = (S7ParameterUserDataItemCPUFunctions) 
s7ParameterUserDataItem;
+                    errorCode = 
s7ParameterUserDataItemCPUFunctions.getErrorCode().shortValue();
+                }
+            }
             //S7PayloadUserData payload = (S7PayloadUserData) 
messageUserData.getPayload();
-            // errorClass = payload.getItems()[0].
-            // errorCode = messageUserData.getParameter().
-
+            //errorClass = payload.getItems()[0]
+            //errorCode = messageUserData.getParameter().
         } else if (responseMessage instanceof S7MessageResponse) {
             S7MessageResponse messageResponse = (S7MessageResponse) 
responseMessage;
             errorClass = messageResponse.getErrorClass();
@@ -639,7 +646,18 @@ public class S7ProtocolLogic extends 
Plc4xProtocolBase<TPKTPacket> {
                 logger.warn("Got an error response from the PLC. This 
particular response code usually indicates " +
                     "that PUT/GET is not enabled on the PLC.");
                 for (String tagName : plcSubscriptionRequest.getTagNames()) {
-                    values.put(tagName, null);
+                    values.put(tagName, new 
ResponseItem<>(PlcResponseCode.REMOTE_ERROR, null));
+                }
+                return new 
DefaultPlcSubscriptionResponse(plcSubscriptionRequest, values);
+            }
+            // This seems to be the case if we're trying to do a subscription 
on a device that doesn't support that.
+            else if((errorClass == 0) && (errorCode == (short) 0x8104)) {
+                logger.warn("Got an error response from the PLC. This 
particular response code usually indicates " +
+                    "that a given service is not implemented on the PLC. Most 
probably you tried to subscribe to " +
+                    "data on a PLC that doesn't support subsciptions (S7-1200 
or S7-1500)",
+                    errorClass, errorCode);
+                for (String tagName : plcSubscriptionRequest.getTagNames()) {
+                    values.put(tagName, new 
ResponseItem<>(PlcResponseCode.UNSUPPORTED, null));
                 }
                 return new 
DefaultPlcSubscriptionResponse(plcSubscriptionRequest, values);
             } else {
@@ -649,7 +667,7 @@ public class S7ProtocolLogic extends 
Plc4xProtocolBase<TPKTPacket> {
                         "containing a capture of the communication.",
                     errorClass, errorCode);
                 for (String tagName : plcSubscriptionRequest.getTagNames()) {
-                    values.put(tagName, null);
+                    values.put(tagName, new 
ResponseItem<>(PlcResponseCode.INTERNAL_ERROR, null));
                 }
                 return new 
DefaultPlcSubscriptionResponse(plcSubscriptionRequest, values);
             }

Reply via email to