From b3280d431ea42f46afe020782b677b11d504be31 Mon Sep 17 00:00:00 2001
From: 1 <1>
Date: 星期三, 14 一月 2026 13:32:38 +0800
Subject: [PATCH] #

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

diff --git a/src/main/java/com/zy/core/thread/SiemensDevpThread.java b/src/main/java/com/zy/core/thread/SiemensDevpThread.java
index 11c8aa7..b605d29 100644
--- a/src/main/java/com/zy/core/thread/SiemensDevpThread.java
+++ b/src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -84,7 +84,7 @@
         add(2022);add(2024);
         add(2028);add(2030);
         add(2025);add(2027);
-        add(2031);add(2033);
+        add(2031);add(2032);add(2033);
         add(2034);add(2035);add(2037);
 //        add(2041);add(2042);add(2043);add(2044);
 //        add(2045);add(2046);add(2047);add(2048);
@@ -538,23 +538,13 @@
                     } else if(siteId<4000){
                         if (siteId == 2110) {
                             if (resultArm5.IsSuccess) {
-                                // 杞崲涓哄瓧绗︿覆
-                                String resultString = siemensS7Net.getByteTransform().TransString(resultArm5.Content, 2, 254,"UTF-8");
-                                // 鍘婚櫎鍙兘鐨勭┖瀛楃
-//                                resultString = resultString.TrimEnd('\0');
-//                                System.out.println("2110Barcode:"+resultString);
-                                staProtocol.setBarcode(resultString);
+                                staProtocol.setBarcode(parseS7String(resultArm5));
                             } else {
                                 staProtocol.setBarcode("");
                             }
                         } else if (siteId == 2120){
                             if (resultArm6.IsSuccess) {
-                                // 杞崲涓哄瓧绗︿覆
-                                String resultString = siemensS7Net.getByteTransform().TransString(resultArm6.Content, 2, 254,"UTF-8");
-                                // 鍘婚櫎鍙兘鐨勭┖瀛楃
-//                                resultString = resultString.TrimEnd('\0');
-//                                System.out.println("2120Barcode:"+resultString);
-                                staProtocol.setBarcode(resultString);
+                                staProtocol.setBarcode(parseS7String(resultArm6));
                             } else {
                                 staProtocol.setBarcode("");
                             }
@@ -623,14 +613,16 @@
                     boolean[] status1 = siemensS7Net.getByteTransform().TransBool(result.Content, offset + offset3 - 4, 3);
                     boolean[] statusErr = siemensS7Net.getByteTransform().TransBool(result.Content, offset + offset3 - 8, 3);
                     boolean[] statusSign = siemensS7Net.getByteTransform().TransBool(resultErr.Content, offsetSign, 3);
-
                     boolean loading = false;
                     if(!status1[0])
                     {
                         loading = true;
                     }
                     staProtocol.setLoading(loading);  // 鏈夌墿
+                    if(siteId == 2032){
+                        staProtocol.setLoading(loading);  // 鏈夌墿
 
+                    }
                     staProtocol.setInEnable(status1[2]); // 鍙叆
                     staProtocol.setOutEnable(status1[2]);// 鍙嚭
                     staProtocol.setErr(status1[6]);
@@ -1018,6 +1010,168 @@
             News.info("SiemensDevp"+" - 5"+" - 杈撻�佺嚎鍛戒护涓嬪彂 [id:{}] >>>>> 鍛戒护涓嬪彂锛� {}",  slave.getId(), JSON.toJSON(staProtocol));
         }
     }
+//    /**
+//     * 鍐欏叆 ID + 鐩爣绔� =====> 鍗曠珯鐐瑰啓鍏ワ紙甯﹁鍥炴牎楠� + 閲嶈瘯鏈哄埗锛�
+//     */
+//    private void write(StaProtocol staProtocol) throws InterruptedException {
+//        if (staProtocol == null) {
+//            return;
+//        }
+//
+//        Integer siteId = staProtocol.getSiteId();
+//        Integer offset = siteOffsetMap.get(siteId);
+//        if (offset == null) {
+//            log.warn("绔欑偣 {} 娌℃湁瀹氫箟鍋忕Щ閲忥紝璺宠繃鍐欏叆", siteId);
+//            return;
+//        }
+//
+//        Integer offset2 = getOffsetBySiteId(siteId);
+//        Integer offset3 = getOffsetBySiteId2(siteId); // 铏界劧鏈柟娉曚富瑕佺敤涓嶅埌锛屼絾淇濈暀浠ラ槻鍚庣画鎵╁睍
+//
+//        // 鍑嗗鏈熸湜鍐欏叆鐨勫�硷紙鐢ㄤ簬鍚庣画姣斿锛�
+//        final long expectWorkNo = staProtocol.getWorkNo();
+//        final long expectStaNo = staProtocol.getStaNo() != null ? staProtocol.getStaNo().intValue() : 0;
+//
+//        boolean isSpecial2 = specialSites2.contains(siteId);
+//        final long expectStaNoOffset = isSpecial2 ? (offset + offset2 + 48) : (offset + offset2 + 12);
+//
+//        // 纭畾瑕佹搷浣滅殑 DB 鍧�
+//        String dbName;
+//        if (siteId < 1029) {
+//            dbName = "DB100.";
+//        } else if (siteId < 1042) {
+//            dbName = "DB101.";
+//        } else if (siteId < 1054) {
+//            dbName = "DB104.";
+//        } else if (siteId < 1080) {
+//            dbName = "DB103.";
+//        } else if (siteId < 1111) {
+//            dbName = "DB102.";
+//        } else if (siteId < 2031) {
+//            dbName = "DB200.";
+//        } else if (siteId < 2121) {
+//            dbName = "DB201.";
+//        } else if (siteId < 4000) {
+//            // 2妤糚DA鏉$爜绔欑偣鐗规畩澶勭悊锛堢洰鍓嶄笉鍋氳鍥為噸璇曪級
+//            Integer offsetBarcode = site2lBarcodeOffsetMap.get(siteId);
+//            if (offsetBarcode == null) {
+//                log.warn("绔欑偣 {} 娌℃湁瀹氫箟2妤兼潯鐮佸亸绉婚噺锛岃烦杩囨潯鐮佸啓鍏�", siteId);
+//                return;
+//            }
+//            OperateResult writeBarcode = siemensS7Net.Write("DB202." + offsetBarcode, staProtocol.getBarcode());
+//            if (writeBarcode.IsSuccess) {
+//                log.info("2妤糚DA鏉$爜鍐欏叆鎴愬姛 site={}, barcode={}", siteId, staProtocol.getBarcode());
+//            } else {
+//                log.error("2妤糚DA鏉$爜鍐欏叆澶辫触 site={}, barcode={}", siteId, staProtocol.getBarcode());
+//            }
+//            return;
+//        } else {
+//            dbName = "DB400.";
+//        }
+//
+//        String addrWorkNo = dbName + (offset + offset2);
+//        String addrStaNo = dbName + expectStaNoOffset;
+//
+//        int maxRetry = 5;
+//        int tryCount = 0;
+//        boolean success = false;
+//
+//        while (tryCount < maxRetry && !success) {
+//            tryCount++;
+//
+//            // 1. 灏濊瘯鍐欏叆
+//            OperateResult writeWork = siemensS7Net.Write(addrWorkNo, expectWorkNo);
+//            writeWork = siemensS7Net.Write("DB100." + (offset + offset2), staProtocol.getWorkNo());    // 宸ヤ綔鍙�
+//
+//            Thread.sleep(80);
+//
+//            OperateResult writeSta = siemensS7Net.Write(addrStaNo, expectStaNo);
+//            Thread.sleep(120);
+//
+//            if (!writeWork.IsSuccess || !writeSta.IsSuccess) {
+//                log.warn("绗瑊}娆″啓鍏ュけ璐� site={}, workNo={}, targetSta={}",
+//                        tryCount, siteId, expectWorkNo, expectStaNo);
+//                Thread.sleep(300);
+//                continue;
+//            }
+//
+//            // 2. 璇诲洖鏍¢獙锛堣嚦灏戣64瀛楄妭锛岀‘淇濊鐩栨墍闇�瀛楁锛�
+//            OperateResultExOne<byte[]> readResult = siemensS7Net.Read(dbName + (offset + offset2), (short) 64);
+//            if (!readResult.IsSuccess || readResult.Content == null || readResult.Content.length < 64) {
+//                log.warn("绗瑊}娆¤鍥炲け璐ワ紝鏃犳硶鏍¢獙 site={}", tryCount, siteId);
+//                Thread.sleep(300);
+//                continue;
+//            }
+//
+//            byte[] data = readResult.Content;
+//
+//            // 璁$畻鐩稿鍋忕Щ杩涜璇诲彇
+//            int relativeWorkNoOffset = 0;
+//            int relativeStaNoOffset = (int) (expectStaNoOffset - (offset + offset2));
+//
+//            int actualWorkNo = siemensS7Net.getByteTransform().TransInt32(data, relativeWorkNoOffset);
+//            int actualStaNo = siemensS7Net.getByteTransform().TransInt32(data, relativeStaNoOffset);
+//
+//            boolean match = (actualWorkNo == expectWorkNo) && (actualStaNo == expectStaNo);
+//
+//            if (match) {
+//                success = true;
+//                log.info("鍐欏叆骞舵牎楠屾垚鍔� site={}, workNo={}, targetSta={}, 绗瑊}娆″皾璇�",
+//                        siteId, expectWorkNo, expectStaNo, tryCount);
+//            } else {
+//                log.warn("绗瑊}娆℃牎楠屽け璐� site={}, 鏈熸湜[workNo={},staNo={}], 瀹為檯[workNo={},staNo={}]",
+//                        tryCount, siteId, expectWorkNo, expectStaNo, actualWorkNo, actualStaNo);
+//                Thread.sleep(300);
+//            }
+//        }
+//
+//        // 鏈�缁堢粨鏋滃鐞�
+//        if (success) {
+//            log.info("杈撻�佺嚎鍛戒护涓嬪彂骞舵牎楠屾垚鍔� [plc:{}] site:{} workNo:{} target:{}",
+//                    slave.getId(), siteId, expectWorkNo, expectStaNo);
+//
+//            OutputQueue.DEVP.offer(MessageFormat.format(
+//                    "銆恵0}銆戣緭閫佺嚎鍛戒护涓嬪彂骞舵牎楠屾垚鍔� [id:{1}] >>>>> site:{2} workNo:{3} target:{4}",
+//                    DateUtils.convert(new Date()), slave.getId(), siteId, expectWorkNo, expectStaNo));
+//
+//            News.info("SiemensDevp - 5 - 杈撻�佺嚎鍛戒护涓嬪彂骞舵牎楠屾垚鍔� [id:{}] >>>>> {}",
+//                    slave.getId(), JSON.toJSON(staProtocol));
+//        } else {
+//            log.error("缁忚繃{}娆″皾璇曪紝鍐欏叆骞舵牎楠屼粛鐒跺け璐ワ紒plc={} site={} workNo棰勬湡={} target棰勬湡={}",
+//                    maxRetry, slave.getId(), siteId, expectWorkNo, expectStaNo);
+//
+//            OutputQueue.DEVP.offer(MessageFormat.format(
+//                    "銆恵0}銆戠粡杩噞1}娆″皾璇曪紝杈撻�佺嚎鍐欏叆鏍¢獙澶辫触锛� [id:{2}] site:{3} workNo:{4} target:{5}",
+//                    DateUtils.convert(new Date()), maxRetry, slave.getId(), siteId, expectWorkNo, expectStaNo));
+//
+//            News.error("SiemensDevp - 4 - 澶氭灏濊瘯鍚庡啓鍏ユ牎楠屽け璐ワ紒site={} workNo={} target={}",
+//                    siteId, expectWorkNo, expectStaNo);
+//        }
+//
+//        // 鎿嶄綔鏃ュ織璁板綍锛堟棤璁烘垚鍔熷け璐ラ兘璁板綍锛�
+//        try {
+//            BasDevpOptService bean = SpringUtils.getBean(BasDevpOptService.class);
+//            BasDevpOpt basDevpOpt = new BasDevpOpt(
+//                    staProtocol.getWorkNo(),
+//                    new Date(),
+//                    staProtocol.getSiteId(),
+//                    (int) staProtocol.getStaNo(),
+//                    new Date(),
+//                    String.valueOf(staProtocol.isLoading()),
+//                    staProtocol.getBarcode(),
+//                    staProtocol.isFrontErr() ? 1L : 0L,
+//                    staProtocol.isBackErr() ? 1L : 0L,
+//                    staProtocol.isHighErr() ? 1L : 0L,
+//                    staProtocol.isLeftErr() ? 1L : 0L,
+//                    staProtocol.isRightErr() ? 1L : 0L,
+//                    staProtocol.isBarcodeErr() ? 1L : 0L,
+//                    staProtocol.getErrCode()
+//            );
+//            bean.insert(basDevpOpt);
+//        } catch (Exception e) {
+//            log.warn("鍐欏叆鎿嶄綔鏃ュ織澶辫触 site={}", siteId, e);
+//        }
+//    }
 
     // 鏇存柊鍏ュ嚭搴撴ā寮�
     private void updateIoMode() throws InterruptedException {
@@ -1071,6 +1225,39 @@
         }
     }
 
+    private String parseS7String(OperateResultExOne<byte[]> result) {
+
+        if (result == null || !result.IsSuccess || result.Content == null) {
+            return "";
+        }
+
+        byte[] buf = result.Content;
+
+        if (buf.length < 2) {
+            return "";
+        }
+
+        // S7 瀛楃涓叉牸寮忥細
+        // buf[0] = 鏈�澶ч暱搴�
+        // buf[1] = 褰撳墠鐪熷疄闀垮害
+        int realLen = buf[1] & 0xFF;
+
+        if (realLen <= 0) {
+            return "";
+        }
+
+        // 闃叉 PLC 鍐欓敊闀垮害瀵艰嚧瓒婄晫
+        if (realLen > buf.length - 2) {
+            realLen = buf.length - 2;
+        }
+
+        String value = siemensS7Net.getByteTransform()
+                .TransString(buf, 2, realLen, "UTF-8");
+
+        return value == null ? "" : value.trim();
+    }
+
+
     /**
      * 璁剧疆鍏ュ簱鏍囪
      */

--
Gitblit v1.9.1