From ea073afc6b4bc758990ac60f5e33254d1143272c Mon Sep 17 00:00:00 2001
From: wang <3597712270@qq.com>
Date: 星期二, 26 三月 2024 13:58:03 +0800
Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/HangZhouMes

---
 UnLoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java |   71 ++++++++++++++++++++++++++++++-----
 1 files changed, 61 insertions(+), 10 deletions(-)

diff --git a/UnLoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java b/UnLoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java
index 9cc9e37..d62cad3 100644
--- a/UnLoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java
+++ b/UnLoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java
@@ -1,23 +1,39 @@
 package com.mes.common.PlcTools;
 
-
-import java.nio.charset.StandardCharsets;
-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 java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+
 public class S7control {
 
     S7PLC s7PLC; // PLC閫氳绫诲疄渚�
-    
-    public S7control(EPlcType plcType, String ip, int port, int rack, int slot) {
+    private EPlcType plcType = EPlcType.S1500; // 瑗块棬瀛怭LC绫诲瀷
+    private String ip = "192.168.10.1"; // plc ip鍦板潃
+    private int port = 102; // plc 绔彛鍙�
+
+    private static volatile S7control instance = null;
+
+    private S7control() {
         if (s7PLC == null)
             s7PLC = new S7PLC(plcType, ip, port,0,0);
-    } 
+    }
+
+    // 鍗曚緥妯″紡 鑾峰彇绫荤殑鍞竴瀹炰緥
+    public static S7control getinstance() {
+        if (instance == null) {
+            synchronized (S7control.class) {
+                if (instance == null)
+                    instance = new S7control();
+            }
+        }
+        return instance;
+    }
+    
     /**
      * 鍏抽棴瑗块棬瀛恠7閫氳杩炴帴
      */
@@ -33,7 +49,7 @@
     public boolean CheckConnected() {
         return s7PLC.checkConnected();
     }
-     
+
     /**
      * 鎸夋寚瀹氱殑鍦板潃 鍐欏叆涓�涓獁ord
      * 
@@ -269,6 +285,41 @@
 }
 
 
+
+
+
+    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
     public void WriteWord(List<String> address, List<Short> datas) {
         if (s7PLC == null)
@@ -300,7 +351,7 @@
             glassidlist.add(Byte.valueOf(String.valueOf(iditem)));
         }
         byte[] bytes = Bytes.toArray(glassidlist);
-        WriteByte(addr, bytes);
+        S7control.getinstance().WriteByte(addr, bytes);
     }
 
 //璇诲彇涓嶈繛缁瓀ord

--
Gitblit v1.8.0