|
@@ -0,0 +1,83 @@
|
|
|
|
+package com.ruoyi.web.controller.warewms.Test;
|
|
|
|
+
|
|
|
|
+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.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
+import com.serotonin.modbus4j.msg.ReadResponse;
|
|
|
|
+import io.swagger.annotations.Api;
|
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author JWK
|
|
|
|
+ * @version 1.0
|
|
|
|
+ * @date 2022/11/14 14:52
|
|
|
|
+ */
|
|
|
|
+@Api("Modbus测试")
|
|
|
|
+@RestController
|
|
|
|
+@RequestMapping("/api/modbus")
|
|
|
|
+public class ModbusTestController {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ModbusTcpMasterTemplate modbusTcpMasterTemplate;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 自动门命令
|
|
|
|
+ */
|
|
|
|
+ public enum AUTOMATIC_DOOR_CODE {
|
|
|
|
+ /***
|
|
|
|
+ * 读门状态
|
|
|
|
+ */
|
|
|
|
+ RAED_STS(1, 0, 0),
|
|
|
|
+ /**
|
|
|
|
+ * 开门
|
|
|
|
+ */
|
|
|
|
+ WRITE_OPEN(1, 1, 1),
|
|
|
|
+ /**
|
|
|
|
+ * 关门
|
|
|
|
+ */
|
|
|
|
+ WRITE_CLOSE(1, 2, 1);
|
|
|
|
+ private int slaveId;
|
|
|
|
+ private int offset;
|
|
|
|
+ private int value;
|
|
|
|
+
|
|
|
|
+ AUTOMATIC_DOOR_CODE(int slaveId, int offset, int value) {
|
|
|
|
+ this.slaveId = slaveId;
|
|
|
|
+ this.offset = offset;
|
|
|
|
+ this.value = value;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 读保持寄存器
|
|
|
|
+ */
|
|
|
|
+ @ApiOperation("读取")
|
|
|
|
+ @PostMapping("/read")
|
|
|
|
+ public AjaxResult read(int slaveId, int offset, int value) {
|
|
|
|
+ ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplate.getModbusMasterUtil();
|
|
|
|
+ if (value > 0) {
|
|
|
|
+ ReadResponse readResponse = modbusMasterUtil.readHoldingRegisters(slaveId, offset, value);
|
|
|
|
+ byte[] data = readResponse.getData();
|
|
|
|
+ }else {
|
|
|
|
+ Number number = modbusMasterUtil.readHoldingRegister(slaveId, offset,DATA_TYPE_WRAPPER.FOUR_BYTE_INT_SIGNED);
|
|
|
|
+ int i = number.intValue();
|
|
|
|
+ }
|
|
|
|
+ return AjaxResult.success();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 写保持寄存器
|
|
|
|
+ */
|
|
|
|
+ @ApiOperation("发送")
|
|
|
|
+ @PostMapping("/send")
|
|
|
|
+ public AjaxResult send(int slaveId, int offset, int value) {
|
|
|
|
+ ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplate.getModbusMasterUtil();
|
|
|
|
+ Boolean aBoolean = modbusMasterUtil.writeHoldingRegisters(slaveId, offset, value);
|
|
|
|
+ return AjaxResult.success(aBoolean.toString());
|
|
|
|
+ }
|
|
|
|
+}
|