You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/06/28 12:02:32 UTC

[incubator-plc4x] 02/02: added possibility to supply port to modbus

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

sruehl pushed a commit to branch feature/mobbus-support-with-lib
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 6f222f06539b842120022885f9fcc15b2a67b7f6
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Jun 28 14:02:24 2018 +0200

    added possibility to supply port to modbus
---
 .../apache/plc4x/java/modbus/ModbusPlcDriver.java  |  8 ++++--
 .../modbus/connection/ModbusTcpPlcConnection.java  |  5 ++++
 .../plc4x/java/modbus/ManualPlc4XModbusTest.java   |  4 +--
 .../modbus/src/test/resources/logback.xml          | 30 +++++++++++-----------
 4 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusPlcDriver.java b/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusPlcDriver.java
index 2f13b91..0ceaf4d 100644
--- a/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusPlcDriver.java
+++ b/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusPlcDriver.java
@@ -66,12 +66,16 @@ public class ModbusPlcDriver implements PlcDriver {
             String hostName = matcher.group("host");
             try {
                 InetAddress inetAddress = InetAddress.getByName(host);
-                return new ModbusTcpPlcConnection(inetAddress, params);
+                if (port == null) {
+                    return new ModbusTcpPlcConnection(inetAddress, params);
+                } else {
+                    return new ModbusTcpPlcConnection(inetAddress, Integer.valueOf(port), params);
+                }
             } catch (UnknownHostException e) {
                 throw new PlcConnectionException("Unknown host" + hostName, e);
             }
         } else {
-            return new ModbusSerialPlcConnection(port, params);
+            return new ModbusSerialPlcConnection(serialDefinition, params);
         }
     }
 
diff --git a/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/connection/ModbusTcpPlcConnection.java b/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/connection/ModbusTcpPlcConnection.java
index 38ca1e2..d5c912d 100644
--- a/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/connection/ModbusTcpPlcConnection.java
+++ b/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/connection/ModbusTcpPlcConnection.java
@@ -44,6 +44,11 @@ public class ModbusTcpPlcConnection extends BaseModbusPlcConnection {
         logger.info("Configured ModbusTcpPlcConnection with: host-name {}", address.getHostAddress());
     }
 
+    public ModbusTcpPlcConnection(InetAddress address, int port, String params) {
+        this(new TcpSocketChannelFactory(address, port), params);
+        logger.info("Configured ModbusTcpPlcConnection with: host-name {}", address.getHostAddress());
+    }
+
     public ModbusTcpPlcConnection(ChannelFactory channelFactory, String params) {
         super(channelFactory, params);
     }
diff --git a/plc4j/protocols/modbus/src/test/java/org/apache/plc4x/java/modbus/ManualPlc4XModbusTest.java b/plc4j/protocols/modbus/src/test/java/org/apache/plc4x/java/modbus/ManualPlc4XModbusTest.java
index 5e89dcf..e320b1f 100644
--- a/plc4j/protocols/modbus/src/test/java/org/apache/plc4x/java/modbus/ManualPlc4XModbusTest.java
+++ b/plc4j/protocols/modbus/src/test/java/org/apache/plc4x/java/modbus/ManualPlc4XModbusTest.java
@@ -37,14 +37,14 @@ public class ManualPlc4XModbusTest {
             connectionUrl = "modbus:serial:///dev/ttys003";
         } else {
             System.out.println("Using tcp");
-            connectionUrl = "modbus:tcp://10.10.64.40";
+            connectionUrl = "modbus:tcp://10.10.64.10";
         }
         try (PlcConnection plcConnection = new PlcDriverManager().getConnection(connectionUrl)) {
             System.out.println("PlcConnection " + plcConnection);
 
             PlcReader reader = plcConnection.getReader().orElseThrow(() -> new RuntimeException("No Reader found"));
 
-            Address address = plcConnection.parseAddress("0/0");
+            Address address = plcConnection.parseAddress("readcoils:1/0");
             CompletableFuture<TypeSafePlcReadResponse<Integer>> response = reader
                 .read(new TypeSafePlcReadRequest<>(Integer.class, address));
             TypeSafePlcReadResponse<Integer> readResponse = response.get();
diff --git a/plc4j/protocols/modbus/src/test/resources/logback.xml b/plc4j/protocols/modbus/src/test/resources/logback.xml
index dd243bd..9f49628 100644
--- a/plc4j/protocols/modbus/src/test/resources/logback.xml
+++ b/plc4j/protocols/modbus/src/test/resources/logback.xml
@@ -1,22 +1,22 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!--
+  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
 
-  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
 
-      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.
-
--->
+  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.
+  -->
 <configuration xmlns="http://ch.qos.logback/xml/ns/logback"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="