提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.channel.modbus.collector; |
H |
2 |
|
|
3 |
import com.iailab.module.data.channel.modbus.dto.ChannelModBusDeviceDTO; |
|
4 |
import com.serotonin.modbus4j.ModbusFactory; |
|
5 |
import com.serotonin.modbus4j.ModbusMaster; |
|
6 |
import com.serotonin.modbus4j.exception.ErrorResponseException; |
|
7 |
import com.serotonin.modbus4j.exception.ModbusInitException; |
|
8 |
import com.serotonin.modbus4j.exception.ModbusTransportException; |
|
9 |
import com.serotonin.modbus4j.ip.IpParameters; |
|
10 |
import com.serotonin.modbus4j.locator.BaseLocator; |
|
11 |
import com.serotonin.modbus4j.msg.*; |
|
12 |
|
|
13 |
/** |
|
14 |
* Modbus 工具类 |
|
15 |
*/ |
|
16 |
public class ModbusUtils { |
|
17 |
|
|
18 |
/** |
|
19 |
* 工厂 |
|
20 |
*/ |
|
21 |
static ModbusFactory modbusFactory; |
|
22 |
static ModbusMaster modbusMaster; |
|
23 |
|
|
24 |
static { |
|
25 |
if (modbusFactory == null) { |
|
26 |
modbusFactory = new ModbusFactory(); |
|
27 |
} |
|
28 |
} |
|
29 |
|
|
30 |
|
|
31 |
/** |
|
32 |
* 获取master |
|
33 |
* |
|
34 |
* @param tModbusDTO |
|
35 |
* @return |
|
36 |
*/ |
|
37 |
public static ModbusMaster getMaster(ChannelModBusDeviceDTO tModbusDTO) throws ModbusInitException { |
|
38 |
if (modbusMaster == null) { |
|
39 |
IpParameters ipParameters = new IpParameters(); |
|
40 |
ipParameters.setHost(tModbusDTO.getAddress()); |
|
41 |
ipParameters.setPort(tModbusDTO.getPort()); |
|
42 |
modbusMaster = modbusFactory.createTcpMaster(ipParameters, true); |
|
43 |
//设置超时时间 |
|
44 |
modbusMaster.setTimeout(tModbusDTO.getReadTimeout()); |
|
45 |
//设置重连次数 |
|
46 |
modbusMaster.setRetries(tModbusDTO.getAttemptsBeforeTimeout()); |
|
47 |
modbusMaster.init(); |
|
48 |
return modbusMaster; |
|
49 |
} |
|
50 |
return modbusMaster; |
|
51 |
} |
|
52 |
|
|
53 |
/** |
|
54 |
* 测试用 |
|
55 |
* |
|
56 |
* @return |
|
57 |
*/ |
|
58 |
public static ModbusMaster getMaster() throws ModbusInitException { |
|
59 |
if (modbusMaster == null) { |
|
60 |
IpParameters ipParameters = new IpParameters(); |
|
61 |
ipParameters.setHost("127.0.0.1"); |
|
62 |
ipParameters.setPort(502); |
|
63 |
modbusMaster = modbusFactory.createTcpMaster(ipParameters, true); |
|
64 |
modbusMaster.init(); |
|
65 |
return modbusMaster; |
|
66 |
} |
|
67 |
return modbusMaster; |
|
68 |
} |
|
69 |
|
|
70 |
|
|
71 |
/** |
|
72 |
* 读取线圈开关状态数据 0x01 |
|
73 |
* |
|
74 |
* @param slaveId |
|
75 |
* @param offset |
|
76 |
* @return |
|
77 |
* @throws ModbusInitException |
|
78 |
* @throws ModbusTransportException |
|
79 |
* @throws ErrorResponseException |
|
80 |
*/ |
|
81 |
public static Boolean readCoilStatus(ModbusMaster modbusMaster, int slaveId, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException { |
|
82 |
BaseLocator<Boolean> coilStatus = BaseLocator.coilStatus(slaveId, offset); |
|
83 |
Boolean res = modbusMaster.getValue(coilStatus); |
|
84 |
return res; |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* 读离散输入寄存器状态数据 0x02 |
|
89 |
* |
|
90 |
* @param slaveId |
|
91 |
* @param offset |
|
92 |
* @return |
|
93 |
* @throws ModbusInitException |
|
94 |
* @throws ModbusTransportException |
|
95 |
* @throws ErrorResponseException |
|
96 |
*/ |
|
97 |
public static Boolean inputStatus(ModbusMaster modbusMaster, int slaveId, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException { |
|
98 |
BaseLocator<Boolean> inputStatus = BaseLocator.inputStatus(slaveId, offset); |
|
99 |
Boolean res = modbusMaster.getValue(inputStatus); |
|
100 |
return res; |
|
101 |
} |
|
102 |
|
|
103 |
/** |
|
104 |
* 读保持寄存器数据 0x03 |
|
105 |
* |
|
106 |
* @param modbusMaster |
|
107 |
* @param slaveId |
|
108 |
* @param offset |
|
109 |
* @param dataType |
|
110 |
* @return |
|
111 |
* @throws ModbusInitException |
|
112 |
* @throws ModbusTransportException |
|
113 |
* @throws ErrorResponseException |
|
114 |
*/ |
|
115 |
public static Number holdingRegister(ModbusMaster modbusMaster, int slaveId, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException { |
|
116 |
BaseLocator<Number> holdingRegister = BaseLocator.holdingRegister(slaveId, offset, dataType); |
|
117 |
Number value = modbusMaster.getValue(holdingRegister); |
|
118 |
return value; |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* 测试用 |
|
123 |
* |
|
124 |
* @return |
|
125 |
*/ |
|
126 |
public static Number holdingRegister(int slaveId, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException { |
|
127 |
BaseLocator<Number> holdingRegister = BaseLocator.holdingRegister(slaveId, offset, dataType); |
|
128 |
Number value = getMaster().getValue(holdingRegister); |
|
129 |
return value; |
|
130 |
} |
|
131 |
|
|
132 |
/** |
|
133 |
* 读输入寄存器数据 0x04 |
|
134 |
* |
|
135 |
* @param slaveId |
|
136 |
* @param offset |
|
137 |
* @param dataType |
|
138 |
* @return |
|
139 |
* @throws ModbusInitException |
|
140 |
* @throws ModbusTransportException |
|
141 |
* @throws ErrorResponseException |
|
142 |
*/ |
|
143 |
public static Number inputRegister(ModbusMaster modbusMaster, int slaveId, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException { |
|
144 |
BaseLocator<Number> inputRegister = BaseLocator.inputRegister(slaveId, offset, dataType); |
|
145 |
Number value = modbusMaster.getValue(inputRegister); |
|
146 |
return value; |
|
147 |
} |
|
148 |
|
|
149 |
|
|
150 |
/** |
|
151 |
* 写线圈开关状态数据 0x05 |
|
152 |
* |
|
153 |
* @param slaveId |
|
154 |
* @param offset |
|
155 |
* @param status |
|
156 |
* @return |
|
157 |
* @throws ModbusTransportException |
|
158 |
* @throws ModbusInitException |
|
159 |
*/ |
|
160 |
public static Boolean writeCoilStatus(ModbusMaster modbusMaster, int slaveId, int offset, boolean status) throws ModbusTransportException, ModbusInitException { |
|
161 |
boolean coilValue = status; |
|
162 |
WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, coilValue); |
|
163 |
WriteCoilResponse coilResponse = (WriteCoilResponse) modbusMaster.send(coilRequest); |
|
164 |
return !coilResponse.isException(); |
|
165 |
} |
|
166 |
|
|
167 |
|
|
168 |
/** |
|
169 |
* 写单个保持寄存器数据 0x06 |
|
170 |
* |
|
171 |
* @param slaveId |
|
172 |
* @param offset |
|
173 |
* @param value |
|
174 |
* @return |
|
175 |
* @throws ModbusTransportException |
|
176 |
* @throws ModbusInitException |
|
177 |
*/ |
|
178 |
public static Boolean writeRegister(ModbusMaster modbusMaster, int slaveId, int offset, int value) throws ModbusTransportException, ModbusInitException { |
|
179 |
WriteRegisterRequest registerRequest = new WriteRegisterRequest(slaveId, offset, value); |
|
180 |
WriteRegisterResponse registerResponse = (WriteRegisterResponse) modbusMaster.send(registerRequest); |
|
181 |
return !registerResponse.isException(); |
|
182 |
|
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* 测试用 |
|
187 |
* |
|
188 |
* @return |
|
189 |
*/ |
|
190 |
public static Boolean writeRegister(int slaveId, int offset, int value) throws ModbusTransportException, ModbusInitException { |
|
191 |
WriteRegisterRequest registerRequest = new WriteRegisterRequest(slaveId, offset, value); |
|
192 |
WriteRegisterResponse registerResponse = (WriteRegisterResponse) getMaster().send(registerRequest); |
|
193 |
return !registerResponse.isException(); |
|
194 |
|
|
195 |
} |
|
196 |
|
|
197 |
/** |
|
198 |
* 写线圈开关状态数据【多】 0x0f |
|
199 |
* |
|
200 |
* @param slaveId |
|
201 |
* @param offset |
|
202 |
* @param booleans |
|
203 |
* @return |
|
204 |
* @throws ModbusTransportException |
|
205 |
* @throws ModbusInitException |
|
206 |
*/ |
|
207 |
public static Boolean writeCoils(ModbusMaster modbusMaster, int slaveId, int offset, boolean[] booleans) throws ModbusTransportException, ModbusInitException { |
|
208 |
WriteCoilsRequest writeCoils = new WriteCoilsRequest(slaveId, offset, booleans); |
|
209 |
WriteCoilsResponse coilsResponse = (WriteCoilsResponse) modbusMaster.send(writeCoils); |
|
210 |
return !coilsResponse.isException(); |
|
211 |
} |
|
212 |
|
|
213 |
|
|
214 |
/** |
|
215 |
* 写保存寄存器数据【多】 0x10 |
|
216 |
* |
|
217 |
* @param slaveId |
|
218 |
* @param offset |
|
219 |
* @param nums |
|
220 |
* @return |
|
221 |
* @throws ModbusTransportException |
|
222 |
* @throws ModbusInitException |
|
223 |
*/ |
|
224 |
public static Boolean writeRegisters(ModbusMaster modbusMaster, int slaveId, int offset, short[] nums) throws ModbusTransportException, ModbusInitException { |
|
225 |
WriteRegistersRequest writeRegisters = new WriteRegistersRequest(slaveId, offset, nums); |
|
226 |
WriteRegistersResponse registersResponse = (WriteRegistersResponse) modbusMaster.send(writeRegisters); |
|
227 |
return !registersResponse.isException(); |
|
228 |
} |
|
229 |
} |