From e7eaa7bb402aae3bb88d9219e50f0e1179f3257f Mon Sep 17 00:00:00 2001
From: wuyouming666 <2265557248@qq.com>
Date: 星期二, 26 十二月 2023 09:28:52 +0800
Subject: [PATCH] layout 布局页增加A01 A02终止继续后选择 弹框

---
 springboot-vue3/src/main/java/com/example/springboot/component/S7control.java |  246 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 225 insertions(+), 21 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 9b9a66e..95ebb15 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 {
 
@@ -78,17 +84,11 @@
      * @param address 鍦板潃
      * @param datas   word鐨勫��
      */
-    public void WriteWord(List<String> address, List<Short> datas) {
-        if (s7PLC==null)
-            return;
-        // s7PLC.write(address, data);
 
-        MultiAddressWrite addressWrite = new MultiAddressWrite();
-        for (int i = 0; i < address.size(); i++) {
-            addressWrite.addInt16(address.get(i), datas.get(i));
-        }
-        s7PLC.writeMultiData(addressWrite);
-    }
+
+
+
+
 
     /**
      * 鎸夋寚瀹氱殑鍦板潃 鍐欏叆涓�涓狟it
@@ -137,6 +137,18 @@
         }
         s7PLC.writeMultiData(addressWrite);
     }
+     /**
+     * 鎸夋寚瀹氱殑鍦板潃 鍐欏叆澶氫釜byte
+     * 
+     * @param address 鍦板潃
+     * @param datas   byte鐨勫��
+     */
+    public void WriteByte(String address, byte[] datas) {
+        if (s7PLC==null)
+            return;
+        // s7PLC.write(address, data); 
+        s7PLC.writeByte(address, datas);
+    }
 
     /**
      * 鎸夋寚瀹氱殑鍦板潃 璇诲彇word缁撴灉闆�
@@ -145,9 +157,30 @@
      * @return 缁撴灉
      */
     public List<Short> ReadWord(List<String> address) {
-        if (s7PLC==null)
-              return null;
-        return s7PLC.readInt16(address);
+        if (s7PLC == null)
+            return null;
+
+        try {
+            return s7PLC.readInt16(address);
+        } catch (Exception e) {
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+            return null;
+        }
+    }
+
+
+
+
+    private int getIndexFromAddress(String address) {
+
+        // 鍙互瑙f瀽鍑哄湴鍧�涓殑鏁板瓧閮ㄥ垎锛屽苟杞崲涓烘暣鏁�
+        return 0;
+    }
+
+    private String getAddressFromIndex(int index) {
+
+        // 鏁存暟杞崲涓哄湴鍧�鏍煎紡鐨勫瓧绗︿覆
+        return "";
     }
 
     /**
@@ -158,11 +191,39 @@
      * @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缁撴灉闆�
+     * 
+     * @param address 鍦板潃
+     * @param count   杩炵画璇诲灏戜釜byte
+     * @return 缁撴灉
+     */
+    public byte[] ReadByte(String address, int count) {
+        if (s7PLC==null)
+             return null;
+       // List<String> addresslist = GetAddressList(address, count, 16);
+
+        try {
+            return s7PLC.readByte(address, count);
+        }catch (Exception e) {
+            // 澶勭悊寮傚父
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+            return null;
+        }
+
     }
 
     /**
@@ -176,6 +237,137 @@
              return null;
         return s7PLC.readBoolean(addresslist);
     }
+//璇诲彇涓嶈繛缁湴鍧�bit
+public List<Boolean> readBits(List<String> addressList) {
+    if (s7PLC == null || addressList.isEmpty()) {
+        return 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;
+}
+
+
+//涓嶈繛缁湴鍧�鍐欏叆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) {
+        e.printStackTrace();
+        return null;
+    }
+}
+
+
+    public void writetime(String address, long datas) {
+        if (s7PLC == null)
+            return;
+
+
+        s7PLC.writeTime(address, datas); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧�
+    }
+
+
+
+
+
+    private int extractAddressNumber(String address) {
+        String numberStr = address.replaceAll("\\D+", ""); // 浣跨敤姝e垯琛ㄨ揪寮忔彁鍙栨暟瀛楅儴鍒�
+        return Integer.parseInt(numberStr);
+    }
+
 
     /**
      * 浠庢寚瀹氱殑鍦板潃寮�濮� 杩炵画鎸塨it浣嶈鍙�
@@ -185,11 +377,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>();
@@ -202,11 +402,13 @@
         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;
 
-        for (int i = 0; i < count; i++) {
+            addresslist.add(address);
+        for (int i = 0; i < count-1; i++) {
 
             int bitcurrent = bitindex + addedbit;
             if (bitcurrent > 7) {
@@ -214,7 +416,9 @@
                 bitindex = 0;
             } else
                 bitindex = bitcurrent;
-            addresslist.add(stringdatas[0] + "." + dbwindex + "." + bitindex);
+
+                String endstr=stringdatas.length==3?"." + bitindex:"";
+            addresslist.add(stringdatas[0] + "." + dbwindex + endstr);
         }
         return addresslist;
     }

--
Gitblit v1.8.0