|
@@ -0,0 +1,133 @@
|
|
|
+package com.ruoyi.hard.modbus.tcp;
|
|
|
+
|
|
|
+import com.jwk.spring.boot.autoconfigure.ModbusTcpMasterTemplate;
|
|
|
+import com.jwk.spring.boot.constant.DATA_TYPE_WRAPPER;
|
|
|
+import com.jwk.spring.boot.modbus4j.ModbusMasterUtil;
|
|
|
+import com.serotonin.modbus4j.msg.ReadResponse;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 按钮盒对接
|
|
|
+ *
|
|
|
+ * @author JWK
|
|
|
+ * @version 1.0
|
|
|
+ * @date 2022/12/21 21:04
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ButtonBoxClient {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按钮盒
|
|
|
+ */
|
|
|
+ @Autowired(required = false)
|
|
|
+ private ModbusTcpMasterTemplate modbusTcpMasterTemplateThird;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按钮盒命令
|
|
|
+ */
|
|
|
+ public enum BUTTON_BOX_CODE {
|
|
|
+ /**
|
|
|
+ * 1号按钮状态 value代表读的长度(读到1代表按钮被按过) value1代表清除按下标记
|
|
|
+ */
|
|
|
+ BTN_STS_01(1, 1, 1, 0, 0, 0, 0),
|
|
|
+ /**
|
|
|
+ * 1号按钮状态 value代表读的长度(读到1代表按钮被按过) value1代表清除按下标记
|
|
|
+ */
|
|
|
+ BTN_STS_02(1, 2, 1, 0, 0, 0, 0),
|
|
|
+ /**
|
|
|
+ * 1号灯状态 value代表读的长度(0:灭 1:亮 2:快闪 3慢闪) value1-4代表写(0:灭 1:亮 2:快闪 3慢闪)
|
|
|
+ */
|
|
|
+ LAMP_STS_01(1, 5, 1, 0, 1, 2, 3),
|
|
|
+ /**
|
|
|
+ * 1号灯状态 value代表读的长度(0:灭 1:亮 2:快闪 3慢闪) value1-4代表写(0:灭 1:亮 2:快闪 3慢闪)
|
|
|
+ */
|
|
|
+ LAMP_STS_02(1, 6, 1, 0, 1, 2, 3);
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从节点id
|
|
|
+ */
|
|
|
+ private int slaveId;
|
|
|
+ /**
|
|
|
+ * 地址
|
|
|
+ */
|
|
|
+ private int offset;
|
|
|
+ /**
|
|
|
+ * 读的长度
|
|
|
+ */
|
|
|
+ private int value;
|
|
|
+ /**
|
|
|
+ * 写的值
|
|
|
+ */
|
|
|
+ private int value1;
|
|
|
+ private int value2;
|
|
|
+ private int value3;
|
|
|
+ private int value4;
|
|
|
+
|
|
|
+ BUTTON_BOX_CODE(int slaveId, int offset, int value, int value1, int value2, int value3, int value4) {
|
|
|
+ this.slaveId = slaveId;
|
|
|
+ this.offset = offset;
|
|
|
+ this.value = value;
|
|
|
+ this.value1 = value1;
|
|
|
+ this.value2 = value2;
|
|
|
+ this.value3 = value3;
|
|
|
+ this.value4 = value4;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getSlaveId() {
|
|
|
+ return slaveId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getOffset() {
|
|
|
+ return offset;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getValue() {
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getValue1() {
|
|
|
+ return value1;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getValue2() {
|
|
|
+ return value2;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getValue3() {
|
|
|
+ return value3;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getValue4() {
|
|
|
+ return value4;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public void readBtn01() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void read(int slaveId, int offset, int len) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
|
|
|
+ ReadResponse readResponse = modbusMasterUtil.readHoldingRegisters(slaveId, offset, len);
|
|
|
+ short[] shortData = readResponse.getShortData();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void readNum(int slaveId, int offset) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
|
|
|
+ Number number = modbusMasterUtil.readHoldingRegister(slaveId, offset, DATA_TYPE_WRAPPER.EIGHT_BYTE_INT_SIGNED);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public Boolean write(int slaveId, int offset, int value) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
|
|
|
+ return modbusMasterUtil.writeHoldingRegisters(slaveId, offset, value);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|