Selaa lähdekoodia

1.镍盐-按钮盒ip和端口配置
2.镍盐-按钮盒通讯方法
3.镍盐-按钮盒测试接口

HuKang 1 vuosi sitten
vanhempi
commit
3811e285e4

+ 18 - 17
ruoyi-admin/src/main/java/com/ruoyi/web/controller/warewms/test/ButtonBoxTestController.java

@@ -4,7 +4,6 @@ import com.ruoyi.hard.modbus.tcp.ButtonBoxClient;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -25,23 +24,25 @@ public class ButtonBoxTestController {
     /**
      * 读按钮盒01是否被按下
      *
+     * @param btnNo
      * @return
      */
     @ApiOperation("读按钮盒01是否被按下")
     @PostMapping("/readBtn01")
-    public Boolean readBtn01() {
-        return buttonBoxClient.readBtn01();
+    public Boolean readBtn01(String btnNo) {
+        return buttonBoxClient.readBtn01(ButtonBoxClient.BUTTON_BOX_NO.getByValue(btnNo));
     }
 
     /**
      * 读按钮盒02是否被按下
      *
+     * @param btnNo
      * @return
      */
     @ApiOperation("读按钮盒02是否被按下")
     @PostMapping("/readBtn02")
-    public Boolean readBtn02() {
-        return buttonBoxClient.readBtn02();
+    public Boolean readBtn02(String btnNo) {
+        return buttonBoxClient.readBtn02(ButtonBoxClient.BUTTON_BOX_NO.getByValue(btnNo));
     }
 
     /**
@@ -51,8 +52,8 @@ public class ButtonBoxTestController {
      */
     @ApiOperation("清除钮盒01按下标记")
     @PostMapping("/clearBtn01")
-    public Boolean clearBtn01() {
-        return buttonBoxClient.clearBtn01();
+    public Boolean clearBtn01(String btnNo) {
+        return buttonBoxClient.clearBtn01(ButtonBoxClient.BUTTON_BOX_NO.getByValue(btnNo));
     }
 
     /**
@@ -62,8 +63,8 @@ public class ButtonBoxTestController {
      */
     @ApiOperation("清除钮盒02按下标记")
     @PostMapping("/clearBtn02")
-    public Boolean clearBtn02() {
-        return buttonBoxClient.clearBtn02();
+    public Boolean clearBtn02(String btnNo) {
+        return buttonBoxClient.clearBtn02(ButtonBoxClient.BUTTON_BOX_NO.getByValue(btnNo));
     }
 
     /**
@@ -73,8 +74,8 @@ public class ButtonBoxTestController {
      */
     @ApiOperation("写钮盒01灭灯")
     @PostMapping("/writeLamp01Death")
-    public Boolean writeLamp01Death() {
-        return buttonBoxClient.writeLamp01Death();
+    public Boolean writeLamp01Death(String btnNo) {
+        return buttonBoxClient.writeLamp01Death(ButtonBoxClient.BUTTON_BOX_NO.getByValue(btnNo));
     }
 
     /**
@@ -84,8 +85,8 @@ public class ButtonBoxTestController {
      */
     @ApiOperation("写钮盒02灭灯")
     @PostMapping("/writeLamp02Death")
-    public Boolean writeLamp02Death() {
-        return buttonBoxClient.writeLamp02Death();
+    public Boolean writeLamp02Death(String btnNo) {
+        return buttonBoxClient.writeLamp02Death(ButtonBoxClient.BUTTON_BOX_NO.getByValue(btnNo));
     }
 
     /**
@@ -95,8 +96,8 @@ public class ButtonBoxTestController {
      */
     @ApiOperation("写钮盒01亮灯")
     @PostMapping("/writeLamp01Bright")
-    public Boolean writeLamp01Bright() {
-        return buttonBoxClient.writeLamp01Bright();
+    public Boolean writeLamp01Bright(String btnNo) {
+        return buttonBoxClient.writeLamp01Bright(ButtonBoxClient.BUTTON_BOX_NO.getByValue(btnNo));
     }
 
     /**
@@ -106,8 +107,8 @@ public class ButtonBoxTestController {
      */
     @ApiOperation("写钮盒02亮灯")
     @PostMapping("/writeLamp02Bright")
-    public Boolean writeLamp02Bright() {
-        return buttonBoxClient.writeLamp02Bright();
+    public Boolean writeLamp02Bright(String btnNo) {
+        return buttonBoxClient.writeLamp02Bright(ButtonBoxClient.BUTTON_BOX_NO.getByValue(btnNo));
     }
 
 }

+ 16 - 1
ruoyi-admin/src/main/resources/application-prod.yml

@@ -73,4 +73,19 @@ modbus:
             open: true
             host: 192.168.100.161
             port: 502
-
+        second: # 按钮盒
+            open: true
+            host: 192.168.100.151
+            port: 502
+        third: # 按钮盒
+            open: true
+            host: 192.168.100.152
+            port: 502
+        fourth: # 按钮盒
+            open: true
+            host: 192.168.100.153
+            port: 502
+        fifth: # 按钮盒
+            open: true
+            host: 192.168.100.154
+            port: 502

+ 64 - 64
warewms-ams/src/main/java/com/ruoyi/ams/agv/ndc/thread/AutoButtonBoxTask.java

@@ -30,81 +30,81 @@ public class AutoButtonBoxTask {
      * 3.任务生成成功,按钮盒灭灯,并且清除按下标记
      */
     public void run() {
-        Boolean readBtn01 = buttonBoxClient.readBtn01();
-        Boolean readBtn02 = buttonBoxClient.readBtn02();
-        if (readBtn01) {
-            buttonTask01();
-        }
-        if (readBtn02) {
-            buttonTask02();
-        }
+//        Boolean readBtn01 = buttonBoxClient.readBtn01();
+//        Boolean readBtn02 = buttonBoxClient.readBtn02();
+//        if (readBtn01) {
+//            buttonTask01();
+//        }
+//        if (readBtn02) {
+//            buttonTask02();
+//        }
     }
 
     /**
      * 按钮1业务(冲边任务)
      */
     public void buttonTask01() {
-        log.info("按钮1已经被按下");
-        // 下发模具搬运任务(冲边)
-        AgvCallDTO agvCallDTO = new AgvCallDTO();
-        agvCallDTO.setWarehouseId(Constant.WAREHOUSE_ID);
-        agvCallDTO.setLocationFrom("");
-        List<AgvCallItemDTO> agvCallItemDTOList = new ArrayList<>();
-        AgvCallItemDTO agvCallItemDTO = new AgvCallItemDTO();
-        agvCallItemDTO.setSku("冲边模具");
-        LotattDTO lotattDTO = new LotattDTO();
-        lotattDTO.setLotatt01("冲边模具");
-        agvCallItemDTO.setLotattDTO(lotattDTO);
-        agvCallItemDTOList.add(agvCallItemDTO);
-        agvCallDTO.setAgvCallItemDTOList(agvCallItemDTOList);
-        AjaxResult ajaxResult = businessService.agvCall(1l, agvCallDTO);
-        if (!ajaxResult.isSuccess()) {
-            log.error("下发模具搬运任务(冲边)失败:" + ajaxResult.getMsg());
-            return;
-        }
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
-        // 清除按下标记
-        buttonBoxClient.clearBtn01();
-        // 灭灯
-        buttonBoxClient.writeLamp01Death();
-        log.info("按钮1已经灭灯");
+//        log.info("按钮1已经被按下");
+//        // 下发模具搬运任务(冲边)
+//        AgvCallDTO agvCallDTO = new AgvCallDTO();
+//        agvCallDTO.setWarehouseId(Constant.WAREHOUSE_ID);
+//        agvCallDTO.setLocationFrom("");
+//        List<AgvCallItemDTO> agvCallItemDTOList = new ArrayList<>();
+//        AgvCallItemDTO agvCallItemDTO = new AgvCallItemDTO();
+//        agvCallItemDTO.setSku("冲边模具");
+//        LotattDTO lotattDTO = new LotattDTO();
+//        lotattDTO.setLotatt01("冲边模具");
+//        agvCallItemDTO.setLotattDTO(lotattDTO);
+//        agvCallItemDTOList.add(agvCallItemDTO);
+//        agvCallDTO.setAgvCallItemDTOList(agvCallItemDTOList);
+//        AjaxResult ajaxResult = businessService.agvCall(1l, agvCallDTO);
+//        if (!ajaxResult.isSuccess()) {
+//            log.error("下发模具搬运任务(冲边)失败:" + ajaxResult.getMsg());
+//            return;
+//        }
+//        try {
+//            Thread.sleep(10000);
+//        } catch (InterruptedException e) {
+//            e.printStackTrace();
+//        }
+//        // 清除按下标记
+//        buttonBoxClient.clearBtn01();
+//        // 灭灯
+//        buttonBoxClient.writeLamp01Death();
+//        log.info("按钮1已经灭灯");
     }
 
     /**
      * 按钮2业务(硫化任务)
      */
     public void buttonTask02() {
-        log.info("按钮2已经被按下");
-        // 下发模具搬运任务(硫化)
-        AgvCallDTO agvCallDTO = new AgvCallDTO();
-        agvCallDTO.setWarehouseId(Constant.WAREHOUSE_ID);
-        agvCallDTO.setLocationFrom("");
-        List<AgvCallItemDTO> agvCallItemDTOList = new ArrayList<>();
-        AgvCallItemDTO agvCallItemDTO = new AgvCallItemDTO();
-        agvCallItemDTO.setSku("硫化模具");
-        LotattDTO lotattDTO = new LotattDTO();
-        lotattDTO.setLotatt01("硫化模具");
-        agvCallItemDTO.setLotattDTO(lotattDTO);
-        agvCallItemDTOList.add(agvCallItemDTO);
-        agvCallDTO.setAgvCallItemDTOList(agvCallItemDTOList);
-        AjaxResult ajaxResult = businessService.agvCall(1l, agvCallDTO);
-        if (!ajaxResult.isSuccess()) {
-            log.error("下发模具搬运任务(硫化)失败:" + ajaxResult.getMsg());
-            return;
-        }
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
-        // 清除按下标记
-        buttonBoxClient.clearBtn02();
-        // 灭灯
-        buttonBoxClient.writeLamp02Death();
-        log.info("按钮2已经灭灯");
+//        log.info("按钮2已经被按下");
+//        // 下发模具搬运任务(硫化)
+//        AgvCallDTO agvCallDTO = new AgvCallDTO();
+//        agvCallDTO.setWarehouseId(Constant.WAREHOUSE_ID);
+//        agvCallDTO.setLocationFrom("");
+//        List<AgvCallItemDTO> agvCallItemDTOList = new ArrayList<>();
+//        AgvCallItemDTO agvCallItemDTO = new AgvCallItemDTO();
+//        agvCallItemDTO.setSku("硫化模具");
+//        LotattDTO lotattDTO = new LotattDTO();
+//        lotattDTO.setLotatt01("硫化模具");
+//        agvCallItemDTO.setLotattDTO(lotattDTO);
+//        agvCallItemDTOList.add(agvCallItemDTO);
+//        agvCallDTO.setAgvCallItemDTOList(agvCallItemDTOList);
+//        AjaxResult ajaxResult = businessService.agvCall(1l, agvCallDTO);
+//        if (!ajaxResult.isSuccess()) {
+//            log.error("下发模具搬运任务(硫化)失败:" + ajaxResult.getMsg());
+//            return;
+//        }
+//        try {
+//            Thread.sleep(10000);
+//        } catch (InterruptedException e) {
+//            e.printStackTrace();
+//        }
+//        // 清除按下标记
+//        buttonBoxClient.clearBtn02();
+//        // 灭灯
+//        buttonBoxClient.writeLamp02Death();
+//        log.info("按钮2已经灭灯");
     }
 }

+ 115 - 21
warewms-hard/src/main/java/com/ruoyi/hard/modbus/tcp/ButtonBoxClient.java

@@ -2,11 +2,16 @@ package com.ruoyi.hard.modbus.tcp;
 
 import com.jwk.spring.boot.autoconfigure.ModbusTcpMasterTemplate;
 import com.jwk.spring.boot.modbus4j.ModbusMasterUtil;
+import com.ruoyi.common.core.domain.AjaxResult;
 import com.serotonin.modbus4j.msg.ReadResponse;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.PostConstruct;
+import java.util.HashMap;
+import java.util.Map;
+
 import static com.ruoyi.hard.modbus.tcp.ButtonBoxClient.BUTTON_BOX_CODE.*;
 
 
@@ -21,12 +26,95 @@ import static com.ruoyi.hard.modbus.tcp.ButtonBoxClient.BUTTON_BOX_CODE.*;
 public class ButtonBoxClient {
 
     /**
-     * 按钮盒
+     * 按钮盒1
+     */
+    @Autowired(required = false)
+    @Qualifier("modbusTcpMasterTemplateSecond")
+    private ModbusTcpMasterTemplate modbusTcpMasterTemplateSecond;
+
+    /**
+     * 按钮盒2
      */
     @Autowired(required = false)
     @Qualifier("modbusTcpMasterTemplateThird")
     private ModbusTcpMasterTemplate modbusTcpMasterTemplateThird;
 
+    /**
+     * 按钮盒3
+     */
+    @Autowired(required = false)
+    @Qualifier("modbusTcpMasterTemplateFourth")
+    private ModbusTcpMasterTemplate modbusTcpMasterTemplateFourth;
+
+    /**
+     * 按钮盒4
+     */
+    @Autowired(required = false)
+    @Qualifier("modbusTcpMasterTemplateFifth")
+    private ModbusTcpMasterTemplate modbusTcpMasterTemplateFifth;
+
+    private Map<String, ModbusTcpMasterTemplate> tcpMasterTemplateMap;
+
+    @PostConstruct
+    public void initMap() {
+        tcpMasterTemplateMap = new HashMap<>();
+        tcpMasterTemplateMap.put(ButtonBoxClient.BUTTON_BOX_NO.A.name(), modbusTcpMasterTemplateSecond);
+        tcpMasterTemplateMap.put(ButtonBoxClient.BUTTON_BOX_NO.B.name(), modbusTcpMasterTemplateThird);
+        tcpMasterTemplateMap.put(ButtonBoxClient.BUTTON_BOX_NO.C.name(), modbusTcpMasterTemplateFourth);
+        tcpMasterTemplateMap.put(ButtonBoxClient.BUTTON_BOX_NO.D.name(), modbusTcpMasterTemplateFifth);
+    }
+
+    public ModbusMasterUtil getModbusMasterUtil(BUTTON_BOX_NO boxNo) {
+        return tcpMasterTemplateMap.get(boxNo.name()).getModbusMasterUtil();
+    }
+
+    /**
+     * 按钮盒编号
+     */
+    public enum BUTTON_BOX_NO {
+        /***
+         * 按钮盒1
+         */
+        A("1"),
+        /***
+         * 按钮盒2
+         */
+        B("2"),
+        /***
+         * 按钮盒3
+         */
+        C("3"),
+        /***
+         * 按钮盒4
+         */
+        D("4");
+
+        private String value;
+
+        BUTTON_BOX_NO(String value) {
+            this.value = value;
+        }
+
+        public String getValue() {
+            return value;
+        }
+
+        /**
+         * 根据值获得枚举类型 switch
+         *
+         * @param value
+         * @return
+         */
+        public static ButtonBoxClient.BUTTON_BOX_NO getByValue(String value) {
+            for (ButtonBoxClient.BUTTON_BOX_NO code : values()) {
+                if (code.getValue().equals(value)) {
+                    return code;
+                }
+            }
+            return null;
+        }
+    }
+
     /**
      * 按钮盒命令
      */
@@ -114,8 +202,9 @@ public class ButtonBoxClient {
      *
      * @return true 代表按钮被按下
      */
-    public Boolean readBtn01() {
-        short i = read(BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue());
+    public Boolean readBtn01(BUTTON_BOX_NO boxNo) {
+        ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
+        short i = read(modbusMasterUtil,BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue());
         return i == 1;
     }
 
@@ -124,8 +213,9 @@ public class ButtonBoxClient {
      *
      * @return true 代表按钮被按下
      */
-    public Boolean readBtn02() {
-        short i = read(BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue());
+    public Boolean readBtn02(BUTTON_BOX_NO boxNo) {
+        ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
+        short i = read(modbusMasterUtil,BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue());
         return i == 1;
     }
 
@@ -134,8 +224,9 @@ public class ButtonBoxClient {
      *
      * @return true
      */
-    public Boolean clearBtn01() {
-        return write(BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue1());
+    public Boolean clearBtn01(BUTTON_BOX_NO boxNo) {
+        ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
+        return write(modbusMasterUtil,BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue1());
     }
 
     /**
@@ -143,8 +234,9 @@ public class ButtonBoxClient {
      *
      * @return true
      */
-    public Boolean clearBtn02() {
-        return write(BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue1());
+    public Boolean clearBtn02(BUTTON_BOX_NO boxNo) {
+        ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
+        return write(modbusMasterUtil,BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue1());
     }
 
     /**
@@ -152,8 +244,9 @@ public class ButtonBoxClient {
      *
      * @return true
      */
-    public Boolean writeLamp01Bright() {
-        return write(LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue2());
+    public Boolean writeLamp01Bright(BUTTON_BOX_NO boxNo) {
+        ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
+        return write(modbusMasterUtil,LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue2());
     }
 
     /**
@@ -161,8 +254,9 @@ public class ButtonBoxClient {
      *
      * @return true
      */
-    public Boolean writeLamp02Bright() {
-        return write(LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue2());
+    public Boolean writeLamp02Bright(BUTTON_BOX_NO boxNo) {
+        ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
+        return write(modbusMasterUtil,LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue2());
     }
 
     /**
@@ -170,8 +264,9 @@ public class ButtonBoxClient {
      *
      * @return true
      */
-    public Boolean writeLamp01Death() {
-        return write(LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue1());
+    public Boolean writeLamp01Death(BUTTON_BOX_NO boxNo) {
+        ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
+        return write(modbusMasterUtil,LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue1());
     }
 
     /**
@@ -179,20 +274,19 @@ public class ButtonBoxClient {
      *
      * @return true
      */
-    public Boolean writeLamp02Death() {
-        return write(LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue1());
+    public Boolean writeLamp02Death(BUTTON_BOX_NO boxNo) {
+        ModbusMasterUtil modbusMasterUtil = getModbusMasterUtil(boxNo);
+        return write(modbusMasterUtil,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();
+    private short read(ModbusMasterUtil modbusMasterUtil,int slaveId, int offset, int len) {
         ReadResponse readResponse = modbusMasterUtil.readHoldingRegisters(slaveId, offset, len);
         short[] shortData = readResponse.getShortData();
         return shortData[0];
     }
 
 
-    private Boolean write(int slaveId, int offset, int value) {
-        ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil();
+    private Boolean write(ModbusMasterUtil modbusMasterUtil,int slaveId, int offset, int value) {
         return modbusMasterUtil.writeHoldingRegisters(slaveId, offset, value);
     }