From 95db3e96a9465f137fdf16540e0c5985752894c2 Mon Sep 17 00:00:00 2001
From: 严智鑫 <test>
Date: 星期二, 09 四月 2024 14:27:33 +0800
Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/HangZhouMes

---
 LoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java |   89 ++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 80 insertions(+), 9 deletions(-)

diff --git a/LoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java b/LoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java
index 9cc9e37..8ffee41 100644
--- a/LoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java
+++ b/LoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java
@@ -1,7 +1,9 @@
 package com.mes.common.PlcTools;
 
-
+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;
 
@@ -9,15 +11,37 @@
 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 {
 
     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 S7control(EPlcType plcType,String ip,int port,int a,int b) {
+        if (s7PLC == null)
+            s7PLC = new S7PLC(plcType, ip, port,a,b);
+    }
+
+    // 鍗曚緥妯″紡 鑾峰彇绫荤殑鍞竴瀹炰緥
+    public static S7control getinstance() {
+        if (instance == null) {
+            synchronized (S7control.class) {
+                if (instance == null)
+                    instance = new S7control();
+            }
+        }
+        return instance;
+    }
+    
     /**
      * 鍏抽棴瑗块棬瀛恠7閫氳杩炴帴
      */
@@ -33,7 +57,7 @@
     public boolean CheckConnected() {
         return s7PLC.checkConnected();
     }
-     
+
     /**
      * 鎸夋寚瀹氱殑鍦板潃 鍐欏叆涓�涓獁ord
      * 
@@ -156,7 +180,7 @@
         }
     }
 
-
+    
 
 
     private int getIndexFromAddress(String address) {
@@ -181,7 +205,6 @@
     public List<Short> ReadWord(String address, int count) {
         if (s7PLC == null)
             return null;
-
         List<String> addresslist = GetAddressList(address, count, 16);
         try {
             return s7PLC.readInt16(addresslist);
@@ -191,7 +214,13 @@
             return null;
         }
     }
-
+    public byte[] Readbyte(String address, int count) {
+        byte[] byt=new byte[count];
+        int wordcount=((count%2==0)?count/2:count+1);
+        List<Short> word=ReadWord(address,wordcount);
+        
+        return byt;
+    }
     /**
      * 鎸夋寚瀹氱殑鍦板潃 璇诲彇byte缁撴灉闆�
      * 
@@ -269,6 +298,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 +364,7 @@
             glassidlist.add(Byte.valueOf(String.valueOf(iditem)));
         }
         byte[] bytes = Bytes.toArray(glassidlist);
-        WriteByte(addr, bytes);
+        S7control.getinstance().WriteByte(addr, bytes);
     }
 
 //璇诲彇涓嶈繛缁瓀ord
@@ -410,4 +474,11 @@
         }
         return addresslist;
     }
+    public void writeString(String addr,String data) {
+        s7PLC.writeString(addr,data);
+    }
+    
+    public String readStrings(String addr) {
+        return s7PLC.readString(addr);
+    }
 }

--
Gitblit v1.8.0