Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Modbus plus" #352

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,14 @@ public class ${type.name} {
return new PlcStruct(_map);
<#break>
<#case "List">
<#if helper.getNonPrimitiveLanguageTypeNameForField(simpleField) == 'String'>
return new PlcSTRING(value);
<#else>
return new PlcList(value);
</#if>
<#break>
<#default>
// default ${case.name}
return new Plc${case.name}(value);
</#switch>
</#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.apache.plc4x.java.api.value.PlcValue;
import org.apache.plc4x.java.modbus.ascii.config.ModbusAsciiConfiguration;
import org.apache.plc4x.java.modbus.base.field.ModbusField;
import org.apache.plc4x.java.modbus.base.field.ModbusFieldBase;
import org.apache.plc4x.java.modbus.base.protocol.ModbusProtocolLogic;
import org.apache.plc4x.java.modbus.readwrite.*;
import org.apache.plc4x.java.modbus.rtu.config.ModbusRtuConfiguration;
import org.apache.plc4x.java.spi.configuration.HasConfiguration;
import org.apache.plc4x.java.spi.generation.ParseException;
import org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest;
Expand Down Expand Up @@ -74,7 +73,7 @@ public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
// Example for sending a request ...
if (request.getFieldNames().size() == 1) {
String fieldName = request.getFieldNames().iterator().next();
ModbusField field = (ModbusField) request.getField(fieldName);
ModbusFieldBase field = (ModbusFieldBase) request.getField(fieldName);
final ModbusPDU requestPdu = getReadRequestPdu(field);

ModbusAsciiADU modbusAsciiADU = new ModbusAsciiADU(unitIdentifier, requestPdu, false);
Expand All @@ -94,7 +93,7 @@ public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
responseCode = getErrorCode(errorResponse);
} else {
try {
plcValue = toPlcValue(requestPdu, responsePdu, field.getDataType());
plcValue = toPlcValue(requestPdu, responsePdu, field);
responseCode = PlcResponseCode.OK;
} catch (ParseException e) {
// Add an error response code ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ModbusExtendedRegister extends ModbusField {
public class ModbusExtendedRegister extends ModbusFieldBase {

public static final Pattern ADDRESS_PATTERN = Pattern.compile("extended-register:" + ModbusField.ADDRESS_PATTERN);
public static final Pattern ADDRESS_SHORTER_PATTERN = Pattern.compile("6" + ModbusField.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_SHORT_PATTERN = Pattern.compile("6x" + ModbusField.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_PATTERN = Pattern.compile("extended-register:" + ModbusFieldBase.ADDRESS_PATTERN);
public static final Pattern ADDRESS_SHORTER_PATTERN = Pattern.compile("6" + ModbusFieldBase.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_SHORT_PATTERN = Pattern.compile("6x" + ModbusFieldBase.FIXED_DIGIT_MODBUS_PATTERN);

protected static final int REGISTER_MAXADDRESS = 655359999;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,12 @@
*/
package org.apache.plc4x.java.modbus.base.field;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.modbus.readwrite.*;
import org.apache.plc4x.java.spi.generation.SerializationException;
import org.apache.plc4x.java.spi.generation.WriteBuffer;
import org.apache.plc4x.java.spi.utils.Serializable;

import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.regex.Pattern;

public abstract class ModbusField implements PlcField, Serializable {

public static final Pattern ADDRESS_PATTERN = Pattern.compile("(?<address>\\d+)(:(?<datatype>[a-zA-Z_]+))?(\\[(?<quantity>\\d+)])?");
public static final Pattern FIXED_DIGIT_MODBUS_PATTERN = Pattern.compile("(?<address>\\d{4,5})?(:(?<datatype>[a-zA-Z_]+))?(\\[(?<quantity>\\d+)])?");

protected static final int PROTOCOL_ADDRESS_OFFSET = 1;

private final int address;

private final int quantity;

private final ModbusDataType dataType;

public static ModbusField of(String addressString) {
if (ModbusFieldCoil.matches(addressString)) {
return ModbusFieldCoil.of(addressString);
Expand All @@ -62,80 +43,4 @@ public static ModbusField of(String addressString) {
throw new PlcInvalidFieldException("Unable to parse address: " + addressString);
}

protected ModbusField(int address, Integer quantity, ModbusDataType dataType) {
this.address = address;
if ((this.address + PROTOCOL_ADDRESS_OFFSET) <= 0) {
throw new IllegalArgumentException("address must be greater than zero. Was " + (this.address + PROTOCOL_ADDRESS_OFFSET));
}
this.quantity = quantity != null ? quantity : 1;
if (this.quantity <= 0) {
throw new IllegalArgumentException("quantity must be greater than zero. Was " + this.quantity);
}
this.dataType = dataType != null ? dataType : ModbusDataType.INT;
}

public int getAddress() {
return address;
}

public int getNumberOfElements() {
return quantity;
}

public int getLengthBytes() {
return quantity * dataType.getDataTypeSize();
}

@JsonIgnore
public int getLengthWords() {
return (int) ((quantity * (float) dataType.getDataTypeSize()) / 2.0f);
}

public ModbusDataType getDataType() {
return dataType;
}

@Override
public String getPlcDataType() {
return dataType.name();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ModbusField)) {
return false;
}
ModbusField that = (ModbusField) o;
return address == that.address;
}

@Override
public int hashCode() {
return Objects.hash(address);
}

@Override
public String toString() {
return "ModbusField{" +
"address=" + address +
"datatype=" + dataType +
"quantity=" + quantity +
'}';
}

@Override
public void serialize(WriteBuffer writeBuffer) throws SerializationException {
writeBuffer.pushContext(getClass().getSimpleName());

writeBuffer.writeUnsignedInt("address", 16, address);
writeBuffer.writeUnsignedInt("numberOfElements", 16, getNumberOfElements());
String dataType = getPlcDataType();
writeBuffer.writeString("dataType", dataType.getBytes(StandardCharsets.UTF_8).length * 8, StandardCharsets.UTF_8.name(), dataType);

writeBuffer.popContext(getClass().getSimpleName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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.base.field;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.plc4x.java.modbus.readwrite.*;
import org.apache.plc4x.java.spi.generation.SerializationException;
import org.apache.plc4x.java.spi.generation.WriteBuffer;

import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.regex.Pattern;

public abstract class ModbusFieldBase extends ModbusField {

public static final Pattern ADDRESS_PATTERN = Pattern.compile("(?<address>\\d+)(:(?<datatype>[a-zA-Z_]+))?(\\[(?<quantity>\\d+)])?");
public static final Pattern FIXED_DIGIT_MODBUS_PATTERN = Pattern.compile("(?<address>\\d{4,5})?(:(?<datatype>[a-zA-Z_]+))?(\\[(?<quantity>\\d+)])?");

protected static final int PROTOCOL_ADDRESS_OFFSET = 1;

private final int address;

private final int quantity;

private final ModbusDataType dataType;

protected ModbusFieldBase(int address, Integer quantity, ModbusDataType dataType) {
this.address = address;
if ((this.address + PROTOCOL_ADDRESS_OFFSET) <= 0) {
throw new IllegalArgumentException("address must be greater than zero. Was " + (this.address + PROTOCOL_ADDRESS_OFFSET));
}
this.quantity = quantity != null ? quantity : 1;
if (this.quantity <= 0) {
throw new IllegalArgumentException("quantity must be greater than zero. Was " + this.quantity);
}
this.dataType = dataType != null ? dataType : ModbusDataType.INT;
}

public int getAddress() {
return address;
}

public int getNumberOfElements() {
return quantity;
}

public int getLengthBytes() {
return quantity * dataType.getDataTypeSize();
}

@JsonIgnore
public int getLengthWords() {
return (int) ((quantity * (float) dataType.getDataTypeSize()) / 2.0f);
}

public ModbusDataType getDataType() {
return dataType;
}

@Override
public String getPlcDataType() {
return dataType.name();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ModbusFieldBase)) {
return false;
}
ModbusFieldBase that = (ModbusFieldBase) o;
return address == that.address;
}

@Override
public int hashCode() {
return Objects.hash(address);
}

@Override
public String toString() {
return "ModbusField{" +
"address=" + address +
"datatype=" + dataType +
"quantity=" + quantity +
'}';
}

@Override
public void serialize(WriteBuffer writeBuffer) throws SerializationException {
writeBuffer.pushContext(getClass().getSimpleName());

writeBuffer.writeUnsignedInt("address", 16, address);
writeBuffer.writeUnsignedInt("numberOfElements", 16, getNumberOfElements());
String dataType = getPlcDataType();
writeBuffer.writeString("dataType", dataType.getBytes(StandardCharsets.UTF_8).length * 8, StandardCharsets.UTF_8.name(), dataType);

writeBuffer.popContext(getClass().getSimpleName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ModbusFieldCoil extends ModbusField {
public class ModbusFieldCoil extends ModbusFieldBase {

public static final Pattern ADDRESS_PATTERN = Pattern.compile("coil:" + ModbusField.ADDRESS_PATTERN);
public static final Pattern ADDRESS_SHORTER_PATTERN = Pattern.compile("0" + ModbusField.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_SHORT_PATTERN = Pattern.compile("0x" + ModbusField.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_PATTERN = Pattern.compile("coil:" + ModbusFieldBase.ADDRESS_PATTERN);
public static final Pattern ADDRESS_SHORTER_PATTERN = Pattern.compile("0" + ModbusFieldBase.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_SHORT_PATTERN = Pattern.compile("0x" + ModbusFieldBase.FIXED_DIGIT_MODBUS_PATTERN);

protected static final int REGISTER_MAXADDRESS = 65535;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ModbusFieldDiscreteInput extends ModbusField {
public class ModbusFieldDiscreteInput extends ModbusFieldBase {

public static final Pattern ADDRESS_PATTERN = Pattern.compile("discrete-input:" + ModbusField.ADDRESS_PATTERN);
public static final Pattern ADDRESS_SHORTER_PATTERN = Pattern.compile("1" + ModbusField.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_SHORT_PATTERN = Pattern.compile("1x" + ModbusField.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_PATTERN = Pattern.compile("discrete-input:" + ModbusFieldBase.ADDRESS_PATTERN);
public static final Pattern ADDRESS_SHORTER_PATTERN = Pattern.compile("1" + ModbusFieldBase.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_SHORT_PATTERN = Pattern.compile("1x" + ModbusFieldBase.FIXED_DIGIT_MODBUS_PATTERN);

protected static final int REGISTER_MAX_ADDRESS = 65535;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public PlcField createField(String fieldQuery) {
return ModbusFieldCoil.of(fieldQuery);
} else if (ModbusExtendedRegister.matches(fieldQuery)) {
return ModbusExtendedRegister.of(fieldQuery);
} else if (ModbusIdentificationRegister.matches(fieldQuery)) {
return ModbusIdentificationRegister.of(fieldQuery);
}
throw new PlcInvalidFieldException(fieldQuery);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ModbusFieldHoldingRegister extends ModbusField {
public class ModbusFieldHoldingRegister extends ModbusFieldBase {

public static final Pattern ADDRESS_PATTERN = Pattern.compile("holding-register:" + ModbusField.ADDRESS_PATTERN);
public static final Pattern ADDRESS_SHORTER_PATTERN = Pattern.compile("4" + ModbusField.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_SHORT_PATTERN = Pattern.compile("4x" + ModbusField.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_PATTERN = Pattern.compile("holding-register:" + ModbusFieldBase.ADDRESS_PATTERN);
public static final Pattern ADDRESS_SHORTER_PATTERN = Pattern.compile("4" + ModbusFieldBase.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_SHORT_PATTERN = Pattern.compile("4x" + ModbusFieldBase.FIXED_DIGIT_MODBUS_PATTERN);

protected static final int REGISTER_MAXADDRESS = 65535;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ModbusFieldInputRegister extends ModbusField {
public class ModbusFieldInputRegister extends ModbusFieldBase {

public static final Pattern ADDRESS_PATTERN = Pattern.compile("input-register:" + ModbusField.ADDRESS_PATTERN);
public static final Pattern ADDRESS_SHORTER_PATTERN = Pattern.compile("3" + ModbusField.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_SHORT_PATTERN = Pattern.compile("3x" + ModbusField.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_PATTERN = Pattern.compile("input-register:" + ModbusFieldBase.ADDRESS_PATTERN);
public static final Pattern ADDRESS_SHORTER_PATTERN = Pattern.compile("3" + ModbusFieldBase.FIXED_DIGIT_MODBUS_PATTERN);
public static final Pattern ADDRESS_SHORT_PATTERN = Pattern.compile("3x" + ModbusFieldBase.FIXED_DIGIT_MODBUS_PATTERN);

protected static final int REGISTER_MAXADDRESS = 65535;

Expand Down
Loading