huang
2025-10-30 a99650cb00bf5b0650c33f39a4221b765201d228
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
package com.mes.connect.addressparser;
 
import com.mes.connect.industrialinterface.AddressParser;
import com.mes.connect.protocol.ProtocolAddress;
import com.mes.connect.protocol.ProtocolType;
 
 
/**
 * Modbus地址解析器
 *
 * @author yzx
 * @version 1.0
 */
public class ModbusAddressParser implements AddressParser {
 
    /**
     * modbusTcp协议
     */
    private final String protocolMB="MB";
    /**
     * 字符串截取
     */
    private final String protocolMB_="MB.";
    /**
     * 地址类型
     */
    private final String addressTypeDB="DB";
 
    /**
     * 地址值类型
     */
    private final String valueTypeDBW="DBW";
 
    /**
     * 地址值类型
     */
    private final String valueTypeDBD="DBD";
    /**
     * 线圈
     */
    private final String functionCode0x="0x";
    /**
     * 离散输入
     */
    private final String functionCode1x="1x";
    /**
     * 离散输入
     */
    private final String functionCode3x="3x";
    /**
     * 离散输入
     */
    private final String functionCode4x="4x";
    /**
     * 离散输入
     */
    private final int functionCode3=3;
    /**
     * 读写功能码
     */
    private final int functionCode4=4;
 
 
    /**
     * @param address
     * @return
     */
    @Override
    public ProtocolAddress parse(String address) {
        // 格式示例: "MB.4x0001" 或 "MB.0x100.2"
        if (!address.startsWith(protocolMB)) {
            throw new IllegalArgumentException("Invalid Modbus address format: " + address);
        }
 
        String[] parts = address.substring(3).split("\\.");
 
        if (parts.length < 1) {
            throw new IllegalArgumentException("Invalid Modbus address format: " + address);
        }
 
        // 解析区域
        String areaStr = parts[0];
        int functionCode;
 
        if (areaStr.startsWith(functionCode0x)) {
            // 1读线圈 5写单个线圈
            functionCode = 1;
        } else if (areaStr.startsWith(functionCode1x)) {
            // 读离散输入
            functionCode = 2;
        } else if (areaStr.startsWith(functionCode3x)) {
            // 读输入寄存器
            functionCode = 4;
        } else if (areaStr.startsWith(functionCode4x)) {
            // 读保持寄存器
            functionCode = 3;
        } else {
            throw new IllegalArgumentException("Invalid Modbus area: " + areaStr);
        }
 
        // 解析地址
        int addressValue = Integer.parseInt(areaStr.substring(2));
        if (functionCode == functionCode3 || functionCode == functionCode4) {
            addressValue--; // Modbus保持寄存器和输入寄存器地址从0开始
        }
 
        // 解析位地址(如果有)
        int bit = 0;
        if (parts.length > 1) {
            bit = Integer.parseInt(parts[1]);
        }
 
        return new ProtocolAddress(ProtocolType.MODBUS_TCP, functionCode, 0, addressValue, bit);
    }
}