Browse Source

1.镍盐-称重机和光电信号ip和端口配置
2.镍盐-读称重机重量方法
3.镍盐-读光电信号方法

HuKang 1 year ago
parent
commit
87b89790c4

+ 22 - 5
ruoyi-admin/src/main/java/com/ruoyi/web/controller/warewms/test/ModbusTestController.java

@@ -1,21 +1,38 @@
 package com.ruoyi.web.controller.warewms.test;
 
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.hard.modbus.tcp.ModbusTcpWeigherClient;
 import io.swagger.annotations.Api;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Map;
 
 /**
  * @author JWK
  * @version 1.0
  * @date 2022/11/14 14:52
  */
-@Api("Modbus测试")
+@Api("Modbus称重机光电测试")
 @RestController
 @RequestMapping("/api/modbus")
 public class ModbusTestController {
 
-    public static void main(String[] args) {
-        System.out.println("test");
+    @Autowired(required = false)
+    private ModbusTcpWeigherClient modbusTcpWeigherClient;
+
+
+    @ApiOperation("称重机读")
+    @RequestMapping(value = "/read", method = RequestMethod.POST)
+    @ResponseBody
+    public AjaxResult read(Integer slaveId, Integer offset) throws Exception {
+        if (modbusTcpWeigherClient == null) {
+            return AjaxResult.error("称重机关闭!");
+        }
+        Number number = modbusTcpWeigherClient.read(slaveId,offset);
+        int i = number.intValue();
+        return AjaxResult.success("拿到数据:" + i);
     }
 
 }

+ 7 - 0
ruoyi-admin/src/main/resources/application-prod.yml

@@ -67,3 +67,10 @@ init-task:
     autosend: true # 自动下发任务
     aciservice: true # ndc通讯
 
+modbus:
+    tcp-master:
+        first: # 称重机
+            open: true
+            host: 192.168.100.161
+            port: 502
+

+ 1 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java

@@ -115,6 +115,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
                 .antMatchers("/wcs/**").anonymous()
                 .antMatchers("/api/wcstask/**").permitAll()
                 .antMatchers("/test/**").permitAll()
+                .antMatchers("/api/modbus/**").permitAll()
                 // 库位视图
                 .antMatchers("/ams/locationView/**").permitAll()
                 // 除上面外的所有请求全部需要鉴权认证

+ 96 - 0
warewms-hard/src/main/java/com/ruoyi/hard/modbus/tcp/ModbusTcpWeigherClient.java

@@ -0,0 +1,96 @@
+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 lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Service;
+
+import java.math.BigDecimal;
+
+/**
+ * @author Jwk
+ * @version 1.0
+ * @date 2023/6/1 12:31
+ */
+@Slf4j
+@Service
+public class ModbusTcpWeigherClient {
+
+    /**
+     * 称重机光电信号
+     */
+    @Autowired(required = false)
+    @Qualifier("modbusTcpMasterTemplateFirst")
+    private ModbusTcpMasterTemplate modbusTcpMasterTemplateFirst;
+
+    /**
+     * 读称重机和光电信号
+     *
+     * @param slaveId
+     * @param offset
+     * @return
+     */
+    public Number read(int slaveId, int offset) {
+        ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
+        return modbusMasterUtil.readHoldingRegister(slaveId,offset, DATA_TYPE_WRAPPER.TWO_BYTE_INT_UNSIGNED);
+    }
+
+
+    /**
+     * 读光电1信号
+     *
+     * @param slaveId
+     * @param offset
+     * @return
+     */
+    public boolean readPhotoelectricity01(int slaveId, int offset) {
+        ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
+        Number number = modbusMasterUtil.readHoldingRegister(1, 0, DATA_TYPE_WRAPPER.TWO_BYTE_INT_UNSIGNED);
+        int i = number.intValue();
+        return i == 1;
+    }
+
+    /**
+     *读光电2信号
+     *
+     * @param slaveId
+     * @param offset
+     * @return
+     */
+    public boolean readPhotoelectricity02(int slaveId, int offset) {
+        ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
+        Number number = modbusMasterUtil.readHoldingRegister(1, 1, DATA_TYPE_WRAPPER.TWO_BYTE_INT_UNSIGNED);
+        int i = number.intValue();
+        return i == 1;
+    }
+
+    /**
+     * 读光电3信号
+     *
+     * @param slaveId
+     * @param offset
+     * @return
+     */
+    public boolean readPhotoelectricity03(int slaveId, int offset) {
+        ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
+        Number number = modbusMasterUtil.readHoldingRegister(1, 2, DATA_TYPE_WRAPPER.TWO_BYTE_INT_UNSIGNED);
+        int i = number.intValue();
+        return i == 1;
+    }
+
+    /**
+     * 读称重机上货物重量
+     *
+     * @param slaveId
+     * @param offset
+     * @return
+     */
+    public int readCharge(int slaveId, int offset) {
+        ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
+        return modbusMasterUtil.readHoldingRegister(1, 3, DATA_TYPE_WRAPPER.TWO_BYTE_INT_UNSIGNED).intValue();
+    }
+}