2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.transport.modbus;
15 import java.util.stream.Stream;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
22 * Constants for Modbus transport
24 * == Regarding maximum read and write limits ==
26 * Maximum number of registers that are allowed to be read.
28 * The Modbus protocol has many intepretation on maximum data size of messages. Good reference is here:
29 * https://wingpath.co.uk/manpage.php?product=modtest&page=message_limits.html
31 * We try to follow modern specification here (V1.1B3):
32 * https://modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf. See section 4.1 Protocol Specification in the
35 * According to V1.1B3, maximum size for PDU is 253 bytes, making maximum ADU size 256 (RTU) or 260 (TCP).
37 * In the spec section 6, one can see maximum values for read and write counts.
39 * Note that this is not the only interpretation -- some sources limit the ADU to 256 also with TCP.
40 * In some cases, slaves cannot take in so much data.
43 * Reads are limited by response PDU size.
44 * Writes (FC15 & FC16) are limited by write request ADU size.
47 * @author Sami Salonen - Initial contribution
51 public class ModbusConstants {
54 * Value types for different number types.
56 * @author Sami Salonen - Initial contribution
59 public static enum ValueType {
67 FLOAT32("float32", 32),
71 INT32_SWAP("int32_swap", 32),
72 UINT32_SWAP("uint32_swap", 32),
73 FLOAT32_SWAP("float32_swap", 32),
74 INT64_SWAP("int64_swap", 64),
75 UINT64_SWAP("uint64_swap", 64);
77 private final String configValue;
78 private final int bits;
80 ValueType(String configValue, int bits) {
81 this.configValue = configValue;
86 * Returns number of bits represented by this ValueType
88 * @return number of bits
90 public int getBits() {
95 * Returns config value to refer to this value type
97 * @return config value as string
99 public String getConfigValue() {
104 * Returns config value
107 public String toString() {
108 return getConfigValue();
112 * Constructs ValueType given the config value string.
114 * @param configValueType config value that will be parsed to ValueType
115 * @return ValueType matching the config value
116 * @throws IllegalArgumentException with unknown value types
118 @SuppressWarnings("null")
119 public static @NonNull ValueType fromConfigValue(@Nullable String configValueType)
120 throws IllegalArgumentException {
121 return Stream.of(ValueType.values()).filter(v -> v.getConfigValue().equals(configValueType)).findFirst()
122 .orElseThrow(() -> new IllegalArgumentException("Invalid valueType " + configValueType));
127 * Maximum number of coils or discrete inputs that are allowed to be read.
128 * Limitation by Modbus protocol V1.1B3, 6.1 definition of Read Holding registers.
130 public static final int MAX_BITS_READ_COUNT = 2000;
132 * Maximum number of registers that are allowed to be read.
133 * Limitation by Modbus protocol V1.1B3, 6.3 definition of Read Coils.
135 public static final int MAX_REGISTERS_READ_COUNT = 125;
137 * Maximum number of coils or discrete inputs that are allowed to be written.
138 * Limitation by Modbus protocol V1.1B3, 6.11 definition of Write Multiple coils.
140 public static final int MAX_BITS_WRITE_COUNT = 1968;
142 * Maximum number of registers that are allowed to be written.
143 * Limitation by Modbus protocol V1.1B3, 6.12 definition of Write Multiple registers.
145 public static final int MAX_REGISTERS_WRITE_COUNT = 123;