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 10:34:58 UTC

[incubator-plc4x] branch feature/mobbus-support-with-lib updated: MODBUS: fixed patterns and aligned them on ADS

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


The following commit(s) were added to refs/heads/feature/mobbus-support-with-lib by this push:
     new 295a5e9  MODBUS: fixed patterns and aligned them on ADS
295a5e9 is described below

commit 295a5e92ced4827a7509632be7e9ecc6bbd2467c
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Jun 28 12:34:54 2018 +0200

    MODBUS: fixed patterns and aligned them on ADS
---
 .../apache/plc4x/java/modbus/ModbusPlcDriver.java  | 21 ++++++------
 .../modbus/src/test/resources/logback.xml          | 38 ++++++++++++++++++++++
 2 files changed, 49 insertions(+), 10 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 7c211be..2f13b91 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
@@ -36,7 +36,9 @@ import java.util.regex.Pattern;
  */
 public class ModbusPlcDriver implements PlcDriver {
 
-    private static final Pattern MODBUS_SERIAL_URI_PATTERN = Pattern.compile("^modbus:(?<subType>.*)://(?<port>.*)|(?<host>.*)(?<params>\\?.*)?");
+    public static final Pattern INET_ADDRESS_PATTERN = Pattern.compile("tcp://(?<host>[\\w.]+)(:(?<port>\\d*))?");
+    public static final Pattern SERIAL_PATTERN = Pattern.compile("serial://(?<serialDefinition>((?!/\\d).)*)");
+    public static final Pattern MODBUS_URI_PATTERN = Pattern.compile("^modbus:(" + INET_ADDRESS_PATTERN + "|" + SERIAL_PATTERN + ")/?" + "(?<params>\\?.*)?");
 
     @Override
     public String getProtocolCode() {
@@ -50,27 +52,26 @@ public class ModbusPlcDriver implements PlcDriver {
 
     @Override
     public PlcConnection connect(String url) throws PlcConnectionException {
-        Matcher matcher = MODBUS_SERIAL_URI_PATTERN.matcher(url);
+        Matcher matcher = MODBUS_URI_PATTERN.matcher(url);
         if (!matcher.matches()) {
             throw new PlcConnectionException(
                 "Connection url doesn't match the format 'modbus:{type}//{port|host}'");
         }
 
-        String subType = matcher.group("subType");
+        String host = matcher.group("host");
+        String serialDefinition = matcher.group("serialDefinition");
+        String port = matcher.group("port");
         String params = matcher.group("params") != null ? matcher.group("params").substring(1) : null;
-        if("tcp".equalsIgnoreCase(subType)) {
+        if (serialDefinition == null) {
             String hostName = matcher.group("host");
             try {
-                InetAddress host = InetAddress.getByName(hostName);
-                return new ModbusTcpPlcConnection(host, params);
+                InetAddress inetAddress = InetAddress.getByName(host);
+                return new ModbusTcpPlcConnection(inetAddress, params);
             } catch (UnknownHostException e) {
                 throw new PlcConnectionException("Unknown host" + hostName, e);
             }
-        } else if("serial".equalsIgnoreCase(subType)) {
-            String port = matcher.group("port");
-            return new ModbusSerialPlcConnection(port, params);
         } else {
-            throw new PlcConnectionException("Unknown sub-type " + subType);
+            return new ModbusSerialPlcConnection(port, params);
         }
     }
 
diff --git a/plc4j/protocols/modbus/src/test/resources/logback.xml b/plc4j/protocols/modbus/src/test/resources/logback.xml
new file mode 100644
index 0000000..dd243bd
--- /dev/null
+++ b/plc4j/protocols/modbus/src/test/resources/logback.xml
@@ -0,0 +1,38 @@
+<?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
+
+      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.
+
+-->
+<configuration xmlns="http://ch.qos.logback/xml/ns/logback"
+               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="
+                  http://ch.qos.logback/xml/ns/logback
+                  https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd">
+
+  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+    <!-- encoders are assigned the type
+         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
+    <encoder>
+      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+    </encoder>
+  </appender>
+
+  <root level="info">
+    <appender-ref ref="STDOUT" />
+  </root>
+
+</configuration>
\ No newline at end of file