]> git.basschouten.com Git - openhab-addons.git/blob
6a3230945218e7df6e19dfaa503f8550fd75d10f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.transport.modbus;
14
15 import java.util.stream.Stream;
16
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * Constants for Modbus transport
23  *
24  * == Regarding maximum read and write limits ==
25  *
26  * Maximum number of registers that are allowed to be read.
27  *
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
30  *
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
33  * specification.
34  *
35  * According to V1.1B3, maximum size for PDU is 253 bytes, making maximum ADU size 256 (RTU) or 260 (TCP).
36  *
37  * In the spec section 6, one can see maximum values for read and write counts.
38  *
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.
41  *
42  *
43  * Reads are limited by response PDU size.
44  * Writes (FC15 & FC16) are limited by write request ADU size.
45  *
46  *
47  * @author Sami Salonen - Initial contribution
48  *
49  */
50 @NonNullByDefault
51 public class ModbusConstants {
52
53     /**
54      * Value types for different number types.
55      *
56      * @author Sami Salonen - Initial contribution
57      *
58      */
59     public static enum ValueType {
60         BIT("bit", 1),
61         INT8("int8", 8),
62         UINT8("uint8", 8),
63         INT16("int16", 16),
64         UINT16("uint16", 16),
65         INT32("int32", 32),
66         UINT32("uint32", 32),
67         FLOAT32("float32", 32),
68         INT64("int64", 64),
69         UINT64("uint64", 64),
70
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);
76
77         private final String configValue;
78         private final int bits;
79
80         ValueType(String configValue, int bits) {
81             this.configValue = configValue;
82             this.bits = bits;
83         }
84
85         /**
86          * Returns number of bits represented by this ValueType
87          *
88          * @return number of bits
89          */
90         public int getBits() {
91             return bits;
92         }
93
94         /**
95          * Returns config value to refer to this value type
96          *
97          * @return config value as string
98          */
99         public String getConfigValue() {
100             return configValue;
101         }
102
103         /**
104          * Returns config value
105          */
106         @Override
107         public String toString() {
108             return getConfigValue();
109         }
110
111         /**
112          * Constructs ValueType given the config value string.
113          *
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
117          */
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));
123         }
124     }
125
126     /**
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.
129      */
130     public static final int MAX_BITS_READ_COUNT = 2000;
131     /**
132      * Maximum number of registers that are allowed to be read.
133      * Limitation by Modbus protocol V1.1B3, 6.3 definition of Read Coils.
134      */
135     public static final int MAX_REGISTERS_READ_COUNT = 125;
136     /**
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.
139      */
140     public static final int MAX_BITS_WRITE_COUNT = 1968;
141     /**
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.
144      */
145     public static final int MAX_REGISTERS_WRITE_COUNT = 123;
146 }