leutu
2024-06-03 3ef35e6cd16bbfa206b26bb3271eac40ad020bcb
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
package com.fastbee.common.core.protocol.modbus;
 
import com.fastbee.common.exception.ServiceException;
import lombok.AllArgsConstructor;
import lombok.Getter;
 
/**
 * Modbus功能码
 * @author bill
 *
 * {bit 位操作}
 * 线圈寄存器:   bit对应一个信号的开关状态。功能码里面又分为写单个线圈寄存器和写多个线圈寄存器。对应上面的功能码也就是:0x01  0x05  0x0f
 * 离散输入寄存器:离散输入寄存器就 是 只读线圈寄存器,每个bit表示一个开关量,是不能够写的。 功能码: 0x02
 *
 * {byte 字节操作}
 * 保持寄存器:  两个byte,可读写的 写也分为单个写和多个写对应的三个:0x03 0x06 0x10
 * 输入寄存器:  和保持寄存器类似,只支持读而不能写,一般是读取各种实时数据。一个寄存器也是占据两个byte的空间。对应的功能码: 0x04
 *
 */
@Getter
@AllArgsConstructor
public enum ModbusCode {
 
    Read01("读线圈",(byte) 0x01), // 读线圈(读写位模式)
    Read02("读离散量输入",(byte) 0x02), // 读离散量输入(位只读模式)
    Read03("读保持寄存器",(byte) 0x03), // 读保持寄存器(字节读写模式)
    Read04("读输入寄存器",(byte) 0x04), // 读输入寄存器(字节只读模式)
 
    Write05("写单个线圈(读写位模式)",(byte) 0x05), // 写单个线圈(读写位模式)
    Write06("写多个线圈",(byte) 0x06), // 写单个保持寄存器
    Write0F("写多个线圈",(byte) 0x0F), // 写多个线圈
    Write10("写多个保持寄存器",(byte) 0x10) // 写多个保持寄存器
    ;
 
    private String desc;
    private byte code;
 
    public static ModbusCode getInstance(int code) {
        switch ((byte)code) {
            case 0x01:
                return Read01;
            case 0x02:
                return Read02;
            case 0x03:
                return Read03;
            case 0x04:
                return Read04;
 
            case 0x05:
                return Write05;
            case 0x06:
                return Write06;
            case 0x0F:
                return Write0F;
            case 0x10:
                return Write10;
 
            default:
                throw new ServiceException("功能码[" + code + "],未定义");
        }
    }
 
    public static String getDes(int code){
        switch ((byte)code) {
            case 0x01:
                return Read01.desc;
            case 0x02:
                return Read02.desc;
            case 0x03:
                return Read03.desc;
            case 0x04:
                return Read04.desc;
            case 0x05:
                return Write05.desc;
            case 0x06:
                return Write06.desc;
            case 0x0F:
                return Write0F.desc;
            case 0x10:
                return Write10.desc;
 
            default:
                return "UNKOWN";
        }
    }
 
}