|
@@ -1,12 +1,13 @@
|
|
package com.ruoyi.hard.modbus.tcp;
|
|
package com.ruoyi.hard.modbus.tcp;
|
|
|
|
|
|
import com.jwk.spring.boot.autoconfigure.ModbusTcpMasterTemplate;
|
|
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.jwk.spring.boot.modbus4j.ModbusMasterUtil;
|
|
import com.serotonin.modbus4j.msg.ReadResponse;
|
|
import com.serotonin.modbus4j.msg.ReadResponse;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
+import static com.ruoyi.hard.modbus.tcp.ButtonBoxClient.BUTTON_BOX_CODE.*;
|
|
|
|
+
|
|
|
|
|
|
/**
|
|
/**
|
|
* 按钮盒对接
|
|
* 按钮盒对接
|
|
@@ -107,25 +108,88 @@ public class ButtonBoxClient {
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 读按钮1状态
|
|
|
|
+ *
|
|
|
|
+ * @return true 代表按钮被按下
|
|
|
|
+ */
|
|
|
|
+ public Boolean readBtn01() {
|
|
|
|
+ short i = read(BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue());
|
|
|
|
+ return i == 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 读按钮2状态
|
|
|
|
+ *
|
|
|
|
+ * @return true 代表按钮被按下
|
|
|
|
+ */
|
|
|
|
+ public Boolean readBtn02() {
|
|
|
|
+ short i = read(BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue());
|
|
|
|
+ return i == 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 清除按钮1状态
|
|
|
|
+ *
|
|
|
|
+ * @return true
|
|
|
|
+ */
|
|
|
|
+ public Boolean clearBtn01() {
|
|
|
|
+ return write(BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue1());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 清除按钮2状态
|
|
|
|
+ *
|
|
|
|
+ * @return true
|
|
|
|
+ */
|
|
|
|
+ public Boolean clearBtn02() {
|
|
|
|
+ return write(BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue1());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 写灯1亮
|
|
|
|
+ *
|
|
|
|
+ * @return true
|
|
|
|
+ */
|
|
|
|
+ public Boolean writeLamp01Bright() {
|
|
|
|
+ return write(LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue2());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 写灯2亮
|
|
*
|
|
*
|
|
|
|
+ * @return true
|
|
*/
|
|
*/
|
|
- public void readBtn01() {
|
|
|
|
|
|
+ public Boolean writeLamp02Bright() {
|
|
|
|
+ return write(LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue2());
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 写灯1灭
|
|
|
|
+ *
|
|
|
|
+ * @return true
|
|
|
|
+ */
|
|
|
|
+ public Boolean writeLamp01Death() {
|
|
|
|
+ return write(LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue1());
|
|
}
|
|
}
|
|
|
|
|
|
- public void read(int slaveId, int offset, int len) {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 写灯2灭
|
|
|
|
+ *
|
|
|
|
+ * @return true
|
|
|
|
+ */
|
|
|
|
+ public Boolean writeLamp02Death() {
|
|
|
|
+ return write(LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue1());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private short read(int slaveId, int offset, int len) {
|
|
ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
|
|
ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
|
|
ReadResponse readResponse = modbusMasterUtil.readHoldingRegisters(slaveId, offset, len);
|
|
ReadResponse readResponse = modbusMasterUtil.readHoldingRegisters(slaveId, offset, len);
|
|
short[] shortData = readResponse.getShortData();
|
|
short[] shortData = readResponse.getShortData();
|
|
|
|
+ return shortData[0];
|
|
}
|
|
}
|
|
|
|
|
|
- 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) {
|
|
|
|
|
|
+ private Boolean write(int slaveId, int offset, int value) {
|
|
ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
|
|
ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
|
|
return modbusMasterUtil.writeHoldingRegisters(slaveId, offset, value);
|
|
return modbusMasterUtil.writeHoldingRegisters(slaveId, offset, value);
|
|
}
|
|
}
|