ModbusTcpMasterUtil.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.jwk.spring.boot.test;
  2. import com.serotonin.modbus4j.BatchRead;
  3. import com.serotonin.modbus4j.BatchResults;
  4. import com.serotonin.modbus4j.ModbusFactory;
  5. import com.serotonin.modbus4j.ModbusMaster;
  6. import com.serotonin.modbus4j.code.DataType;
  7. import com.serotonin.modbus4j.exception.ErrorResponseException;
  8. import com.serotonin.modbus4j.exception.ModbusInitException;
  9. import com.serotonin.modbus4j.exception.ModbusTransportException;
  10. import com.serotonin.modbus4j.ip.IpParameters;
  11. import com.serotonin.modbus4j.locator.BaseLocator;
  12. /**
  13. *
  14. * todo 这是另一种读法 先保存在这里
  15. * modbus通讯工具类,采用modbus4j实现
  16. *
  17. * @author lxq
  18. * @dependencies modbus4j-3.0.3.jar
  19. * @website https://github.com/infiniteautomation/modbus4j
  20. */
  21. public class ModbusTcpMasterUtil {
  22. /**
  23. * 工厂。
  24. */
  25. static ModbusFactory modbusFactory;
  26. static {
  27. if (modbusFactory == null) {
  28. modbusFactory = new ModbusFactory();
  29. }
  30. }
  31. /**
  32. * 获取master
  33. *
  34. * @return
  35. * @throws ModbusInitException
  36. */
  37. public static ModbusMaster getMaster() throws ModbusInitException {
  38. IpParameters params = new IpParameters();
  39. params.setHost("localhost");
  40. params.setPort(502);
  41. //
  42. // modbusFactory.createRtuMaster(wapper); //RTU 协议
  43. // modbusFactory.createUdpMaster(params);//UDP 协议
  44. // modbusFactory.createAsciiMaster(wrapper);//ASCII 协议
  45. ModbusMaster master = modbusFactory.createTcpMaster(params, false);// TCP 协议
  46. master.init();
  47. return master;
  48. }
  49. /**
  50. * 读取[01 Coil Status 0x]类型 开关数据
  51. *
  52. * @param slaveId slaveId
  53. * @param offset 位置
  54. * @return 读取值
  55. * @throws ModbusTransportException 异常
  56. * @throws ErrorResponseException 异常
  57. * @throws ModbusInitException 异常
  58. */
  59. public static Boolean readCoilStatus(int slaveId, int offset)
  60. throws ModbusTransportException, ErrorResponseException, ModbusInitException {
  61. // 01 Coil Status
  62. BaseLocator<Boolean> loc = BaseLocator.coilStatus(slaveId, offset);
  63. Boolean value = getMaster().getValue(loc);
  64. return value;
  65. }
  66. /**
  67. * 读取[02 Input Status 1x]类型 开关数据
  68. *
  69. * @param slaveId
  70. * @param offset
  71. * @return
  72. * @throws ModbusTransportException
  73. * @throws ErrorResponseException
  74. * @throws ModbusInitException
  75. */
  76. public static Boolean readInputStatus(int slaveId, int offset)
  77. throws ModbusTransportException, ErrorResponseException, ModbusInitException {
  78. // 02 Input Status
  79. BaseLocator<Boolean> loc = BaseLocator.inputStatus(slaveId, offset);
  80. Boolean value = getMaster().getValue(loc);
  81. return value;
  82. }
  83. /**
  84. * 读取[03 Holding Register类型 2x]模拟量数据
  85. *
  86. * @param slaveId slave Id
  87. * @param offset 位置
  88. * @param dataType 数据类型,来自com.serotonin.modbus4j.code.DataType
  89. * @return
  90. * @throws ModbusTransportException 异常
  91. * @throws ErrorResponseException 异常
  92. * @throws ModbusInitException 异常
  93. */
  94. public static Number readHoldingRegister(int slaveId, int offset, int dataType)
  95. throws ModbusTransportException, ErrorResponseException, ModbusInitException {
  96. // 03 Holding Register类型数据读取
  97. BaseLocator<Number> loc = BaseLocator.holdingRegister(slaveId, offset, dataType);
  98. Number value = getMaster().getValue(loc);
  99. return value;
  100. }
  101. /**
  102. * 读取[04 Input Registers 3x]类型 模拟量数据
  103. *
  104. * @param slaveId slaveId
  105. * @param offset 位置
  106. * @param dataType 数据类型,来自com.serotonin.modbus4j.code.DataType
  107. * @return 返回结果
  108. * @throws ModbusTransportException 异常
  109. * @throws ErrorResponseException 异常
  110. * @throws ModbusInitException 异常
  111. */
  112. public static Number readInputRegisters(int slaveId, int offset, int dataType)
  113. throws ModbusTransportException, ErrorResponseException, ModbusInitException {
  114. // 04 Input Registers类型数据读取
  115. BaseLocator<Number> loc = BaseLocator.inputRegister(slaveId, offset, dataType);
  116. Number value = getMaster().getValue(loc);
  117. return value;
  118. }
  119. /**
  120. * 批量读取使用方法
  121. *
  122. * @throws ModbusTransportException
  123. * @throws ErrorResponseException
  124. * @throws ModbusInitException
  125. */
  126. public static void batchRead() throws ModbusTransportException, ErrorResponseException, ModbusInitException {
  127. BatchRead<Integer> batch = new BatchRead<Integer>();
  128. batch.addLocator(0, BaseLocator.holdingRegister(1, 1, DataType.FOUR_BYTE_FLOAT));
  129. batch.addLocator(1, BaseLocator.inputStatus(1, 0));
  130. ModbusMaster master = getMaster();
  131. batch.setContiguousRequests(false);
  132. BatchResults<Integer> results = master.send(batch);
  133. System.out.println(results.getValue(0));
  134. System.out.println(results.getValue(1));
  135. }
  136. /**
  137. * 测试
  138. *
  139. * @param args
  140. */
  141. public static void main(String[] args) {
  142. try {
  143. // 01测试
  144. Boolean v011 = readCoilStatus(1, 0);
  145. Boolean v012 = readCoilStatus(1, 1);
  146. Boolean v013 = readCoilStatus(1, 6);
  147. System.out.println("v011:" + v011);
  148. System.out.println("v012:" + v012);
  149. System.out.println("v013:" + v013);
  150. // 02测试
  151. Boolean v021 = readInputStatus(1, 0);
  152. Boolean v022 = readInputStatus(1, 1);
  153. Boolean v023 = readInputStatus(1, 2);
  154. System.out.println("v021:" + v021);
  155. System.out.println("v022:" + v022);
  156. System.out.println("v023:" + v023);
  157. // 03测试
  158. Number v031 = readHoldingRegister(1, 1, DataType.FOUR_BYTE_FLOAT);// 注意,float
  159. Number v032 = readHoldingRegister(1, 3, DataType.FOUR_BYTE_FLOAT);// 同上
  160. System.out.println("v031:" + v031);
  161. System.out.println("v032:" + v032);
  162. // 04测试
  163. Number v041 = readInputRegisters(1, 1, DataType.FOUR_BYTE_FLOAT);//
  164. Number v042 = readInputRegisters(1, 3, DataType.FOUR_BYTE_FLOAT);//
  165. System.out.println("v041:" + v041);
  166. System.out.println("v042:" + v042);
  167. // 批量读取
  168. batchRead();
  169. } catch (Exception e) {
  170. e.printStackTrace();
  171. }
  172. }
  173. }