From 9a0dd8244282d90b08b16d2c239683bbac62fab2 Mon Sep 17 00:00:00 2001
From: ZengTao <2773468879@qq.com>
Date: 星期二, 16 一月 2024 15:02:21 +0800
Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/CanadaMes

---
 springboot-vue3/src/main/java/com/example/springboot/component/S7control.java |  294 ++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 202 insertions(+), 92 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 e207db3..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
@@ -1,11 +1,17 @@
 package com.example.springboot.component;
 
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.sql.Time;
 import java.util.ArrayList;
 import java.util.List;
 
 import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
 import com.github.xingshuangs.iot.protocol.s7.service.MultiAddressWrite;
 import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
+import com.google.common.primitives.Bytes;
+import org.apache.ibatis.jdbc.Null;
 
 public class S7control {
 
@@ -31,13 +37,21 @@
         }
         return instance;
     }
-
+    
     /**
      * 鍏抽棴瑗块棬瀛恠7閫氳杩炴帴
      */
     public void CloseS7client() {
         if (s7PLC == null)
             s7PLC.close();
+            s7PLC.checkConnected();
+    }
+
+    /**
+     * s7閫氳杩炴帴鐘舵��
+     */
+    public boolean CheckConnected() {
+        return s7PLC.checkConnected();
     }
 
     /**
@@ -78,35 +92,7 @@
      * @param address 鍦板潃
      * @param datas   word鐨勫��
      */
-    public void WriteWord(List<String> address, List<Short> datas) {
-        if (s7PLC == null)
-            return;
 
-        for (int i = 0; i < address.size(); i++) {
-            String addr = address.get(i);
-            short data = datas.get(i);
-
-            if (addr.contains("-")) {
-                // 澶勭悊鑼冨洿鍦板潃
-                String[] range = addr.split("-");
-                if (range.length == 2) {
-                    String startAddr = range[0].trim();
-                    String endAddr = range[1].trim();
-
-                    int startIndex = Integer.parseInt(startAddr.substring(startAddr.indexOf('.') + 1));
-                    int endIndex = Integer.parseInt(endAddr.substring(endAddr.indexOf('.') + 1));
-
-                    for (int j = startIndex; j <= endIndex; j++) {
-                        String currentAddress = startAddr.substring(0, startAddr.indexOf('.') + 1) + j;
-                        s7PLC.writeInt16(currentAddress, data); // 灏嗘暟鎹啓鍏ュ綋鍓嶅湴鍧�
-                    }
-                }
-            } else {
-                // 澶勭悊鍗曚釜鍦板潃
-                s7PLC.writeInt16(addr, data); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧�
-            }
-        }
-    }
 
 
 
@@ -179,48 +165,19 @@
      * @return 缁撴灉
      */
     public List<Short> ReadWord(List<String> address) {
-        if (s7PLC==null)
-              return null;
-        return s7PLC.readInt16(address);
-    }
+        if (s7PLC == null)
+            return null;
 
-    public List<Short> readWords(List<String> addresses) {
-        if (s7PLC == null) {
+        try {
+            return s7PLC.readInt16(address);
+        } catch (Exception e) {
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
             return null;
         }
-
-        List<Short> data = new ArrayList<>();
-
-        for (String address : addresses) {
-            if (address.contains("-")) {
-                String[] range = address.split("-");
-                String startAddress = range[0];
-                String endAddress = range[1];
-
-                if (startAddress.equals(endAddress)) {
-                    // 鍗曚釜鍦板潃
-                    Short value = s7PLC.readInt16(startAddress);
-                    data.add(value);
-                } else {
-                    // 鑼冨洿鍦板潃
-                    int startIndex = getIndexFromAddress(startAddress);
-                    int endIndex = getIndexFromAddress(endAddress);
-
-                    for (int i = startIndex; i <= endIndex; i++) {
-                        String currentAddress = getAddressFromIndex(i);
-                        Short value = s7PLC.readInt16(currentAddress);
-                        data.add(value);
-                    }
-                }
-            } else {
-                // 鍗曚釜鍦板潃
-                Short value = s7PLC.readInt16(address);
-                data.add(value);
-            }
-        }
-
-        return data;
     }
+
+
+
 
     private int getIndexFromAddress(String address) {
 
@@ -242,12 +199,19 @@
      * @return 缁撴灉
      */
     public List<Short> ReadWord(String address, int count) {
-        if (s7PLC==null)
-             return null;
+        if (s7PLC == null)
+            return null;
 
         List<String> addresslist = GetAddressList(address, count, 16);
-        return s7PLC.readInt16(addresslist);
+        try {
+            return s7PLC.readInt16(addresslist);
+        } catch (Exception e) {
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+
+            return null;
+        }
     }
+
     /**
      * 鎸夋寚瀹氱殑鍦板潃 璇诲彇byte缁撴灉闆�
      * 
@@ -258,9 +222,16 @@
     public byte[] ReadByte(String address, int count) {
         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;
+        }
+
     }
 
     /**
@@ -274,35 +245,165 @@
              return null;
         return s7PLC.readBoolean(addresslist);
     }
+//璇诲彇涓嶈繛缁湴鍧�bit
+public List<Boolean> readBits(List<String> addressList) {
+    if (s7PLC == null || addressList.isEmpty()) {
+        return null;
+    }
 
-    public List<Boolean> readBits(List<String> addressList) {
-        if (s7PLC == null)
+    List<Boolean> values = new ArrayList<>();
+    for (String address : addressList) {
+        try {
+            boolean value = s7PLC.readBoolean(address);
+            values.add(value);
+        } catch (Exception e) {
+            // 澶勭悊寮傚父
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+        }
+    }
+
+    return values;
+}
+
+
+//璇诲彇String
+public List<String> readStrings(List<String> addressList) {
+    if (s7PLC == null) {
+        return null;
+    }
+    List<String> result = new ArrayList<>();
+    for (String address : addressList) {
+        try {
+            byte[] bytes = s7PLC.readByte(address, 14);
+            if (bytes != null) {
+                String str = new String(bytes, StandardCharsets.UTF_8);
+                result.add(str);
+            }
+        } catch (Exception e) {
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+            result.add(null);
+        }
+    }
+
+    return result;
+}
+
+
+
+
+
+    public List<String> readStringsandword(List<String> addressList) {
+        if (s7PLC == null) {
             return null;
-
-        List<Boolean> result = new ArrayList<>();
-
+        }
+        List<String> result = new ArrayList<>();
         for (String address : addressList) {
-            if (address.contains("~")) {
-                String[] range = address.split("~");
-                String startAddress = range[0];
-                String endAddress = range[1];
+            try {
+                if (address.contains("-")) {
+                    address = address.substring(0, address.indexOf("-"));
 
-                int startIndex = extractAddressNumber(startAddress);
-                int endIndex = extractAddressNumber(endAddress);
+                    byte[] bytes = s7PLC.readByte(address, 14);
+                    if (bytes != null) {
+                        String str = new String(bytes, StandardCharsets.UTF_8);
+                        result.add(str);
+                    }
 
-                String prefix = startAddress.substring(0, startAddress.indexOf(".") + 1);
+                } else {
+                    Short value = s7PLC.readInt16(address);
+                    result.add(value.toString());
 
-                for (int i = startIndex; i <= endIndex; i++) {
-                    String newAddress = prefix + i;
-                    result.add(s7PLC.readBoolean(newAddress));
                 }
-            } else {
-                result.add(s7PLC.readBoolean(address));
+            } catch (Exception e) {
+                System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+                result.add(null);
             }
         }
 
         return result;
     }
+
+
+
+//涓嶈繛缁湴鍧�鍐欏叆Word
+    public void WriteWord(List<String> address, List<Short> datas) {
+        if (s7PLC == null)
+            return;
+
+        for (int i = 0; i < address.size(); i++) {
+            String addr = address.get(i);
+            short data = datas.get(i);
+
+            if (addr.contains("-")) {
+                outmesid(String.valueOf(data),addr); // 鍗曠嫭澶勭悊甯︾牬鎶樺彿鐨勫湴鍧�
+            } else {
+                s7PLC.writeInt16(addr, data); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧�
+            }
+        }
+    }
+    
+    
+    
+//瀛楃涓插啓鍏�
+    public void outmesid(String data, String addr) {
+//        System.out.println("outmesid: " + data);
+        List<Byte> glassidlist = new ArrayList<>();
+        String[] parts = addr.split("-");
+        if (parts.length == 2) {
+            addr = parts[0]; // 鍙繚鐣� "-" 鍓嶉潰鐨勯儴鍒�
+        }
+        for (char iditem : data.toCharArray()) {
+            glassidlist.add(Byte.valueOf(String.valueOf(iditem)));
+        }
+        byte[] bytes = Bytes.toArray(glassidlist);
+        S7control.getinstance().WriteByte(addr, bytes);
+    }
+
+//璇诲彇涓嶈繛缁瓀ord
+    public List<Short> readWords(List<String> addresses) {
+        if (s7PLC == null) {
+            return null;
+        }
+
+        List<Short> data = new ArrayList<>();
+
+        for (String address : addresses) {
+            try {
+
+                // 鍗曚釜鍦板潃
+                Short value = s7PLC.readInt16(address);
+                data.add(value);
+            } catch (Exception e) {
+                System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+
+            }
+
+        }
+        return data;
+    }
+
+//璇诲彇鏃堕棿
+public Long readtime(String address) {
+    if (s7PLC == null)
+        return null;
+    try {
+        return s7PLC.readTime(address);
+    } catch (Exception e) {
+        System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+        return null;
+    }
+}
+
+
+    public void writetime(String address, long datas) {
+        if (s7PLC == null)
+            return;
+
+
+        s7PLC.writeTime(address, datas); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧�
+    }
+
+
+
 
 
     private int extractAddressNumber(String address) {
@@ -319,11 +420,19 @@
      * @return Boolean缁撴灉
      */
     public List<Boolean> ReadBits(String address, int count) {
-        if (s7PLC==null)
+        if (s7PLC == null)
             return null;
         List<String> addresslist = GetAddressList(address, count, 1);
-        return s7PLC.readBoolean(addresslist);
-    }
+        try {
+            return s7PLC.readBoolean(addresslist);
+        } catch (Exception e) {
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+            return null;
+        }
+
+    };
+    
+    
 
     private List<String> GetAddressList(String address, int count, int addedbit) {
         List<String> addresslist = new ArrayList<String>();
@@ -336,6 +445,7 @@
         if (stringdatas.length == 2) {
             dbwindex = Integer.parseInt(stringdatas[1]);
         } else if (stringdatas.length == 3) {
+            dbwindex = Integer.parseInt(stringdatas[1]);
             bitindex = Integer.parseInt(stringdatas[2]);
         } else
             return null;

--
Gitblit v1.8.0