jiazx0107@163.com
2023-08-25 73f74cbf665a431b0db474be129a03c655e2338a
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
package com.ld.igds.protocol.modbus;
 
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.serotonin.modbus4j.locator.BaseLocator;
import com.serotonin.modbus4j.msg.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
 
/**
 * @Desc: 工具类
 * @author: Andy
 * @update-time: 2023/8/11
 */
@Slf4j
public class ModbusUtil2 {
 
    //从机默认值
    private static Integer slaveId = 1;
 
    private static HashMap<String, ModbusMaster> masterMap = new HashMap<>();
 
    /**
     * 工厂
     */
    static ModbusFactory modbusFactory;
    //static ModbusMaster modbusMaster;
 
    static {
        if (modbusFactory == null) {
            modbusFactory = new ModbusFactory();
        }
    }
 
 
    /**
     * 获取master
     *
     * @return
     */
    public static ModbusMaster getMaster(String ip, int port) throws ModbusInitException {
        String key = ip;
        ModbusMaster modbusMaster = masterMap.get(key);
        if (modbusMaster == null || !modbusMaster.isConnected()) {
            IpParameters ipParameters = new IpParameters();
            ipParameters.setHost(ip);
            ipParameters.setPort(port);
            modbusMaster = modbusFactory.createTcpMaster(ipParameters, true);
            modbusMaster.init();
            masterMap.put(key, modbusMaster);
            return modbusMaster;
        }
        return modbusMaster;
    }
 
 
    /**
     * 读取线圈开关状态数据  0x01
     *
     * @param offset
     * @return
     * @throws ModbusInitException
     * @throws ModbusTransportException
     * @throws ErrorResponseException
     */
    public static Boolean readCoilStatus(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException {
        BaseLocator<Boolean> coilStatus = BaseLocator.coilStatus(slaveId, offset);
        Boolean res = getMaster(ip, port).getValue(coilStatus);
        return res;
    }
 
    /**
     * 读离散输入寄存器状态数据  0x02
     *
     * @param offset
     * @return
     * @throws ModbusInitException
     * @throws ModbusTransportException
     * @throws ErrorResponseException
     */
    public static Boolean readInputStatus(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException {
        BaseLocator<Boolean> inputStatus = BaseLocator.inputStatus(slaveId, offset);
        Boolean res = getMaster(ip, port).getValue(inputStatus);
        return res;
    }
 
    /**
     * 读保持寄存器数据  0x03
     *
     * @param offset
     * @param dataType
     * @return
     * @throws ModbusInitException
     * @throws ModbusTransportException
     * @throws ErrorResponseException
     */
    public static Number readHoldingRegister(String ip, int port, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException {
        BaseLocator<Number> holdingRegister = BaseLocator.holdingRegister(slaveId, offset, dataType);
        Number value = getMaster(ip, port).getValue(holdingRegister);
        return value;
    }
 
    /**
     * 读输入寄存器数据   0x04
     *
     * @param offset
     * @param dataType
     * @return
     * @throws ModbusInitException
     * @throws ModbusTransportException
     * @throws ErrorResponseException
     */
    public static Number readInputRegister(String ip, int port, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException {
        BaseLocator<Number> inputRegister = BaseLocator.inputRegister(slaveId, offset, dataType);
        Number value = getMaster(ip, port).getValue(inputRegister);
        return value;
    }
 
 
    /**
     * 写线圈开关状态数据  0x05
     *
     * @param offset
     * @param status
     * @return
     * @throws ModbusTransportException
     * @throws ModbusInitException
     */
    public static Boolean writeCoilStatus(String ip, int port, int offset, boolean status) throws ModbusTransportException, ModbusInitException {
        boolean coilValue = status;
        WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, coilValue);
        WriteCoilResponse coilResponse = (WriteCoilResponse) getMaster(ip, port).send(coilRequest);
        return !coilResponse.isException();
    }
 
 
    /**
     * 写单个保持寄存器数据 0x06
     *
     * @param offset
     * @param value
     * @return
     * @throws ModbusTransportException
     * @throws ModbusInitException
     */
    public static Boolean writeRegister(String ip, int port, int offset, int value) throws ModbusTransportException, ModbusInitException {
        WriteRegisterRequest registerRequest = new WriteRegisterRequest(slaveId, offset, value);
        WriteRegisterResponse registerResponse = (WriteRegisterResponse) getMaster(ip, port).send(registerRequest);
        return !registerResponse.isException();
 
    }
 
    /**
     * 写线圈开关状态数据【多】  0x0f
     *
     * @param offset
     * @param booleans
     * @return
     * @throws ModbusTransportException
     * @throws ModbusInitException
     */
    public static Boolean writeCoils(String ip, int port, int offset, boolean[] booleans) throws ModbusTransportException, ModbusInitException {
        WriteCoilsRequest writeCoils = new WriteCoilsRequest(slaveId, offset, booleans);
        WriteCoilsResponse coilsResponse = (WriteCoilsResponse) getMaster(ip, port).send(writeCoils);
        return !coilsResponse.isException();
    }
 
 
    /**
     * 写保存寄存器数据【多】  0x10
     *
     * @param offset
     * @param nums
     * @return
     * @throws ModbusTransportException
     * @throws ModbusInitException
     */
    public static Boolean writeRegisters(String ip, int port, int offset, short[] nums) throws ModbusTransportException, ModbusInitException {
        WriteRegistersRequest writeRegisters = new WriteRegistersRequest(slaveId, offset, nums);
        WriteRegistersResponse registersResponse = (WriteRegistersResponse) getMaster(ip, port).send(writeRegisters);
        return !registersResponse.isException();
    }
}