huang
7 天以前 9571229a2013472dc701ecf5767f2873b36d8f90
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package com.mes.connect;
 
import com.mes.connect.industrialinterface.IndustrialDataHandler;
import com.mes.connect.addressparser.ModbusAddressParser;
import com.mes.connect.protocol.ProtocolAddress;
 
/**
 * ExampleDataHandler
 *
 * @author yzx
 * @version 1.0
 */
public class ExampleDataHandler implements IndustrialDataHandler {
    /**
     * 线圈
     */
    private final boolean[] coils = new boolean[1000];
    /**
     * 离散型
     */
    private final boolean[] discreteInputs = new boolean[1000];
    /**
     * 存储寄存器内容
     */
    private final int[] holdingRegisters = new int[1000];
    /**
     * 输入
     */
    private final int[] inputRegisters = new int[1000];
 
    /**
     * S7协议
     */
    private final String protocolS7="S7";
    /**
     * S7协议
     */
    private final String protocolS7_="S7.";
    /**
     * S7协议
     */
    /**
     * 地址类型
     */
    private final String addressTypeDB="DB";
 
    /**
     * 地址值类型
     */
    private final String valueTypeDBW="DBW";
 
    /**
     * 地址值类型
     */
    private final String valueTypeDBD="DBD";
    /**
     * 线圈
     */
    private final int functionCode1=1;
    /**
     * 离散输入
     */
    private final int functionCode2=2;
    /**
     * 离散输入
     */
    private final int functionCode3=3;
    /**
     * 离散输入
     */
    private final int functionCode4=4;
    /**
     * 离散输入
     */
    private final int functionCode5=5;
    /**
     * 读写功能码
     */
    private final int functionCode16=16;
 
    @Override
    public boolean handleReadBit(String address) {
        ProtocolAddress parsedAddress = new ModbusAddressParser().parse(address);
        if (parsedAddress.getFunctionCode() == functionCode1) {
            int index = parsedAddress.getAddress();
            return coils[index];
        } else if (parsedAddress.getFunctionCode() == functionCode2) {
            int index = parsedAddress.getAddress();
            return discreteInputs[index];
        }
        return false;
    }
 
    @Override
    public void handleWriteBit(String address, boolean value) {
        ProtocolAddress parsedAddress = new ModbusAddressParser().parse(address);
        if (parsedAddress.getFunctionCode() == 1) {
            // 线圈
            int index = parsedAddress.getAddress();
            coils[index] = value;
            System.out.println("写入线圈 " + address + " 值: " + value);
        }
    }
 
    @Override
    public int handleReadRegister(String address) {
        ProtocolAddress parsedAddress = new ModbusAddressParser().parse(address);
        if (parsedAddress.getFunctionCode() == functionCode3) {
            // 保持寄存器
            int index = parsedAddress.getAddress();
            return holdingRegisters[index];
        } else if (parsedAddress.getFunctionCode() == functionCode4) {
            // 输入寄存器
            int index = parsedAddress.getAddress();
            return inputRegisters[index];
        }
        return 0;
    }
 
    @Override
    public void handleWriteRegister(String address, int value) {
        ProtocolAddress parsedAddress = new ModbusAddressParser().parse(address);
        if (parsedAddress.getFunctionCode() == functionCode3) {
            // 保持寄存器
            int index = parsedAddress.getAddress();
            holdingRegisters[index] = value;
            System.out.println("写入寄存器 " + address + " 值: " + value);
        }
    }
 
    @Override
    public void handleWriteRegisters(String address, int[] values) {
        ProtocolAddress parsedAddress = new ModbusAddressParser().parse(address);
        if (parsedAddress.getFunctionCode() == functionCode16) {
            // 写多个寄存器
            int index = parsedAddress.getAddress();
            System.arraycopy(values, 0, holdingRegisters, index, values.length);
            System.out.println("写入寄存器 " + address + " 数量: " + values.length);
        }
    }
 
    @Override
    public int[] handleReadRegisters(String address, int quantity) {
        ProtocolAddress parsedAddress = new ModbusAddressParser().parse(address);
        if (parsedAddress.getFunctionCode() == functionCode3) {
            // 保持寄存器
            int startIndex = parsedAddress.getAddress();
            int[] result = new int[quantity];
            System.arraycopy(holdingRegisters, startIndex, result, 0, Math.min(quantity, holdingRegisters.length - startIndex));
            return result;
        } else if (parsedAddress.getFunctionCode() == functionCode4) {
            // 输入寄存器
            int startIndex = parsedAddress.getAddress();
            int[] result = new int[quantity];
            System.arraycopy(inputRegisters, startIndex, result, 0, Math.min(quantity, inputRegisters.length - startIndex));
            return result;
        } else if (address.startsWith(protocolS7_)) {
            return handleS7ReadRegisters(address, quantity);
        }
        return new int[0];
    }
 
    private int[] handleS7ReadRegisters(String address, int quantity) {
        // 解析S7地址格式并读取多个寄存器
        String[] parts = address.split("\\.");
        if (parts.length >= functionCode3 && protocolS7.equals(parts[0]) && parts[1].startsWith(addressTypeDB)) {
            try {
                int dbNumber = Integer.parseInt(parts[1].substring(2));
                String type = parts[2].substring(0, 3);
                int startIndex = Integer.parseInt(parts[2].substring(3));
 
                int baseIndex = dbNumber * 1000 + startIndex;
                int[] result = new int[quantity];
 
                if (valueTypeDBW.equals(type)) {
                    // 字(16位)数组
                    for (int i = 0; i < quantity && baseIndex + i < holdingRegisters.length; i++) {
                        result[i] = holdingRegisters[baseIndex + i];
                    }
                } else if (valueTypeDBD.equals(type)) {
                    // 双字(32位)数组,每个双字占两个寄存器位置
                    for (int i = 0; i < quantity && baseIndex + i * 2 + 1 < holdingRegisters.length; i++) {
                        result[i] = (holdingRegisters[baseIndex + i * 2] << 16) |
                                (holdingRegisters[baseIndex + i * 2 + 1] & 0xFFFF);
                    }
                }
 
                return result;
            } catch (NumberFormatException e) {
                System.err.println("地址解析错误: " + address);
            }
        }
        return new int[0];
    }
 
    @Override
    public float handleReadFloat(String address) {
        int[] registers = handleReadRegisters(address, 2);
        if (registers.length >= functionCode2) {
            int intBits = (registers[0] << 16) | registers[1];
            return Float.intBitsToFloat(intBits);
        }
        return 0.0f;
    }
 
    @Override
    public void handleWriteFloat(String address, float value) {
        int intBits = Float.floatToIntBits(value);
        int highWord = (intBits >> 16) & 0xFFFF;
        int lowWord = intBits & 0xFFFF;
        handleWriteRegisters(address, new int[]{highWord, lowWord});
    }
 
    @Override
    public String handleReadString(String address, int length) {
        int[] registers = handleReadRegisters(address, (length + 1) / 2);
        byte[] bytes = new byte[registers.length * 2];
 
        for (int i = 0; i < registers.length; i++) {
            bytes[i * 2] = (byte) ((registers[i] >> 8) & 0xFF);
            bytes[i * 2 + 1] = (byte) (registers[i] & 0xFF);
        }
 
        return new String(bytes, 0, length);
    }
 
    @Override
    public void handleWriteString(String address, String value) {
        byte[] bytes = value.getBytes();
        int[] registers = new int[(bytes.length + 1) / 2];
 
        for (int i = 0; i < bytes.length; i++) {
            int regIndex = i / 2;
            int byteIndex = i % 2;
 
            if (byteIndex == 0) {
                registers[regIndex] = (bytes[i] & 0xFF) << 8;
            } else {
                registers[regIndex] |= (bytes[i] & 0xFF);
            }
        }
 
        handleWriteRegisters(address, registers);
    }
}