From 123a6f57a1e52980b323de65e5642148e4348ff3 Mon Sep 17 00:00:00 2001
From: yxFwq <1>
Date: 星期二, 10 六月 2025 15:04:53 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/SteThread.java |  219 ++++++++++++++++++++++++++++++++----------------------
 1 files changed, 129 insertions(+), 90 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/SteThread.java b/src/main/java/com/zy/core/thread/SteThread.java
index 536e038..0682039 100644
--- a/src/main/java/com/zy/core/thread/SteThread.java
+++ b/src/main/java/com/zy/core/thread/SteThread.java
@@ -44,6 +44,7 @@
     private SteProtocol steProtocol;
     private short heartBeatVal = 1;
     private boolean resetFlag = false;
+    private boolean connectSte = false;
 
     public SteThread(SteSlave slave) {
         this.slave = slave;
@@ -52,7 +53,31 @@
     @Override
     @SuppressWarnings("InfiniteLoopStatement")
     public void run() {
-        this.connect();
+        connectSte = this.connect();
+        while(!connectSte){
+            try {
+                connectSte = this.connect();
+                Thread.sleep(100);
+            } catch (Exception e){
+
+            }
+        }
+
+        // 鍚姩绾跨▼鑷姩閲嶈繛
+        new Thread(this::steConnect).start();
+
+        // 鍚姩璇绘暟鎹嚎绋�
+        new Thread(this::readStatusSte).start();
+
+        // 鍚姩浠诲姟涓嬪彂绾跨▼
+        new Thread(this::taskIssued).start();
+
+    }
+
+    /**
+     * 浠诲姟涓嬪彂
+     */
+    private void taskIssued() {
         while (true) {
             try {
                 int step = 1;
@@ -63,7 +88,7 @@
                 switch (step) {
                     // 璇绘暟鎹�
                     case 1:
-                        readStatus();
+//                        readStatus();
                         break;
                     // 鍐欏叆鏁版嵁
                     case 2:
@@ -74,12 +99,53 @@
                 }
                 // 蹇冭烦
 //                heartbeat();
-                Thread.sleep(500);
+                Thread.sleep(50);
             } catch (Exception e) {
-                e.printStackTrace();
+                log.error("STE鍐欑嚎绋嬪紓甯�"+e.getMessage());
+
+//                e.printStackTrace();
             }
 
         }
+    }
+
+    private void steConnect() {
+        while (true) {
+            try {
+                Thread.sleep(1000);
+                if(!connectSte){
+                    try {
+                        connectSte = this.connect();
+                        Thread.sleep(100);
+                    } catch (Exception e){
+
+                    }
+                }
+            } catch (Exception e) {
+                log.error("ste杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+                initSte();
+//                e.printStackTrace();
+            }
+        }
+    }
+
+    private void readStatusSte() {
+        while (true) {
+            try {
+                Thread.sleep(50);
+//                System.out.println("璇荤嚎绋�"+ slave.getId());
+
+                readStatus();
+
+            } catch (Exception e) {
+                log.error("RGV鏁版嵁璇诲彇绾跨▼寮傚父锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+
+                initSte();
+//                e.printStackTrace();
+            }
+
+        }
+
     }
 
     /**
@@ -123,7 +189,7 @@
      */
     private void readStatus(){
         try {
-            OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) 22);
+            OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) 28);
             if (result.IsSuccess) {
                 if (null == steProtocol) {
                     steProtocol = new SteProtocol();
@@ -143,57 +209,36 @@
                 steProtocol.setRow(siemensS7Net.getByteTransform().TransInt16(result.Content, 14));
                 steProtocol.setBay(siemensS7Net.getByteTransform().TransInt16(result.Content, 16));
                 steProtocol.setLev(siemensS7Net.getByteTransform().TransInt16(result.Content, 18));
-
-//                steProtocol.setReady(siemensS7Net.getByteTransform().TransInt16(result.Content, 20));
-                boolean[] status = siemensS7Net.getByteTransform().TransBool(result.Content, 20, 1);
-                steProtocol.setReady(status[0]?(short)1:(short)0);
+                steProtocol.setLoca(siemensS7Net.getByteTransform().TransInt16(result.Content, 20));
+                steProtocol.setLocaStart(siemensS7Net.getByteTransform().TransInt16(result.Content, 22));
+                steProtocol.setLocaEnd(siemensS7Net.getByteTransform().TransInt16(result.Content, 24));
+                boolean[] status = siemensS7Net.getByteTransform().TransBool(result.Content, 26, 1);
+//                steProtocol.setReady(status[0]?(short)1:(short)0);
 //                steProtocol.setReady(status[1]?(short)1:(short)0);
-//                steProtocol.setReady(status[2]?(short)1:(short)0);
-//                steProtocol.setLoad(status[3]?(short)1:(short)0);
-//                steProtocol.setReady(status[4]?(short)1:(short)0);
-                steProtocol.setChargeStatus(status[5]?(short)1:(short)0);
-                steProtocol.setTrack(status[6]?(short)1:(short)0);
+                steProtocol.setReady(status[3]?(short)1:(short)0);
+//                steProtocol.setReady(status[3]?(short)1:(short)0);
+//                steProtocol.setTrack(status[6]?(short)1:(short)0);
 
-                SteLocaType locaType = SteLocaType.NONE;
-                boolean[] status2 = siemensS7Net.getByteTransform().TransBool(result.Content, 21, 1);
-                if (steProtocol.getRow().intValue() >= 6) {
-                    if (status[7]){//A鐐�
-                        locaType = SteLocaType.A;
-                    }
-                    if (status2[1]){//A寰呮満鐐�
-                        if (locaType.equals(SteLocaType.NONE)) {
-                            locaType = SteLocaType.A_WAITING;
-                        }
-                    }
-                }else {
-                    if (status2[0]){//B鐐�
-                        if (locaType.equals(SteLocaType.NONE)) {
-                            locaType = SteLocaType.B;
-                        }
-                    }
-                    if (status2[2]){//B寰呮満鐐�
-                        if (locaType.equals(SteLocaType.NONE)) {
-                            locaType = SteLocaType.B_WAITING;
-                        }
-                    }
-                }
+                boolean[] status2 = siemensS7Net.getByteTransform().TransBool(result.Content, 27, 1);
 
-                steProtocol.setLoca(locaType.id.shortValue());
-                steProtocol.setReady(status2[3]?(short)1:(short)0);
-                steProtocol.setLoad(status2[4]?(short)1:(short)0);
+                steProtocol.setChargeStatus(status2[0]?(short)1:(short)0);
+                steProtocol.setTrack(status2[1]?(short)1:(short)0);
+
+//                steProtocol.setLoad(status2[6]?(short)1:(short)0);
+                steProtocol.setLoad(status2[7]?(short)1:(short)0);
 
                 OutputQueue.STE.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId()));
 
-                // 澶嶄綅淇″彿
-                if (steProtocol.getWaiting()) {
-                    if (resetFlag) {
-                        SteCommand steCommand = new SteCommand();
-                        steCommand.setComplete(true);
-                        if (write(steCommand) && confirmPos()) {
-                            resetFlag = false;
-                        }
-                    }
-                }
+//                // 澶嶄綅淇″彿
+//                if (steProtocol.getWaiting()) {
+//                    if (resetFlag) {
+//                        SteCommand steCommand = new SteCommand();
+//                        steCommand.setComplete(true);
+//                        if (write(steCommand) && confirmPos()) {
+//                            resetFlag = false;
+//                        }
+//                    }
+//                }
 
                 // 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴�
                 BasSteService service = SpringUtils.getBean(BasSteService.class);
@@ -239,17 +284,22 @@
         }
         command.setSteNo(slave.getId());
         OperateResult result = null;
+        News.error("绌挎杞﹀啓鍏ュ懡浠�"+JSON.toJSONString(command));
+
         // 寮�濮嬩换鍔�
         if (!command.getComplete()) {
             //缁勭粐浠诲姟鍓嶏紝鍏堟竻绌哄啓浠诲姟纭浣嶏紝浠ュ強浠诲姟瀹屾垚纭浣�
             siemensS7Net.Write("DB100.0", (short) 0);//璁惧鍙�
             siemensS7Net.Write("DB100.2", (short) 0);//浠诲姟鍙�
-            siemensS7Net.Write("DB100.12.0", false);//浠诲姟寮�濮嬬‘璁や綅
-            siemensS7Net.Write("DB100.12.1", false);//浠诲姟瀹屾垚纭
+            siemensS7Net.Write("DB100.12", (short) 0);//璧峰鐐逛綅
+            siemensS7Net.Write("DB100.14", (short) 0);//鐩殑鐐逛綅
+            siemensS7Net.Write("DB100.16.0", false);//浠诲姟寮�濮嬬‘璁や綅
+            siemensS7Net.Write("DB100.16.1", false);//浠诲姟瀹屾垚纭浣�
+//            siemensS7Net.Write("DB100.18", (short) 0);//纭寲缃愬彿
             // 1.浠诲姟鍙�
             OperateResult result0 = siemensS7Net.Write("DB100.2", command.getTaskNo().shortValue());
             try {
-                Thread.sleep(200);
+                Thread.sleep(50);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
@@ -257,12 +307,15 @@
             if (command.getTaskMode() != 0) {
                 // 2.浣滀笟
                 OperateResult result1 = siemensS7Net.Write("DB100.4", command.getTaskMode());
+                OperateResult result2 = siemensS7Net.Write("DB100.12", (short) command.getStartLoad().shortValue());//璧峰鐐逛綅
+                OperateResult result3 = siemensS7Net.Write("DB100.14", (short) command.getEndLoad().shortValue());//鐩殑鐐逛綅
+                OperateResult result4 = siemensS7Net.Write("DB100.18", (short) command.getJarNo());//鐩殑鐐逛綅
                 // 3.纭寮�濮嬩换鍔�
-                if (result0.IsSuccess && result1.IsSuccess) {
-                    result = siemensS7Net.Write("DB100.12.0", true);
+                if (result0.IsSuccess && result1.IsSuccess && result2.IsSuccess && result3.IsSuccess && result4.IsSuccess) {
+                    result = siemensS7Net.Write("DB100.16.0", true);
 
                     try {
-                        Thread.sleep(300);
+                        Thread.sleep(50);
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
@@ -270,11 +323,11 @@
             }
             // 浠诲姟瀹屾垚
         } else {
-            siemensS7Net.Write("DB100.2", (short) 0);//浠诲姟鍙�
-            siemensS7Net.Write("DB100.4.0", (short) 0);//浠诲姟绫诲瀷
-            siemensS7Net.Write("DB100.12.0", false);//浠诲姟寮�濮嬬‘璁や綅
+//            siemensS7Net.Write("DB100.2", (short) 0);//浠诲姟鍙�
+//            siemensS7Net.Write("DB100.4.0", (short) 0);//浠诲姟绫诲瀷
+            siemensS7Net.Write("DB100.16.0", false);//浠诲姟寮�濮嬬‘璁や綅
 
-            result = siemensS7Net.Write("DB100.12.1", true);//浠诲姟瀹屾垚纭
+            result = siemensS7Net.Write("DB100.16.1", true);//浠诲姟瀹屾垚纭
         }
 
         try {
@@ -307,13 +360,6 @@
         } catch (Exception ignore) {}
 
         if (result != null && result.IsSuccess) {
-            // 缁存姢鏁版嵁搴撴帓鍒楀眰
-            if (!steProtocol.getWaiting()) {
-                if (!Cools.isEmpty(command.getRow(), command.getBay(), command.getLev())) {
-                    this.modifyPos(command.getRow().intValue(), command.getBay().intValue(), command.getLev().intValue());
-                }
-            }
-
             News.info("绌挎杞﹀懡浠や笅鍙慬id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
             OutputQueue.STE.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
             return true;
@@ -321,13 +367,6 @@
             OutputQueue.STE.offer(MessageFormat.format("銆恵0}銆戝啓鍏ョ┛姊溅plc鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
             News.error("鍐欏叆绌挎杞lc鏁版嵁澶辫触 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
             return false;
-        }
-    }
-
-    public void modifyPos(Integer row, Integer bay, Integer lev) {
-        BasSteService service = SpringUtils.getBean(BasSteService.class);
-        if (!service.updatePos(this.slave.getId(), row, bay, lev)) {
-            News.error("鏇存柊{}鍙风┛姊溅瀹氫綅澶辫触 ===>> 鎺掞細銆恵}銆�, 鍒楋細銆恵}銆戯紝灞傦細銆恵}銆�", this.slave.getId(), row, bay, lev);
         }
     }
 
@@ -388,21 +427,21 @@
     /******************************************************************************************/
     /**************************************** 娴嬭瘯涓撶敤 *****************************************/
     /*****************************************************************************************/
-    public static void main(String[] args) throws InterruptedException {
-        SteSlave slave = new SteSlave();
-        slave.setId(1);
-        slave.setIp("192.168.2.1");
-        slave.setPort(502);
-        SteThread thread = new SteThread(slave);
-        thread.connect();
-        thread.readStatus();
-        System.out.println(JSON.toJSONString(thread.steProtocol));
-
-        // 绌挎杞﹁繍琛岀姝�
-        SteCommand command = new SteCommand();
-        command.setRun((short)0);
-        thread.write(command);
-
-    }
+//    public static void main(String[] args) throws InterruptedException {
+//        SteSlave slave = new SteSlave();
+//        slave.setId(1);
+//        slave.setIp("192.168.2.1");
+//        slave.setPort(502);
+//        SteThread thread = new SteThread(slave);
+//        thread.connect();
+//        thread.readStatus();
+//        System.out.println(JSON.toJSONString(thread.steProtocol));
+//
+//        // 绌挎杞﹁繍琛岀姝�
+//        SteCommand command = new SteCommand();
+////        command.setRun((short)0);
+//        thread.write(command);
+//
+//    }
 
 }

--
Gitblit v1.9.1