Sfoglia il codice sorgente

与西门子S1500进行通讯的工具类

LZH 1 anno fa
parent
commit
a18cf601a6
1 ha cambiato i file con 138 aggiunte e 0 eliminazioni
  1. 138 0
      warewms-hard/src/main/java/com/ruoyi/hard/plc/HslTools.java

+ 138 - 0
warewms-hard/src/main/java/com/ruoyi/hard/plc/HslTools.java

@@ -0,0 +1,138 @@
+package com.ruoyi.hard.plc;
+
+import HslCommunication.Core.Types.OperateResult;
+import HslCommunication.Core.Types.OperateResultExOne;
+import HslCommunication.Profinet.Siemens.SiemensPLCS;
+import HslCommunication.Profinet.Siemens.SiemensS7Net;
+import com.ruoyi.common.exception.ServiceException;
+import lombok.extern.slf4j.Slf4j;
+
+import java.nio.charset.StandardCharsets;
+
+@Slf4j
+public class HslTools {
+
+    private SiemensS7Net siemensS7Net;
+    private String ip;
+
+    private HslTools(String ip) {
+        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500, ip);
+        OperateResult operateResult = siemensS7Net.ConnectServer();
+        if(operateResult.IsSuccess)
+        {
+            log.info("连接成功");
+        }
+        else
+        {
+            log.info("连接失败");
+        }
+        this.ip = ip;
+    }
+
+    private static final HslTools hslTools= new HslTools("10.95.8.66");
+
+    //静态工厂方法
+    public static HslTools getInstance() {
+        return hslTools;
+    }
+
+
+    public OperateResult closeConn() {
+        OperateResult result = OperateResult.CreateSuccessResult();
+        try {
+            result = siemensS7Net.ConnectClose();
+        } catch (Exception e) {
+
+        }
+        siemensS7Net = null;
+        return result;
+    }
+
+    public boolean writeInt(String address, Integer value) {
+        OperateResult result = siemensS7Net.Write(address, value);
+        log.info(result.Message);
+        if (!result.IsSuccess) {
+            closeConn();
+            throw new ServiceException(String.format("写入数据失败,db:%s,数据:%d,错误信息:%s", address, value, result.Message));
+        }
+        return result.IsSuccess;
+    }
+
+    public boolean writeShort(String address, short value) {
+        OperateResult result = siemensS7Net.Write(address, value);
+        log.info(result.Message);
+        if (!result.IsSuccess) {
+            closeConn();
+            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
+        } else {
+            log.info(String.format("写入数据完成,db:%s,数据:%d", address, value));
+        }
+        return result.IsSuccess;
+    }
+
+    public boolean writeBoolean(String address, Boolean value) {
+        OperateResult result = siemensS7Net.Write(address, value);
+        if (!result.IsSuccess) {
+            closeConn();
+            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, address, result.Message));
+        } else {
+            log.info(String.format("写入数据完成,db:%s,数据:%b", address, value));
+        }
+        return result.IsSuccess;
+    }
+
+    public boolean writeString(String address, String value) {
+        OperateResult result = siemensS7Net.Write(address, value.getBytes(StandardCharsets.US_ASCII));
+        if (!result.IsSuccess) {
+            closeConn();
+            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
+        } else {
+            log.info(String.format("写入数据完成,db:%s,数据:%s", address, value));
+        }
+        return result.IsSuccess;
+    }
+
+    public boolean writeStringStr(String address, String value) {
+        OperateResult result = siemensS7Net.Write(address, value);
+        if (!result.IsSuccess) {
+            closeConn();
+            throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
+        } else {
+            log.info(String.format("写入数据完成,db:%s,数据:%s", address, value));
+        }
+        return result.IsSuccess;
+    }
+
+    public String getString(String address) {
+        OperateResultExOne<String> result = siemensS7Net.ReadString(address);
+        if (!result.IsSuccess) {
+            closeConn();
+            log.error(String.format(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message)));
+            return "";
+        } else {
+            return result.Content;
+        }
+    }
+
+    public boolean getBool(String address) {
+        OperateResultExOne<Boolean> result = siemensS7Net.ReadBool(address);
+        if (!result.IsSuccess) {
+            closeConn();
+            log.error(String.format(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message)));
+            return false;
+        } else {
+            return result.Content;
+        }
+    }
+
+    public int getInt(String address) {
+        OperateResultExOne<Integer> result = siemensS7Net.ReadInt32(address);
+        if (!result.IsSuccess) {
+            closeConn();
+            log.error(String.format(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message)));
+            return 0;
+        } else {
+            return result.Content;
+        }
+    }
+}