ButtonBoxClient.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package com.ruoyi.hard.modbus.tcp;
  2. import com.jwk.spring.boot.autoconfigure.ModbusTcpMasterTemplate;
  3. import com.jwk.spring.boot.modbus4j.ModbusMasterUtil;
  4. import com.serotonin.modbus4j.msg.ReadResponse;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.stereotype.Service;
  8. import static com.ruoyi.hard.modbus.tcp.ButtonBoxClient.BUTTON_BOX_CODE.*;
  9. /**
  10. * 按钮盒对接
  11. *
  12. * @author JWK
  13. * @version 1.0
  14. * @date 2022/12/21 21:04
  15. */
  16. @Service
  17. public class ButtonBoxClient {
  18. /**
  19. * 按钮盒
  20. */
  21. @Autowired(required = false)
  22. @Qualifier("modbusTcpMasterTemplateThird")
  23. private ModbusTcpMasterTemplate modbusTcpMasterTemplateThird;
  24. /**
  25. * 按钮盒命令
  26. */
  27. public enum BUTTON_BOX_CODE {
  28. /**
  29. * 1号按钮状态 value代表读的长度(读到1代表按钮被按过) value1代表清除按下标记
  30. */
  31. BTN_STS_01(1, 1, 1, 0, 0, 0, 0),
  32. /**
  33. * 1号按钮状态 value代表读的长度(读到1代表按钮被按过) value1代表清除按下标记
  34. */
  35. BTN_STS_02(1, 2, 1, 0, 0, 0, 0),
  36. /**
  37. * 1号灯状态 value代表读的长度(0:灭 1:亮 2:快闪 3慢闪) value1-4代表写(0:灭 1:亮 2:快闪 3慢闪)
  38. */
  39. LAMP_STS_01(1, 5, 1, 0, 1, 2, 3),
  40. /**
  41. * 1号灯状态 value代表读的长度(0:灭 1:亮 2:快闪 3慢闪) value1-4代表写(0:灭 1:亮 2:快闪 3慢闪)
  42. */
  43. LAMP_STS_02(1, 6, 1, 0, 1, 2, 3);
  44. /**
  45. * 从节点id
  46. */
  47. private int slaveId;
  48. /**
  49. * 地址
  50. */
  51. private int offset;
  52. /**
  53. * 读的长度
  54. */
  55. private int value;
  56. /**
  57. * 写的值
  58. */
  59. private int value1;
  60. private int value2;
  61. private int value3;
  62. private int value4;
  63. BUTTON_BOX_CODE(int slaveId, int offset, int value, int value1, int value2, int value3, int value4) {
  64. this.slaveId = slaveId;
  65. this.offset = offset;
  66. this.value = value;
  67. this.value1 = value1;
  68. this.value2 = value2;
  69. this.value3 = value3;
  70. this.value4 = value4;
  71. }
  72. public int getSlaveId() {
  73. return slaveId;
  74. }
  75. public int getOffset() {
  76. return offset;
  77. }
  78. public int getValue() {
  79. return value;
  80. }
  81. public int getValue1() {
  82. return value1;
  83. }
  84. public int getValue2() {
  85. return value2;
  86. }
  87. public int getValue3() {
  88. return value3;
  89. }
  90. public int getValue4() {
  91. return value4;
  92. }
  93. }
  94. /**
  95. * 读按钮1状态
  96. *
  97. * @return true 代表按钮被按下
  98. */
  99. public Boolean readBtn01() {
  100. short i = read(BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue());
  101. return i == 1;
  102. }
  103. /**
  104. * 读按钮2状态
  105. *
  106. * @return true 代表按钮被按下
  107. */
  108. public Boolean readBtn02() {
  109. short i = read(BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue());
  110. return i == 1;
  111. }
  112. /**
  113. * 清除按钮1状态
  114. *
  115. * @return true
  116. */
  117. public Boolean clearBtn01() {
  118. return write(BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue1());
  119. }
  120. /**
  121. * 清除按钮2状态
  122. *
  123. * @return true
  124. */
  125. public Boolean clearBtn02() {
  126. return write(BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue1());
  127. }
  128. /**
  129. * 写灯1亮
  130. *
  131. * @return true
  132. */
  133. public Boolean writeLamp01Bright() {
  134. return write(LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue2());
  135. }
  136. /**
  137. * 写灯2亮
  138. *
  139. * @return true
  140. */
  141. public Boolean writeLamp02Bright() {
  142. return write(LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue2());
  143. }
  144. /**
  145. * 写灯1灭
  146. *
  147. * @return true
  148. */
  149. public Boolean writeLamp01Death() {
  150. return write(LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue1());
  151. }
  152. /**
  153. * 写灯2灭
  154. *
  155. * @return true
  156. */
  157. public Boolean writeLamp02Death() {
  158. return write(LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue1());
  159. }
  160. private short read(int slaveId, int offset, int len) {
  161. ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
  162. ReadResponse readResponse = modbusMasterUtil.readHoldingRegisters(slaveId, offset, len);
  163. if (readResponse == null) {
  164. return 0;
  165. }
  166. short[] shortData = readResponse.getShortData();
  167. return shortData[0];
  168. }
  169. private Boolean write(int slaveId, int offset, int value) {
  170. ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
  171. return modbusMasterUtil.writeHoldingRegisters(slaveId, offset, value);
  172. }
  173. }