zhang
10 小时以前 70930071a49190f414c8d8bc9c9e9795a4096739
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.zy.acs.charge.protocol;
 
import com.ghgande.j2mod.modbus.ModbusException;
import com.ghgande.j2mod.modbus.io.ModbusTCPTransaction;
import com.ghgande.j2mod.modbus.msg.*;
import com.ghgande.j2mod.modbus.net.TCPMasterConnection;
import com.ghgande.j2mod.modbus.procimg.SimpleRegister;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
/**
 * 读取Modbus 协议工具类
 */
@Slf4j
@Component
public class ModbusAdapter {
 
    @Value("${charger.modbus.host}")
    private String host;
 
    @Value("${charger.modbus.port}")
    private int port;
 
    @Value("${charger.modbus.unit-id}")
    private int unitId;
 
    @Value("${charger.modbus.connect-timeout:3000}")
    private int connectTimeout;
 
    @Value("${charger.modbus.transaction-timeout:5000}")
    private int transactionTimeout;
 
    @Value("${charger.modbus.heartbeat.enabled:false}")
    private boolean heartbeatEnabled;
 
    @Value("${charger.modbus.heartbeat.interval:30000}")
    private long heartbeatInterval;
 
    private TCPMasterConnection connection;
    private ModbusTCPTransaction transaction;
    private volatile boolean connected = false;
    private ScheduledExecutorService heartbeatScheduler;
 
    @PostConstruct
    public void init() {
        try {
            InetAddress address = InetAddress.getByName(host);
            connection = new TCPMasterConnection(address);
            connection.setPort(port);
            connection.setTimeout(connectTimeout);
        } catch (UnknownHostException e) {
            throw new RuntimeException("Invalid Modbus host: " + host, e);
        }
    }
 
    /**
     * 建立连接
     */
    public synchronized void connect() throws Exception {
        if (connected) {
            return;
        }
        if (connection == null) {
            throw new IllegalStateException("Modbus connection not initialized");
        }
        connection.connect();
        transaction = new ModbusTCPTransaction(connection);
        connected = true;
        log.info("Modbus TCP connected to {}:{}", host, port);
 
        if (heartbeatEnabled) {
            startHeartbeat();
        }
    }
 
    /**
     * 断开连接
     */
    public synchronized void disconnect() {
        if (heartbeatScheduler != null && !heartbeatScheduler.isShutdown()) {
            heartbeatScheduler.shutdownNow();
        }
        if (connection != null && connection.isConnected()) {
            connection.close();
        }
        connected = false;
        log.info("Modbus TCP disconnected");
    }
 
    /**
     * 确保连接可用(内部自动重连)
     */
    private void ensureConnected() throws Exception {
        if (!connected) {
            connect();
        }
    }
 
    /**
     * 读取线圈(功能码01)
     */
    public boolean readCoil(int address) throws Exception {
        return executeWithRetry(() -> {
            ensureConnected();
            ReadCoilsRequest request = new ReadCoilsRequest(address, 1);
            request.setUnitID(unitId);
            transaction.setRequest(request);
            transaction.execute();
            ReadCoilsResponse response = (ReadCoilsResponse) transaction.getResponse();
            return response.getCoilStatus(0);
        });
    }
 
    /**
     * 批量读取线圈(功能码01)
     *
     * @param startAddress 起始地址
     * @param quantity     数量(1~2000)
     * @return boolean数组,索引从0开始对应起始地址的线圈
     */
    public boolean[] readCoils(int startAddress, int quantity) throws Exception {
        return executeWithRetry(() -> {
            ensureConnected();
            ReadCoilsRequest request = new ReadCoilsRequest(startAddress, quantity);
            request.setUnitID(unitId);
            transaction.setRequest(request);
            transaction.execute();
            ReadCoilsResponse response = (ReadCoilsResponse) transaction.getResponse();
            boolean[] result = new boolean[quantity];
            for (int i = 0; i < quantity; i++) {
                result[i] = response.getCoilStatus(i);
            }
            return result;
        });
    }
 
 
    /**
     * 写入单个线圈(功能码05)
     */
    public void writeCoil(int address, boolean state) throws Exception {
        executeWithRetry(() -> {
            ensureConnected();
            WriteCoilRequest request = new WriteCoilRequest(address, state);
            request.setUnitID(unitId);
            transaction.setRequest(request);
            transaction.execute();
            return null;
        });
    }
 
    /**
     * 读取保持寄存器(功能码03)
     */
    public int readHoldingRegister(int address) throws Exception {
        return executeWithRetry(() -> {
            ensureConnected();
            ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(address, 1);
            request.setUnitID(unitId);
            transaction.setRequest(request);
            transaction.execute();
            ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) transaction.getResponse();
            return response.getRegister(0).getValue();
        });
    }
 
    /**
     * 写入单个寄存器(功能码06)
     */
    public void writeHoldingRegister(int address, int value) throws Exception {
        executeWithRetry(() -> {
            ensureConnected();
            WriteSingleRegisterRequest request = new WriteSingleRegisterRequest(address, new SimpleRegister(value));
            request.setUnitID(unitId);
            transaction.setRequest(request);
            transaction.execute();
            return null;
        });
    }
 
    /**
     * 批量读取多个寄存器
     */
    public int[] readHoldingRegisters(int address, int quantity) throws Exception {
        return executeWithRetry(() -> {
            ensureConnected();
            ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(address, quantity);
            request.setUnitID(unitId);
            transaction.setRequest(request);
            transaction.execute();
            ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) transaction.getResponse();
            int[] values = new int[quantity];
            for (int i = 0; i < quantity; i++) {
                values[i] = response.getRegister(i).getValue();
            }
            return values;
        });
    }
 
    /**
     * 带重试的执行器(自动处理网络异常并重连重试)
     */
    private <T> T executeWithRetry(Operation<T> operation) throws Exception {
        Exception lastException = null;
        int maxRetries = 3;
        for (int i = 0; i < maxRetries; i++) {
            try {
                return operation.execute();
            } catch (Exception e) {
                lastException = e;
                if (isNetworkException(e)) {
                    log.warn("Modbus network error, retrying {}/{}", i + 1, maxRetries);
                    tryReconnect();
                } else {
                    // 非网络异常不重试
                    throw e;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ignored) {
                    Thread.currentThread().interrupt();
                    throw new Exception("Retry interrupted", e);
                }
            }
        }
        throw new Exception("Modbus operation failed after " + maxRetries + " retries", lastException);
    }
 
    private boolean isNetworkException(Exception e) {
        return e instanceof java.net.SocketTimeoutException ||
                e instanceof java.io.IOException ||
                (e instanceof ModbusException && e.getMessage().toLowerCase().contains("timeout"));
    }
 
    private synchronized void tryReconnect() {
        try {
            disconnect();
            connect();
        } catch (Exception e) {
            log.error("Reconnect failed", e);
        }
    }
 
    /**
     * 心跳保活
     */
    private void startHeartbeat() {
        heartbeatScheduler = Executors.newSingleThreadScheduledExecutor();
        heartbeatScheduler.scheduleAtFixedRate(() -> {
            try {
                // 读取一个无副作用的寄存器,如充电机编号(地址113)
                int chargerId = readHoldingRegister(113);
                log.debug("Heartbeat success, chargerId={}", chargerId);
            } catch (Exception e) {
                log.warn("Heartbeat failed", e);
                tryReconnect();
            }
        }, heartbeatInterval, heartbeatInterval, TimeUnit.MILLISECONDS);
    }
 
    @PreDestroy
    public void destroy() {
        disconnect();
    }
 
    @FunctionalInterface
    private interface Operation<T> {
        T execute() throws Exception;
    }
}