严智鑫
2025-06-13 d14cdaf28222bfef468185e34de7c823f1436b19
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
package com.mes.connect.addressParser;
 
import com.mes.connect.IndustrialInterface.AddressParser;
import com.mes.connect.protocol.ProtocolAddress;
import com.mes.connect.protocol.ProtocolType;
 
/**
 * S7地址解析器
 */
public class S7OldAddressParser implements AddressParser {
    @Override
    public ProtocolAddress parse(String address) {
        // 格式示例: "S7.DB1.DBX10.2" 或 "S7.M100"
        //位地址:S7.DB1.DBX0.0 (DB1 块中的字节 0 的位 0)
        //字节地址:S7.DB1.DBB0 (DB1 块中的字节 0)
        //字地址:S7.DB1.DBW0 (DB1 块中的字 0)
        //双字地址:S7.DB1.DBD0 (DB1 块中的双字 0)
        if (!address.startsWith("S7.")) {
            throw new IllegalArgumentException("Invalid S7 address format: " + address);
        }
        
        String[] parts = address.substring(3).split("\\.");
        
        if (parts.length < 2) {
            throw new IllegalArgumentException("Invalid S7 address format: " + address);
        }
        
        // 解析DB号
        int dbNumber = 0;
        int area = 0x84; // 默认DB区域
        
        if (parts[0].startsWith("DB")) {
            dbNumber = Integer.parseInt(parts[0].substring(2));
        } else if (parts[0].equals("I")) {
            area = 0x81; // 输入区域
        } else if (parts[0].equals("Q")) {
            area = 0x82; // 输出区域
        } else if (parts[0].equals("M")) {
            area = 0x83; // 内存区域
        } else {
            throw new IllegalArgumentException("Invalid S7 area: " + parts[0]);
        }
        
        // 解析地址类型和地址值
        String addressPart = parts[1];
        int addressValue = 0;
        int bit = 0;
        
        if (area == 0x84) { // DB区域
            if (addressPart.startsWith("DBX")) {
                // 位地址
                String[] bitParts = addressPart.substring(3).split("\\.");
                addressValue = Integer.parseInt(bitParts[0]);
                if (bitParts.length > 1) {
                    bit = Integer.parseInt(bitParts[1]);
                }
            } else if (addressPart.startsWith("DBW")) {
                // 字地址
                addressValue = Integer.parseInt(addressPart.substring(3));
            } else if (addressPart.startsWith("DBD")) {
                // 双字地址
                addressValue = Integer.parseInt(addressPart.substring(3));
            } else {
                throw new IllegalArgumentException("Invalid S7 DB address type: " + addressPart);
            }
        } else { // I/Q/M区域
            if (addressPart.startsWith("X")) {
                // 位地址
                String[] bitParts = addressPart.substring(1).split("\\.");
                addressValue = Integer.parseInt(bitParts[0]);
                if (bitParts.length > 1) {
                    bit = Integer.parseInt(bitParts[1]);
                }
            } else {
                // 字地址
                addressValue = Integer.parseInt(addressPart);
            }
        }
        
        return new ProtocolAddress(ProtocolType.S7, area, dbNumber, addressValue, bit);
    }
}