HuKang 2 anni fa
parent
commit
a24fcaed69

+ 196 - 0
ruoyi-admin/src/main/java/com/ruoyi/xuankuang/controller/test/UnpackingMachineController.java

@@ -0,0 +1,196 @@
+package com.ruoyi.xuankuang.controller.test;
+
+import com.ruoyi.ams.xuankuang.domain.form.OutTaskForm;
+import com.ruoyi.ams.xuankuang.domain.vo.WcsResponseVo;
+import com.ruoyi.hard.xuankang.UnpackingMachineClient;
+import com.ruoyi.hard.xuankang.UnpackingMachineSubClient;
+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.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import static com.ruoyi.hard.xuankang.UnpackingMachineClient.REGISTER_CODE.REGISTER_CODE_131;
+import static com.ruoyi.hard.xuankang.UnpackingMachineClient.REGISTER_CODE.REGISTER_CODE_132;
+
+/**
+ * @author Jwk
+ * @version 1.0
+ * @date 2023/4/20 15:43
+ */
+@Api("拆包机接口测试")
+@RestController
+@RequestMapping("/test/wcs2")
+public class UnpackingMachineController {
+    @Autowired
+    private UnpackingMachineClient unpackingMachineClient;
+    @Autowired
+    private UnpackingMachineSubClient unpackingMachineSubClient;
+
+    /**
+     * 拆包机1#状态读取接口
+     *
+     * @return
+     */
+    @ApiOperation("拆包机1#状态读取接口")
+    @PostMapping("/read01")
+    public boolean[] read01() {
+        return unpackingMachineClient.readUnpacking01();
+    }
+
+    /**
+     * 拆包机2#状态读取接口
+     *
+     * @return
+     */
+    @ApiOperation("拆包机2#状态读取接口")
+    @PostMapping("/read02")
+    public boolean[] read02() {
+        return unpackingMachineClient.readUnpacking02();
+    }
+
+    /**
+     * 拆包机3#状态读取接口
+     *
+     * @return
+     */
+    @ApiOperation("拆包机3#状态读取接口")
+    @PostMapping("/read03")
+    public boolean[] read03() {
+        return unpackingMachineClient.readUnpacking03();
+    }
+
+    /**
+     * 拆包机4#状态读取接口
+     *
+     * @return
+     */
+    @ApiOperation("拆包机4#状态读取接口")
+    @PostMapping("/read04")
+    public boolean[] read04() {
+        return unpackingMachineClient.readUnpacking04();
+    }
+
+
+    /**
+     * 拆包机5#状态读取接口
+     *
+     * @return
+     */
+    @ApiOperation("拆包机5#状态读取接口")
+    @PostMapping("/read05")
+    public boolean[] read05() {
+        return unpackingMachineClient.readUnpacking05();
+    }
+
+    /**
+     * 皮带运行状态读取接口
+     *
+     * @return
+     */
+    @ApiOperation("皮带运行状态读取接口")
+    @PostMapping("/read06")
+    public boolean[] read06() {
+        return unpackingMachineClient.readBelt();
+    }
+
+    /**
+     * 启动拆包机1#
+     *
+     * @return
+     */
+    @ApiOperation("启动拆包机1#接口")
+    @PostMapping("write01")
+    public boolean write01() {
+        return unpackingMachineSubClient.writeV140_5(true);
+    }
+//    /**
+//     * 拆包机1#状态写入接口
+//     *
+//     * @return
+//     */
+//    @ApiOperation("拆包机1#状态写入接口")
+//    @PostMapping("write01")
+//    public boolean write01() {
+//        return unpackingMachineClient.writeUnpacking01();
+//    }
+//
+//    /**
+//     * 拆包机2#状态写入接口
+//     *
+//     * @return
+//     */
+//    @ApiOperation("拆包机2#状态写入接口")
+//    @PostMapping("write02")
+//    public boolean write02() {
+//        return unpackingMachineClient.writeUnpacking02();
+//    }
+//
+//    /**
+//     * 拆包机3#状态写入接口
+//     *
+//     * @return
+//     */
+//    @ApiOperation("拆包机3#状态写入接口")
+//    @PostMapping("write03")
+//    public boolean write03() {
+//        return unpackingMachineClient.writeUnpacking03();
+//    }
+//
+//    /**
+//     * 拆包机4#状态写入接口
+//     *
+//     * @return
+//     */
+//    @ApiOperation("拆包机4#状态写入接口")
+//    @PostMapping("write04")
+//    public boolean write04() {
+//        return unpackingMachineClient.writeUnpacking04();
+//    }
+//
+//    /**
+//     * 拆包机5#状态写入接口
+//     *
+//     * @return
+//     */
+//    @ApiOperation("拆包机5#状态写入接口")
+//    @PostMapping("write05")
+//    public boolean write05() {
+//        return unpackingMachineClient.writeUnpacking05();
+//    }
+//
+//    /**
+//     * 皮带状态写入接口
+//     *
+//     * @return
+//     */
+//    @ApiOperation("皮带状态写入接口")
+//    @PostMapping("write06")
+//    public boolean write06() {
+//        return unpackingMachineClient.writeBlet();
+//    }
+//
+//    /**
+//     * 拆包机就地/远程 1# 2# 3#拆包机启动 状态写入接口
+//     *
+//     * @return
+//     */
+//    @ApiOperation("拆包机就地/远程 1# 2# 3#拆包机启动 状态写入接口")
+//    @PostMapping("write07")
+//    public boolean write07(int value) {
+//        return unpackingMachineClient.writeUnpackingRemoteInPlace01(value);
+//    }
+//
+//    /**
+//     * 4# 5#拆包机启动  1# 2# 3# 4# 5#拆包机停止 状态写入接口
+//     *
+//     * @return
+//     */
+//    @ApiOperation("4# 5#拆包机启动 1# 2# 3# 4# 5#停止 拆包机状态写入接口")
+//    @PostMapping("write08")
+//    public boolean write08() {
+//        return unpackingMachineClient.writeUnpackingRemoteInPlace02();
+//    }
+}

+ 11 - 10
ruoyi-admin/src/main/resources/application-prod.yml

@@ -68,16 +68,17 @@ init-task:
     aciservice: true # ndc通讯
 
 # modbus-tcp
-#modbus:
-#    tcp-master:
-#        first:
-#            open: true
-#            host: 192.168.42.201
-#            port: 9000
-#        second:
-#            open: true
-#            host: 192.168.42.202
-#            port: 9000
+modbus:
+    tcp-master:
+        first: # 搅拌槽
+            open: true
+            host: 192.168.100.130
+            port: 502
+        second: # 拆包机
+            open: true
+            host: 192.168.100.136
+            port: 502
+
 
 
 

+ 39 - 0
warewms-hard/src/main/java/com/ruoyi/hard/xuankang/ConvertUtil.java

@@ -0,0 +1,39 @@
+package com.ruoyi.hard.xuankang;
+
+/**
+ * @author Jwk
+ * @version 1.0
+ * @date 2023/4/23 16:12
+ */
+public class ConvertUtil {
+
+    public static boolean[] convertToBooleans(Number num) {
+        //转换为bool数组
+        int i = num.intValue();
+        String string = Integer.toBinaryString(i);
+        StringBuilder stringBuilder = new StringBuilder(string);
+        String string1 = stringBuilder.reverse().toString();
+        char[] chars = string1.toCharArray();
+        boolean[] booleans = new boolean[chars.length];
+        for (int j = 0; j < chars.length; j++) {
+            if (chars[j] == 49) {
+                booleans[j] = true;
+            } else {
+                booleans[j] = false;
+            }
+        }
+        return booleans;
+    }
+
+    public static int convertToInt(boolean[] booleans) {
+        int n = 0;
+        for (boolean b : booleans)
+            n = (n << 1) | (b ? 1 : 0);
+        return n;
+    }
+
+    public static void main(String[] args) {
+        boolean[] booleans = {true,true,true,true,true,true,true,true};
+        System.out.println(ConvertUtil.convertToInt(booleans));
+    }
+}

+ 265 - 0
warewms-hard/src/main/java/com/ruoyi/hard/xuankang/UnpackingMachineClient.java

@@ -1,11 +1,19 @@
 package com.ruoyi.hard.xuankang;
 
 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 lombok.extern.slf4j.Slf4j;
+import com.jwk.spring.boot.constant.DATA_TYPE_WRAPPER;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.stereotype.Service;
 
+import static com.ruoyi.hard.xuankang.UnpackingMachineClient.REGISTER_CODE.*;
+
+
+
 /**
  * 拆包机通讯客户端
  *
@@ -17,7 +25,264 @@ import org.springframework.stereotype.Service;
 @Service
 public class UnpackingMachineClient {
 
+    /**
+     * 拆包机
+     */
     @Autowired(required = false)
     @Qualifier("modbusTcpMasterTemplateSecond")
     private ModbusTcpMasterTemplate modbusTcpMasterTemplateSecond;
+    /**
+     * 拆包机命令
+     */
+    public enum REGISTER_CODE {
+        //寄存器VB128
+        REGISTER_CODE_128(1,127,1,0),
+        //寄存器VB129
+        REGISTER_CODE_129(1,128,1,0),
+        //寄存器VB130
+        REGISTER_CODE_130(1,129,1,0),
+        //寄存器VB131
+        REGISTER_CODE_131(1,130,1,0),
+        //寄存器VB132
+        REGISTER_CODE_132(1,131,1,0),
+        //寄存器VB133 皮带运行状态
+        REGISTER_CODE_133(1,132,1,0),
+        //寄存器VB134
+        REGISTER_CODE_134(1,133,1,0),
+        //寄存器VB135
+        REGISTER_CODE_135(1,134,1,0),
+        //寄存器VB136
+        REGISTER_CODE_136(1,135,1,0),
+        //寄存器VB137
+        REGISTER_CODE_137(1,136,1,0),
+        //寄存器VB138
+        REGISTER_CODE_138(1,137,1,0),
+        //寄存器VB139
+        REGISTER_CODE_139(1,138,1,0),
+        //寄存器VB140
+        REGISTER_CODE_140(1,139,1,0),
+        //寄存器VB141
+        REGISTER_CODE_141(1,140,1,0);
+        /**
+         * 从节点id
+         */
+        private int slaveId;
+        /**
+         * 地址
+         */
+        private int offset;
+        /**
+         * 读的长度
+         */
+        private int value0;
+        /**
+         * 写的值
+         */
+        private int value1;
+
+
+        REGISTER_CODE(int slaveId, int offset, int value0, int value1) {
+            this.slaveId = slaveId;
+            this.offset = offset;
+            this.value0 = value0;
+            this.value1 = value1;
+
+        }
+
+        public int getSlaveId() {return slaveId;}
+
+        public int getOffset() {
+            return offset;
+        }
+
+        public int getValue0() {return value0;}
+
+        public int getValue1() {
+            return value1;
+        }
+
+    }
+    /**
+     * 读拆包机1#各部件状态
+     *
+     * @return
+     */
+    public boolean[] readUnpacking01() {
+        Number number = read(REGISTER_CODE_128.getSlaveId(), REGISTER_CODE_128.getOffset());
+        boolean[] booleans = ConvertUtil.convertToBooleans(number);
+        return booleans;
+    }
+
+    /**
+     * 读拆包机2#各部件状态
+     *
+     * @return
+     */
+    public boolean[] readUnpacking02() {
+        Number number = read(REGISTER_CODE_129.getSlaveId(), REGISTER_CODE_129.getOffset());
+        boolean[] booleans = ConvertUtil.convertToBooleans(number);
+        return booleans;
+    }
+
+    /**
+     * 读拆包机3#各部件状态
+     *
+     * @return
+     */
+    public boolean[] readUnpacking03() {
+        Number number = read(REGISTER_CODE_130.getSlaveId(), REGISTER_CODE_130.getOffset());
+        boolean[] booleans = ConvertUtil.convertToBooleans(number);
+        return booleans;
+    }
+
+    /**
+     * 读拆包机4#各部件状态
+     *
+     * @return
+     */
+    public boolean[] readUnpacking04() {
+        Number number = read(REGISTER_CODE_131.getSlaveId(), REGISTER_CODE_131.getOffset());
+        boolean[] booleans = ConvertUtil.convertToBooleans(number);
+        return booleans;
+    }
+
+    /**
+     * 读拆包机5#各部件状态
+     *
+     * @return
+     */
+    public boolean[] readUnpacking05() {
+        Number number = read(REGISTER_CODE_132.getSlaveId(), REGISTER_CODE_132.getOffset());
+        boolean[] booleans = ConvertUtil.convertToBooleans(number);
+        return booleans;
+    }
+
+    /**
+     * 读皮带运行状态
+     *
+     * @return
+     */
+    public boolean[] readBelt() {
+        Number number = read(REGISTER_CODE_133.getSlaveId(), REGISTER_CODE_133.getOffset());
+        boolean[] booleans = ConvertUtil.convertToBooleans(number);
+        return booleans;
+    }
+
+//    /**
+//     * 拆包机1#启动
+//     * @return
+//     */
+//    public Boolean writeUnpacking01() {
+//
+//        return write(REGISTER_CODE_134.getSlaveId(), REGISTER_CODE_134.getOffset(), REGISTER_CODE_134.getValue1());
+//    }
+//
+//    /**
+//     * 写拆包机2#各部件状态
+//     * @return
+//     */
+//    public Boolean writeUnpacking02() {
+//        return write(REGISTER_CODE_135.getSlaveId(), REGISTER_CODE_135.getOffset(), REGISTER_CODE_135.getValue1());
+//    }
+//
+//    /**
+//     * 写拆包机3#各部件状态
+//     * @return
+//     */
+//    public Boolean writeUnpacking03() {
+//        return write(REGISTER_CODE_136.getSlaveId(), REGISTER_CODE_136.getOffset(), REGISTER_CODE_136.getValue1());
+//    }
+//
+//    /**
+//     * 写拆包机4#各部件状态
+//     * @return
+//     */
+//    public Boolean writeUnpacking04() {
+//        return write(REGISTER_CODE_137.getSlaveId(), REGISTER_CODE_137.getOffset(), REGISTER_CODE_137.getValue1());
+//    }
+//
+//    /**
+//     * 写拆包机5#各部件状态
+//     * @return
+//     */
+//    public Boolean writeUnpacking05() {
+//        return write(REGISTER_CODE_138.getSlaveId(), REGISTER_CODE_138.getOffset(), REGISTER_CODE_138.getValue1());
+//    }
+//
+//
+//    /**
+//     * 写皮带状态
+//     * @return
+//     */
+//    public Boolean writeBlet() {
+//        return write(REGISTER_CODE_139.getSlaveId(), REGISTER_CODE_139.getOffset(), REGISTER_CODE_139.getValue1());
+//    }
+
+    /**
+     * 读拆包机就地/远程 1# 2# 3#拆包机启动
+     * @return
+     */
+    public boolean[] readUnpackingRemoteInPlace01() {
+        Number number = read(REGISTER_CODE_140.getSlaveId(), REGISTER_CODE_140.getOffset());
+        boolean[] booleans = ConvertUtil.convertToBooleans(number);
+        return booleans;
+    }
+
+
+    /**
+     * 写拆包机就地/远程 1# 2# 3#拆包机启动
+     * @return
+     */
+    public Boolean writeUnpackingRemoteInPlace01(int value) {
+        return write(REGISTER_CODE_140.getSlaveId(), REGISTER_CODE_140.getOffset(), value);
+    }
+
+    /**
+     * 读4# 5#拆包机启动   1# 2# 3# 4# 5#拆包机停止
+     * @return
+     */
+    public boolean[] readUnpackingRemoteInPlace02() {
+        Number number = read(REGISTER_CODE_141.getSlaveId(), REGISTER_CODE_141.getOffset());
+        boolean[] booleans = ConvertUtil.convertToBooleans(number);
+        return booleans;
+    }
+
+    /**
+     * 写 4# 5#拆包机启动   1# 2# 3# 4# 5#拆包机停止
+     * @return
+     */
+    public Boolean writeUnpackingRemoteInPlace02(int value) {
+        return write(REGISTER_CODE_141.getSlaveId(), REGISTER_CODE_141.getOffset(), value);
+    }
+//    public boolean[] read1(){
+//        return read(1, 128, 1);
+//    }
+//    public boolean write1(){
+//        return write(1, 128, 7);
+//    }
+    /**
+     * 读的方法
+     * @param slaveId
+     * @param offset
+     * @return
+     */
+      private Number read (int slaveId,int offset) {
+            ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateSecond.getModbusMasterUtil();
+            Number number  = modbusMasterUtil.readHoldingRegister(slaveId,offset,DATA_TYPE_WRAPPER.TWO_BYTE_INT_UNSIGNED);
+//            boolean[] booleanData = readResponse.getBooleanData();
+//            return booleanData;
+          return number;
+        }
+
+    /**
+     * 写的方法
+     * @param slaveId
+     * @param offset
+     * @param value
+     * @return
+     */
+    private Boolean write(int slaveId,int offset,int value) {
+        ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateSecond.getModbusMasterUtil();
+        return modbusMasterUtil.writeHoldingRegisters(slaveId,offset,value);
+    }
 }

+ 162 - 0
warewms-hard/src/main/java/com/ruoyi/hard/xuankang/UnpackingMachineSubClient.java

@@ -0,0 +1,162 @@
+package com.ruoyi.hard.xuankang;
+
+import com.jwk.spring.boot.autoconfigure.ModbusTcpMasterTemplate;
+import com.jwk.spring.boot.constant.DATA_TYPE_WRAPPER;
+import com.jwk.spring.boot.modbus4j.ModbusMasterUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Service;
+
+import static com.ruoyi.hard.xuankang.UnpackingMachineClient.REGISTER_CODE.*;
+
+
+/**
+ * 拆包机通讯客户端
+ *
+ * @author JWK
+ * @version 1.0
+ * @date 2023/3/30 11:00
+ */
+@Slf4j
+@Service
+public class UnpackingMachineSubClient {
+    @Autowired
+    private UnpackingMachineClient unpackingMachineClient;
+
+    /**
+     * 1#拆包机就地/远程
+     *
+     * @param con
+     * @return
+     */
+    public boolean writeV140_0(boolean con) {
+        boolean[] booleans = unpackingMachineClient.readUnpackingRemoteInPlace01();
+        booleans[0] = con;
+        int i = ConvertUtil.convertToInt(booleans);
+        return unpackingMachineClient.writeUnpackingRemoteInPlace01(i);
+    }
+
+    /**
+     * 2#拆包机就地/远程
+     *
+     * @param con
+     * @return
+     */
+    public boolean writeV140_1(boolean con) {
+        boolean[] booleans = unpackingMachineClient.readUnpackingRemoteInPlace01();
+        booleans[1] = con;
+        int i = ConvertUtil.convertToInt(booleans);
+        return unpackingMachineClient.writeUnpackingRemoteInPlace01(i);
+    }
+
+    /**
+     * 3#拆包机就地/远程
+     *
+     * @param con
+     * @return
+     */
+    public boolean writeV140_2(boolean con) {
+        boolean[] booleans = unpackingMachineClient.readUnpackingRemoteInPlace01();
+        booleans[2] = con;
+        int i = ConvertUtil.convertToInt(booleans);
+        return unpackingMachineClient.writeUnpackingRemoteInPlace01(i);
+    }
+
+    /**
+     * 4#拆包机就地/远程
+     *
+     * @param con
+     * @return
+     */
+    public boolean writeV140_3(boolean con) {
+        boolean[] booleans = unpackingMachineClient.readUnpackingRemoteInPlace01();
+        booleans[3] = con;
+        int i = ConvertUtil.convertToInt(booleans);
+        return unpackingMachineClient.writeUnpackingRemoteInPlace01(i);
+    }
+
+    /**
+     * 5#拆包机就地/远程
+     *
+     * @param con
+     * @return
+     */
+    public boolean writeV140_4(boolean con) {
+        boolean[] booleans = unpackingMachineClient.readUnpackingRemoteInPlace01();
+        booleans[4] = con;
+        int i = ConvertUtil.convertToInt(booleans);
+        return unpackingMachineClient.writeUnpackingRemoteInPlace01(i);
+    }
+
+    /**
+     * 1#拆包机启动
+     *
+     * @param con
+     * @return
+     */
+    public boolean writeV140_5(boolean con) {
+        writeV140_0(false);
+        boolean[] booleans = unpackingMachineClient.readUnpackingRemoteInPlace01();
+        booleans[5] = con;
+        int i = ConvertUtil.convertToInt(booleans);
+        return unpackingMachineClient.writeUnpackingRemoteInPlace01(i);
+    }
+
+    /**
+     * 2#拆包机启动
+     *
+     * @param con
+     * @return
+     */
+    public boolean writeV140_6(boolean con) {
+        writeV140_1(false);
+        boolean[] booleans = unpackingMachineClient.readUnpackingRemoteInPlace01();
+        booleans[6] = con;
+        int i = ConvertUtil.convertToInt(booleans);
+        return unpackingMachineClient.writeUnpackingRemoteInPlace01(i);
+    }
+
+    /**
+     * 3#拆包机启动
+     *
+     * @param con
+     * @return
+     */
+    public boolean writeV140_7(boolean con) {
+        writeV140_0(false);
+        boolean[] booleans = unpackingMachineClient.readUnpackingRemoteInPlace01();
+        booleans[7] = con;
+        int i = ConvertUtil.convertToInt(booleans);
+        return unpackingMachineClient.writeUnpackingRemoteInPlace01(i);
+    }
+
+    /**
+     * 4#拆包机启动
+     *
+     * @param con
+     * @return
+     */
+    public boolean writeV141_0(boolean con) {
+        writeV140_3(false);
+        boolean[] booleans = unpackingMachineClient.readUnpackingRemoteInPlace02();
+        booleans[0] = con;
+        int i = ConvertUtil.convertToInt(booleans);
+        return unpackingMachineClient.writeUnpackingRemoteInPlace01(i);
+    }
+
+    /**
+     * 5#拆包机启动
+     *
+     * @param con
+     * @return
+     */
+    public boolean writeV141_1(boolean con) {
+        writeV140_4(false);
+        boolean[] booleans = unpackingMachineClient.readUnpackingRemoteInPlace02();
+        booleans[1] = con;
+        int i = ConvertUtil.convertToInt(booleans);
+        return unpackingMachineClient.writeUnpackingRemoteInPlace01(i);
+    }
+
+}