From c588ec9236ef644fe13af957920198bdcb4e05e8 Mon Sep 17 00:00:00 2001
From: lty <876263681@qq.com>
Date: 星期六, 29 三月 2025 13:44:38 +0800
Subject: [PATCH] #输送线循环覆写由plc执行

---
 src/main/java/com/zy/core/thread/SiemensDevpThread.java |   89 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 88 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/SiemensDevpThread.java b/src/main/java/com/zy/core/thread/SiemensDevpThread.java
index d731d3a..783e791 100644
--- a/src/main/java/com/zy/core/thread/SiemensDevpThread.java
+++ b/src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -24,6 +24,7 @@
 import com.zy.core.enums.SlaveType;
 import com.zy.core.model.DevpSlave;
 import com.zy.core.model.Task;
+import com.zy.core.model.protocol.Cycle;
 import com.zy.core.model.protocol.StaProtocol;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
@@ -132,7 +133,7 @@
                         break;
                     // 鍐欐暟鎹� ID+鐩爣绔�
                     case 4:
-                        writeAgvOk((StaProtocol)task.getData());
+                        writeCycle((StaProtocol)task.getData());
                         read();
                         break;
                    /* case 3:
@@ -544,6 +545,76 @@
         handleWriteFailure(staProtocol, writeFlag);
     }
 
+    private void writeCycle(StaProtocol staProtocol) throws InterruptedException {
+        if (staProtocol == null) {
+            return;
+        }
+
+        ArrayList<Integer> staNos = getStaNo();
+        int index = staNos.indexOf(staProtocol.getSiteId());
+
+        if (index == -1) {
+            log.error("绔欑偣缂栧彿 {} 涓嶅湪宸茬煡鍒楄〃涓紝鏃犳硶鍐欏叆浠诲姟锛�", staProtocol.getSiteId());
+            return;
+        }
+
+        int writeCount = 0; // 浠诲姟涓嬪彂灏濊瘯娆℃暟
+        boolean writeFlag = false; // 浠诲姟涓嬪彂鎴愬姛鏍囪
+        String plcAddressWorkNo = "";
+        String plcAddressStaNo = "" ;
+        switch (staProtocol.getSiteId()){
+            case 105:
+                plcAddressWorkNo = "DB73." + 0;
+                plcAddressStaNo = "DB73." + 4;
+                break;
+            case 106:
+                plcAddressWorkNo = "DB73." + 6;
+                plcAddressStaNo = "DB73." + (6 + 4);
+                break;
+            case 108:
+                plcAddressWorkNo = "DB73." + 2 * 6;
+                plcAddressStaNo = "DB73." + (2 * 6 + 4);
+                break;
+            case 110:
+                plcAddressWorkNo = "DB73." + 3 * 6;
+                plcAddressStaNo = "DB73." + (3 * 6 + 4);
+                break;
+            case 112:
+                plcAddressWorkNo = "DB73." + 4 * 6;
+                plcAddressStaNo = "DB73." + (4 * 6 + 4);
+                break;
+        }
+//        String plcAddressWorkNo = "DB100." + index * 6;
+//        String plcAddressStaNo = "DB100." + (index * 6 + 4);
+
+        while (writeCount < 5) {
+            // **璇诲彇褰撳墠PLC鐘舵�侊紝閬垮厤涓嶅繀瑕佺殑鍐欏叆**
+            OperateResultExOne<byte[]> readResult = siemensS7Net.Read(plcAddressWorkNo, (short) 6);
+            if (readResult.IsSuccess) {
+                int currentWorkNo = siemensS7Net.getByteTransform().TransInt32(readResult.Content, 0);
+                short currentStaNo = siemensS7Net.getByteTransform().TransInt16(readResult.Content, 4);
+
+                if (currentWorkNo == staProtocol.getWorkNo().intValue() && currentStaNo == staProtocol.getStaNo()) {
+                    log.info("绔欑偣 {} 褰撳墠鐘舵�佸凡鍖归厤锛屾棤闇�閲嶅鍐欏叆", staProtocol.getSiteId());
+                    return;
+                }
+            }
+
+            // **鍐欏叆鏂颁换鍔�**
+            if (writeTaskToPLC(plcAddressWorkNo, plcAddressStaNo, staProtocol)) {
+                writeFlag = true;
+                log.info("杈撻�佺嚎鍛戒护鍐欏叆鎴愬姛锛孭LC缂栧彿={}锛岀珯鐐规暟鎹�={}锛屽皾璇曟鏁�={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
+                break;
+            }
+
+            log.warn("杈撻�佺嚎鍛戒护鍐欏叆澶辫触锛孭LC缂栧彿={}锛岀珯鐐规暟鎹�={}锛屽皾璇曟鏁�={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
+            writeCount++;
+        }
+
+        // **鍐欏叆澶辫触澶勭悊**
+        handleWriteFailure(staProtocol, writeFlag);
+    }
+
     /**
      * 娓呴浂 PLC 鏁版嵁骞堕獙璇佹竻闆舵槸鍚︽垚鍔�
      */
@@ -584,6 +655,22 @@
         return false;
     }
 
+    private boolean writeTaskToPLC(String plcAddressWorkNo, String plcAddressStaNo, Cycle cycle) throws InterruptedException {
+        OperateResult writeResult1 = siemensS7Net.Write(plcAddressWorkNo, cycle.getWorkNo().intValue());
+        OperateResult writeResult2 = siemensS7Net.Write(plcAddressStaNo, cycle.getStaNo());
+
+        if (writeResult1.IsSuccess && writeResult2.IsSuccess) {
+            Thread.sleep(200); // 绛夊緟 PLC 璇嗗埆鏂板��
+            OperateResultExOne<byte[]> readResult = siemensS7Net.Read(plcAddressWorkNo, (short) 6);
+            if (readResult.IsSuccess) {
+                int workNo = siemensS7Net.getByteTransform().TransInt32(readResult.Content, 0);
+                short staNo = siemensS7Net.getByteTransform().TransInt16(readResult.Content, 4);
+                return workNo == cycle.getWorkNo().intValue() && staNo == cycle.getStaNo();
+            }
+        }
+        return false;
+    }
+
     /**
      * 澶勭悊鍐欏叆澶辫触鐨勬儏鍐�
      */

--
Gitblit v1.9.1