This is an automated email from the ASF dual-hosted git repository. cdutz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
commit 691f3b843582b53622df166cc374394b8cc81335 Author: Christofer Dutz <christofer.d...@c-ware.de> AuthorDate: Sun Mar 11 11:06:37 2018 +0100 - Changed the modbus driver to be a single driver with multiple sub-types --- ...usSerialPlcDriver.java => ModbusPlcDriver.java} | 28 +++++--- .../plc4x/java/modbus/ModbusTcpPlcDriver.java | 76 ---------------------- .../services/org.apache.plc4x.java.api.PlcDriver | 3 +- 3 files changed, 21 insertions(+), 86 deletions(-) diff --git a/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusSerialPlcDriver.java b/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusPlcDriver.java similarity index 70% rename from plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusSerialPlcDriver.java rename to plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusPlcDriver.java index 739ebb1..7c211be 100644 --- a/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusSerialPlcDriver.java +++ b/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusPlcDriver.java @@ -33,20 +33,19 @@ import java.util.regex.Pattern; /** * Implementation of the Modbus protocol, based on: * - Modbus Protocol (http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf) - * - TCP */ -public class ModbusSerialPlcDriver implements PlcDriver { +public class ModbusPlcDriver implements PlcDriver { - private static final Pattern MODBUS_SERIAL_URI_PATTERN = Pattern.compile("^modbus-serial://(?<port>.*)(?<params>\\?.*)?"); + private static final Pattern MODBUS_SERIAL_URI_PATTERN = Pattern.compile("^modbus:(?<subType>.*)://(?<port>.*)|(?<host>.*)(?<params>\\?.*)?"); @Override public String getProtocolCode() { - return "modbus-serial"; + return "modbus"; } @Override public String getProtocolName() { - return "Modbus (Serial)"; + return "Modbus (TCP / Serial)"; } @Override @@ -54,12 +53,25 @@ public class ModbusSerialPlcDriver implements PlcDriver { Matcher matcher = MODBUS_SERIAL_URI_PATTERN.matcher(url); if (!matcher.matches()) { throw new PlcConnectionException( - "Connection url doesn't match the format 'modbus-serial://{port}'"); + "Connection url doesn't match the format 'modbus:{type}//{port|host}'"); } - String port = matcher.group("port"); + String subType = matcher.group("subType"); String params = matcher.group("params") != null ? matcher.group("params").substring(1) : null; - return new ModbusSerialPlcConnection(port, params); + if("tcp".equalsIgnoreCase(subType)) { + String hostName = matcher.group("host"); + try { + InetAddress host = InetAddress.getByName(hostName); + return new ModbusTcpPlcConnection(host, 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); + } } @Override diff --git a/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusTcpPlcDriver.java b/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusTcpPlcDriver.java deleted file mode 100644 index 225429d..0000000 --- a/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/ModbusTcpPlcDriver.java +++ /dev/null @@ -1,76 +0,0 @@ -/* -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. -*/ -package org.apache.plc4x.java.modbus; - -import org.apache.plc4x.java.api.PlcDriver; -import org.apache.plc4x.java.api.authentication.PlcAuthentication; -import org.apache.plc4x.java.api.connection.PlcConnection; -import org.apache.plc4x.java.api.exceptions.PlcConnectionException; -import org.apache.plc4x.java.modbus.connection.ModbusSerialPlcConnection; -import org.apache.plc4x.java.modbus.connection.ModbusTcpPlcConnection; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Implementation of the Modbus protocol, based on: - * - Modbus Protocol (http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf) - * - Modbus TCP Protocol (http://www.modbus.org/docs/Modbus_Messaging_Implementation_Guide_V1_0b.pdf) - * - TCP - */ -public class ModbusTcpPlcDriver implements PlcDriver { - - private static final Pattern MODBUS_TCP_URI_PATTERN = Pattern.compile("^modbus-tcp://(?<host>.*)(?<params>\\?.*)?"); - - @Override - public String getProtocolCode() { - return "modbus-tcp"; - } - - @Override - public String getProtocolName() { - return "Modbus (TCP)"; - } - - @Override - public PlcConnection connect(String url) throws PlcConnectionException { - Matcher matcher = MODBUS_TCP_URI_PATTERN.matcher(url); - if (!matcher.matches()) { - throw new PlcConnectionException( - "Connection url doesn't match the format 'modbus-tcp://{host|ip}'"); - } - - String host = matcher.group("host"); - String params = matcher.group("params") != null ? matcher.group("params").substring(1) : null; - try { - InetAddress serverInetAddress = InetAddress.getByName(host); - return new ModbusTcpPlcConnection(serverInetAddress, params); - } catch (UnknownHostException e) { - throw new PlcConnectionException("Error parsing address", e); - } - } - - @Override - public PlcConnection connect(String url, PlcAuthentication authentication) throws PlcConnectionException { - throw new PlcConnectionException("Modbus connections don't support authentication."); - } - -} diff --git a/plc4j/protocols/modbus/src/main/resources/META-INF/services/org.apache.plc4x.java.api.PlcDriver b/plc4j/protocols/modbus/src/main/resources/META-INF/services/org.apache.plc4x.java.api.PlcDriver index a199527..8df2ee4 100644 --- a/plc4j/protocols/modbus/src/main/resources/META-INF/services/org.apache.plc4x.java.api.PlcDriver +++ b/plc4j/protocols/modbus/src/main/resources/META-INF/services/org.apache.plc4x.java.api.PlcDriver @@ -16,5 +16,4 @@ # specific language governing permissions and limitations # under the License. # -org.apache.plc4x.java.modbus.ModbusTcpPlcDriver -org.apache.plc4x.java.modbus.ModbusSerialPlcDriver +org.apache.plc4x.java.modbus.ModbusPlcDriver -- To stop receiving notification emails like this one, please contact cd...@apache.org.