whyclj
2021-01-05 64aba412c2b739d67795b14a3cae069d311697f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.intelligt.modbus.jlibmodbus.utils;
 
/*
 * Copyright (C) 2016 "Invertor" Factory", JSC
 * [http://www.sbp-invertor.ru]
 *
 * This file is part of JLibModbus.
 *
 * Licensed 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.
 *
 * Authors: Vladislav Y. Kochedykov, software engineer.
 * email: vladislav.kochedykov@gmail.com
 */
 
/*
the list of available modbus functions
 
01 (0x01) Read Coils
02 (0x02) Read Discrete Inputs
03 (0x03) Read Holding Registers
04 (0x04) Read Input Registers
05 (0x05) Write Single Coil
06 (0x06) Write Single Register
07 (0x07) Read Exception Status (Serial Line only)
08 (0x08) Diagnostics (Serial Line only)
11 (0x0B) Get Comm Event Counter (Serial Line only)
12 (0x0C) Get Comm Event Log (Serial Line only)
15 (0x0F) Write Multiple Coils
16 (0x10) Write Multiple registers
17 (0x11) Report Slave ID (Serial Line only)
20 (0x14) Read File Record
21 (0x15) Write File Record
22 (0x16) Mask Write Register
23 (0x17) Read/Write Multiple registers
24 (0x18) Read FIFO Queue
43 ( 0x2B) Encapsulated Interface Transport
43 / 13 (0x2B / 0x0D) CAN open General Reference Request and Response PDU
43 / 14 (0x2B / 0x0E) Read Device Identification
*/
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * a enum containing all modbus-function-codes listed in the modbus protocol v1.1b:
 * <p>
 * 01 (0x01) Read Coils
 * 02 (0x02) Read Discrete Inputs
 * 03 (0x03) Read Holding Registers
 * 04 (0x04) Read Input Registers
 * 05 (0x05) Write Single Coil
 * 06 (0x06) Write Single Register
 * 07 (0x07) Read Exception Status (Serial Line only)
 * 08 (0x08) Diagnostics (Serial Line only)
 * 11 (0x0B) Get Comm Event Counter (Serial Line only)
 * 12 (0x0C) Get Comm Event Log (Serial Line only)
 * 15 (0x0F) Write Multiple Coils
 * 16 (0x10) Write Multiple registers
 * 17 (0x11) Report Slave ID (Serial Line only)
 * 20 (0x14) Read File Record
 * 21 (0x15) Write File Record
 * 22 (0x16) Mask Write Register
 * 23 (0x17) Read/Write Multiple registers
 * 24 (0x18) Read FIFO Queue
 * 43 ( 0x2B) Encapsulated Interface Transport
 * 43 / 13 (0x2B / 0x0D) CAN open General Reference Request and Response PDU
 * 43 / 14 (0x2B / 0x0E) Read Device Identification
 */
public enum ModbusFunctionCode {
    READ_COILS(0x1),
    READ_DISCRETE_INPUTS(0x2),
    READ_HOLDING_REGISTERS(0x3),
    READ_INPUT_REGISTERS(0x4),
    WRITE_SINGLE_COIL(0x5),
    WRITE_SINGLE_REGISTER(0x6),
    READ_EXCEPTION_STATUS(0x7),//serial line only
    DIAGNOSTICS(0x8),//serial line only
    GET_COMM_EVENT_COUNTER(0x0B),//serial line only
    GET_COMM_EVENT_LOG(0x0C),//serial line only
    WRITE_MULTIPLE_COILS(0x0F),
    WRITE_MULTIPLE_REGISTERS(0x10),
    REPORT_SLAVE_ID(0x11),//serial line only
    READ_FILE_RECORD(0x14),
    WRITE_FILE_RECORD(0x15),
    MASK_WRITE_REGISTER(0x16),
    READ_WRITE_MULTIPLE_REGISTERS(0x17),
    READ_FIFO_QUEUE(0x18),
    ENCAPSULATED_INTERFACE_TRANSPORT(0x2B),
    /**
     * this function code is not presented in the modbus protocol specification.
     * <code>ModbusFunctionCode.get(int value)</code> returns <code>UNKNOWN</code>
     * if <code>value</code> is not listed in <code>ModbusFunctionCode</code>.
     */
    UNKNOWN(0xff);
 
    final static private int MODBUS_EXCEPTION_FLAG = 0x80;
    /**
     * create the map containing all values of this enum for the optimization purpose.
     */
    final static private Map<Integer, ModbusFunctionCode> values = new HashMap<Integer, ModbusFunctionCode>(ModbusFunctionCode.values().length);
 
    static {
        for (ModbusFunctionCode func : ModbusFunctionCode.values()) {
            values.put(func.value, func);
        }
    }
 
    final private int value;
 
    ModbusFunctionCode(int value) {
        this.value = value;
    }
 
    static public ModbusFunctionCode get(int value) {
        if (isException(value)) {
            value &= ~MODBUS_EXCEPTION_FLAG;
        }
 
        if (values.containsKey(value)) {
            return values.get(value);
        }
        return UNKNOWN;
    }
 
    static public boolean isException(int value) {
        return (value & MODBUS_EXCEPTION_FLAG) != 0;
    }
 
    static public int getExceptionValue(int functionCode) {
        return functionCode | MODBUS_EXCEPTION_FLAG;
    }
 
    public int toInt() {
        return value;
    }
 
    @Override
    public String toString() {
        return String.valueOf(value);
    }
}