From e36b74525f9c7400da2d3438c5e4164622da059a Mon Sep 17 00:00:00 2001
From: wuyouming666 <2265557248@qq.com>
Date: 星期三, 24 一月 2024 13:14:21 +0800
Subject: [PATCH] 封装初始化方法为工具类,方便调用, getPlcParameterValues getPlcBitValues 方法codeid 按照传入参数的顺序遍历 来获取值

---
 springboot-vue3/src/main/java/com/example/springboot/component/S7control.java |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/springboot-vue3/src/main/java/com/example/springboot/component/S7control.java b/springboot-vue3/src/main/java/com/example/springboot/component/S7control.java
index a5f3e44..ce4f674 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/component/S7control.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/component/S7control.java
@@ -37,13 +37,21 @@
         }
         return instance;
     }
-
+    
     /**
      * 鍏抽棴瑗块棬瀛恠7閫氳杩炴帴
      */
     public void CloseS7client() {
         if (s7PLC == null)
             s7PLC.close();
+            s7PLC.checkConnected();
+    }
+
+    /**
+     * s7閫氳杩炴帴鐘舵��
+     */
+    public boolean CheckConnected() {
+        return s7PLC.checkConnected();
     }
 
     /**
@@ -215,7 +223,15 @@
         if (s7PLC==null)
              return null;
        // List<String> addresslist = GetAddressList(address, count, 16);
-        return s7PLC.readByte(address,count);
+
+        try {
+            return s7PLC.readByte(address, count);
+        }catch (Exception e) {
+            // 澶勭悊寮傚父
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+            return null;
+        }
+
     }
 
     /**
@@ -271,6 +287,41 @@
 
     return result;
 }
+
+
+
+
+
+    public List<String> readStringsandword(List<String> addressList) {
+        if (s7PLC == null) {
+            return null;
+        }
+        List<String> result = new ArrayList<>();
+        for (String address : addressList) {
+            try {
+                if (address.contains("-")) {
+                    address = address.substring(0, address.indexOf("-"));
+
+                    byte[] bytes = s7PLC.readByte(address, 14);
+                    if (bytes != null) {
+                        String str = new String(bytes, StandardCharsets.UTF_8);
+                        result.add(str);
+                    }
+
+                } else {
+                    Short value = s7PLC.readInt16(address);
+                    result.add(value.toString());
+
+                }
+            } catch (Exception e) {
+                System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+                result.add(null);
+            }
+        }
+
+        return result;
+    }
+
 
 
 //涓嶈繛缁湴鍧�鍐欏叆Word
@@ -337,7 +388,7 @@
     try {
         return s7PLC.readTime(address);
     } catch (Exception e) {
-        e.printStackTrace();
+        System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
         return null;
     }
 }

--
Gitblit v1.8.0