Modbus4jUtils.java
7.11 KB
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
//package com.tcb.modbus;
//
//import com.serotonin.modbus4j.BatchRead;
//import com.serotonin.modbus4j.BatchResults;
//import com.serotonin.modbus4j.ModbusFactory;
//import com.serotonin.modbus4j.ModbusMaster;
//import com.serotonin.modbus4j.code.DataType;
//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;
//
///**
// * modbus通讯工具类,采用modbus4j实现
// *
// * @author lxq
// * @dependencies modbus4j-3.0.3.jar
// * @website https://github.com/infiniteautomation/modbus4j
// */
//public class Modbus4jUtils {
// /**
// * 工厂。
// */
// static ModbusFactory modbusFactory;
// static {
// if (modbusFactory == null) {
// modbusFactory = new ModbusFactory();
// }
// }
//
// /**
// * 获取master
// *
// * @return
// * @throws ModbusInitException
// */
// public static ModbusMaster getMaster() throws ModbusInitException {
// IpParameters params = new IpParameters();
// params.setHost("192.168.2.1");
// params.setPort(502);
// //
// // modbusFactory.createRtuMaster(wapper); //RTU 协议
// // modbusFactory.createUdpMaster(params);//UDP 协议
// // modbusFactory.createAsciiMaster(wrapper);//ASCII 协议
// ModbusMaster master = modbusFactory.createTcpMaster(params, false);// TCP 协议
// master.init();
//
// return master;
// }
//
// /**
// * 读取[01 Coil Status 0x]类型 开关数据
// *
// * @param slaveId
// * slaveId
// * @param offset
// * 位置
// * @return 读取值
// * @throws ModbusTransportException
// * 异常
// * @throws ErrorResponseException
// * 异常
// * @throws ModbusInitException
// * 异常
// */
// public static Boolean readCoilStatus(int slaveId, int offset)
// throws ModbusTransportException, ErrorResponseException, ModbusInitException {
// // 01 Coil Status
// BaseLocator<Boolean> loc = BaseLocator.coilStatus(slaveId, offset);
// Boolean value = getMaster().getValue(loc);
// return value;
// }
//
// /**
// * 读取[02 Input Status 1x]类型 开关数据
// *
// * @param slaveId
// * @param offset
// * @return
// * @throws ModbusTransportException
// * @throws ErrorResponseException
// * @throws ModbusInitException
// */
// public static Boolean readInputStatus(int slaveId, int offset)
// throws ModbusTransportException, ErrorResponseException, ModbusInitException {
// // 02 Input Status
// BaseLocator<Boolean> loc = BaseLocator.inputStatus(slaveId, offset);
// Boolean value = getMaster().getValue(loc);
// return value;
// }
//
// /**
// * 读取[03 Holding Register类型 2x]模拟量数据
// *
// * @param slaveId
// * slave Id
// * @param offset
// * 位置
// * @param dataType
// * 数据类型,来自com.serotonin.modbus4j.code.DataType
// * @return
// * @throws ModbusTransportException
// * 异常
// * @throws ErrorResponseException
// * 异常
// * @throws ModbusInitException
// * 异常
// */
// public static Number readHoldingRegister(int slaveId, int offset, int dataType)
// throws ModbusTransportException, ErrorResponseException, ModbusInitException {
// // 03 Holding Register类型数据读取
// BaseLocator<Number> loc = BaseLocator.holdingRegister(slaveId, offset, dataType);
// Number value = getMaster().getValue(loc);
// return value;
// }
//
// /**
// * 读取[04 Input Registers 3x]类型 模拟量数据
// *
// * @param slaveId
// * slaveId
// * @param offset
// * 位置
// * @param dataType
// * 数据类型,来自com.serotonin.modbus4j.code.DataType
// * @return 返回结果
// * @throws ModbusTransportException
// * 异常
// * @throws ErrorResponseException
// * 异常
// * @throws ModbusInitException
// * 异常
// */
// public static Number readInputRegisters(int slaveId, int offset, int dataType)
// throws ModbusTransportException, ErrorResponseException, ModbusInitException {
// // 04 Input Registers类型数据读取
// BaseLocator<Number> loc = BaseLocator.inputRegister(slaveId, offset, dataType);
// Number value = getMaster().getValue(loc);
// return value;
// }
//
// /**
// * 批量读取使用方法
// *
// * @throws ModbusTransportException
// * @throws ErrorResponseException
// * @throws ModbusInitException
// */
// public static void batchRead() throws ModbusTransportException, ErrorResponseException, ModbusInitException {
//
// BatchRead<Integer> batch = new BatchRead<Integer>();
//
// batch.addLocator(0, BaseLocator.holdingRegister(1, 1, DataType.FOUR_BYTE_FLOAT));
// batch.addLocator(1, BaseLocator.inputStatus(1, 0));
//
// ModbusMaster master = getMaster();
//
// batch.setContiguousRequests(false);
// BatchResults<Integer> results = master.send(batch);
// System.out.println(results.getValue(0));
// System.out.println(results.getValue(1));
// }
//
// /**
// * 测试
// *
// * @param args
// */
// public static void main(String[] args) {
// try {
// // 01测试
// Boolean v011 = readCoilStatus(1, 0);
// //Boolean v012 = readCoilStatus(1, 1);
// // Boolean v013 = readCoilStatus(1, 6);
// System.out.println("v011:" + v011);
// // System.out.println("v012:" + v012);
// // System.out.println("v013:" + v013);
// // 02测试
// // Boolean v021 = readInputStatus(1, 0);
// // Boolean v022 = readInputStatus(1, 1);
// // Boolean v023 = readInputStatus(1, 2);
// // System.out.println("v021:" + v021);
// // System.out.println("v022:" + v022);
// // System.out.println("v023:" + v023);
//
// // 03测试
// Number v031 = readHoldingRegister(1, 1, DataType.FOUR_BYTE_FLOAT);// 注意,float
// // Number v032 = readHoldingRegister(1, 3, DataType.FOUR_BYTE_FLOAT);// 同上
// System.out.println("v031:" + v031);
// // System.out.println("v032:" + v032);
//
// // 04测试
// Number v041 = readInputRegisters(1, 0, DataType.FOUR_BYTE_FLOAT);//
// // Number v042 = readInputRegisters(1, 2, DataType.FOUR_BYTE_FLOAT);//
// System.out.println("v041:" + v041);
// // System.out.println("v042:" + v042);
// // 批量读取
// // batchRead();
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//}
//