You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2022/08/29 12:46:57 UTC

[plc4x] 01/03: chore(plc4j/modbus): Updated the modbus discovery to accept a remote as a modbus device, even if it responds with an error code.

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 52526829e75dfedec74062f5453492de02ae9d22
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Aug 29 14:46:00 2022 +0200

    chore(plc4j/modbus): Updated the modbus discovery to accept a remote as a modbus device, even if it responds with an error code.
---
 .../java/modbus/tcp/discovery/ModbusPlcDiscoverer.java  | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/discovery/ModbusPlcDiscoverer.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/discovery/ModbusPlcDiscoverer.java
index 70ca8284f..eafa99e0f 100644
--- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/discovery/ModbusPlcDiscoverer.java
+++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/discovery/ModbusPlcDiscoverer.java
@@ -83,7 +83,7 @@ public class ModbusPlcDiscoverer implements PlcDiscoverer {
                 logger.debug("Found {} addresses: {}", inetAddresses.size(), inetAddresses);
                 possibleAddresses.addAll(inetAddresses);
             }
-        } catch (PcapNativeException e) {
+        } catch (Throwable e) {
             logger.error("Error collecting list of possible IP addresses", e);
             future.complete(new DefaultPlcDiscoveryResponse(
                 discoveryRequest, PlcResponseCode.INTERNAL_ERROR, Collections.emptyList()));
@@ -190,7 +190,19 @@ public class ModbusPlcDiscoverer implements PlcDiscoverer {
                         try {
                             ModbusTcpADU response = (ModbusTcpADU) ModbusTcpADU.staticParse(readBuffer, DriverType.MODBUS_TCP, true);
                             PlcDiscoveryItem discoveryItem;
-                            if (!response.getPdu().getErrorFlag()) {
+                            boolean found = false;
+                            // If we got a response telling us the address is unknown, we still know there's a
+                            // Modbus device at the other side. In general ... as soon as we get a valid Modbus
+                            // response, we should accept that we're talking to a Modbus device
+                            if (response.getPdu().getErrorFlag()) {
+                                ModbusPDUError errorPdu = (ModbusPDUError) response.getPdu();
+                                if (errorPdu.getExceptionCode() == ModbusErrorCode.ILLEGAL_DATA_ADDRESS) {
+                                    found = true;
+                                }
+                            } else {
+                                found = true;
+                            }
+                            if (found) {
                                 discoveryItem = new DefaultPlcDiscoveryItem(
                                     "modbus-tcp", "tcp", possibleAddress.getHostAddress(), Collections.singletonMap("unit-identifier", Integer.toString(unitIdentifier)), "unknown", Collections.emptyMap());
                                 discoveryItems.add(discoveryItem);
@@ -201,6 +213,7 @@ public class ModbusPlcDiscoverer implements PlcDiscoverer {
                                 }
                                 break;
                             }
+
                         } catch (ParseException e) {
                             // Ignore.
                         }