From 3271f9b1293bef8304734534a965e2230b309116 Mon Sep 17 00:00:00 2001
From: zhang <zc857179121@qq.com>
Date: 星期二, 14 四月 2026 10:01:22 +0800
Subject: [PATCH] 1

---
 zy-asc-conveyor/src/main/java/com/zy/acs/conveyor/core/thread/SiemensDevpThread.java |  201 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 170 insertions(+), 31 deletions(-)

diff --git a/zy-asc-conveyor/src/main/java/com/zy/acs/conveyor/core/thread/SiemensDevpThread.java b/zy-asc-conveyor/src/main/java/com/zy/acs/conveyor/core/thread/SiemensDevpThread.java
index 0c9d2ee..039cb00 100644
--- a/zy-asc-conveyor/src/main/java/com/zy/acs/conveyor/core/thread/SiemensDevpThread.java
+++ b/zy-asc-conveyor/src/main/java/com/zy/acs/conveyor/core/thread/SiemensDevpThread.java
@@ -3,9 +3,9 @@
 import HslCommunication.Core.Types.OperateResultExOne;
 import HslCommunication.Profinet.Siemens.SiemensS7Net;
 import com.zy.acs.common.utils.News;
-import com.zy.acs.conveyor.core.constant.DeviceField;
-import com.zy.acs.conveyor.core.constant.PlcAlarmDefinition;
-import com.zy.acs.conveyor.core.constant.StationStatusField;
+import com.zy.acs.common.utils.RedisSupport;
+import com.zy.acs.conveyor.core.constant.*;
+import com.zy.acs.conveyor.core.model.SafeSignal;
 import com.zy.acs.conveyor.core.model.StaProtocol;
 import com.zy.acs.conveyor.core.properties.DevpSlave;
 import com.zy.acs.conveyor.core.service.DevpS7Service;
@@ -17,6 +17,8 @@
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -34,6 +36,9 @@
     private SiemensS7Net siemensS7Net;
 
     private Map<Integer, StaProtocol> station;
+
+    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
+
 
     private static final int WRITE_RETRY_MAX = 5;
 
@@ -107,18 +112,25 @@
 
         byte[] content = result.Content;
         for (int i = 0; i < staNoSize; i++) {
-            StaProtocol staProtocol = station.get(staNos.get(staNoSize));
+            StaProtocol staProtocol = station.get(staNos.get(i));
             parseStationStatus(content, i, staProtocol);
         }
 
         // 璇诲彇鏉$爜
         readBarcodes();
 
+        // 绉伴噸
+        readWeight();
+
         // 璇诲彇澶栧舰妫�娴嬮敊璇�
         readDimensionErrors();
 
         // 璇诲彇PLC鏁呴殰
         readPlcAlarms(staNos, staNoSize);
+
+        // 璇诲彇瀹夊叏浜や簰淇″彿
+        readSaleSingle(slave.getPutSta(), slave.getTakeSta());
+
 
         // 瀹氭湡鏇存柊鏁版嵁搴擄紙闄嶄綆棰戠巼锛�
         updateDatabaseIfNeeded();
@@ -129,13 +141,17 @@
      */
     private void parseStationStatus(byte[] content, int index, StaProtocol staProtocol) {
         int offset = index * StationStatusField.ALL.getByteLength();
-        staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(content, offset));
+        staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(content, offset + StationStatusField.TASK_NUMBER.getOffset()));
         staProtocol.setStaNo((int) siemensS7Net.getByteTransform().TransInt16(
                 content, offset + StationStatusField.FINAL_TARGET.getOffset()));
 
-        boolean[] status = siemensS7Net.getByteTransform().TransBool(
+        boolean[] status0 = siemensS7Net.getByteTransform().TransBool(
                 content, offset + StationStatusField.STATUS_WORD.getOffset(),
                 StationStatusField.STATUS_WORD.getByteLength());
+
+        boolean[] status = siemensS7Net.getByteTransform().TransBool(
+                content, offset + StationStatusField.STATUS_WORD.getOffset(),
+                1);
 
         staProtocol.setAutoing(status[0]);
         staProtocol.setLoading(status[1]);
@@ -145,22 +161,32 @@
         staProtocol.setFullPlt(status[5]);
         staProtocol.setHigh(status[6]);
         staProtocol.setLow(status[7]);
+        boolean[] status2 = siemensS7Net.getByteTransform().TransBool(
+                content, offset + StationStatusField.STATUS_WORD.getOffset() + 1,
+                1);
+        staProtocol.setMid(status2[0]);
+        staProtocol.setStartup(status2[1]);
+        staProtocol.setSegApply(status2[2]);
+        staProtocol.setApplyErr(status2[3]);
 
-
+        for (int i = 0; i < 8; i++){
+            if (status0[i+8]==status2[i]){
+                log.info("鐘舵�佺爜涓嶄竴鑷� [id:{}] [i:{}] [status0:{}] [status2:{}]", slave.getId(), i, status0[i], status2[i]);
+            }
+        }
     }
 
     /**
      * 璇诲彇鏉$爜淇℃伅
      */
     private void readBarcodes() {
-        List<Integer> barcodeArr = slave.getBarcodeArr();
-        if (barcodeArr == null || barcodeArr.isEmpty()) {
+        if (slave.getBarcode() == null || !slave.getBarcode()) {
             return;
         }
 
         OperateResultExOne<byte[]> result = siemensS7Net.Read(
                 DeviceField.BARCODE.buildAddress(),
-                (short) (barcodeArr.size() * DeviceField.BARCODE.getByteLength()));
+                (short) (DeviceField.BARCODE.getByteLength() * DeviceField.BARCODE.getArrLength()));
 
         if (!result.IsSuccess) {
             log.warn("璇诲彇鏉$爜澶辫触 [id:{}]", slave.getId());
@@ -168,15 +194,56 @@
         }
 
         byte[] content = result.Content;
-        for (int i = 0; i < barcodeArr.size(); i++) {
+        for (int i = 0; i < DeviceField.BARCODE.getArrLength(); i++) {
+            int[] seg = DeviceField.BARCODE.getSeg();
+            int staNo = siemensS7Net.getByteTransform().TransInt16(
+                    content, seg[0] + i * DeviceField.BARCODE.getByteLength());
             String barcode = siemensS7Net.getByteTransform().TransString(
-                    content, i * DeviceField.BARCODE.getByteLength(),
-                    DeviceField.BARCODE.getByteLength(), "UTF-8");
+                    content, seg[1] + i * DeviceField.BARCODE.getByteLength(),
+                    DeviceField.BARCODE.getByteLength() - seg[1], "UTF-8");
 
-            if (!Cools.isEmpty(barcode)) {
-                StaProtocol staProtocol = station.get(barcodeArr.get(i));
+            if (!Cools.isEmpty(barcode) && staNo != 0) {
+                StaProtocol staProtocol = station.get(staNo);
+                if (staProtocol == null) {
+                    log.warn("鎵爜绔欑偣涓嶅瓨鍦� [id:{}] [staNo:{}]", slave.getId(), staNo);
+                    continue;
+                }
                 staProtocol.setBarcode(barcode);
                 News.info("鏂欑鐮侊細{}", barcode);
+            }
+        }
+    }
+
+    /**
+     * 璇诲彇閲嶉噺淇℃伅
+     */
+    private void readWeight() {
+        if (slave.getWeight() == null || !slave.getWeight()) {
+            return;
+        }
+
+        OperateResultExOne<byte[]> result = siemensS7Net.Read(
+                DeviceField.WEIGHT.buildAddress(),
+                (short) (DeviceField.WEIGHT.getArrLength() * DeviceField.WEIGHT.getByteLength()));
+
+        if (!result.IsSuccess) {
+            log.warn("璇诲彇閲嶉噺澶辫触 [id:{}]", slave.getId());
+            return;
+        }
+        byte[] content = result.Content;
+        for (int i = 0; i < DeviceField.WEIGHT.getArrLength(); i++) {
+            int[] seg = DeviceField.WEIGHT.getSeg();
+            int staNo = siemensS7Net.getByteTransform().TransInt16(
+                    content, seg[0] + i * DeviceField.WEIGHT.getByteLength());
+            Double weight = (double) siemensS7Net.getByteTransform().TransSingle(
+                    content, seg[1] + i * DeviceField.WEIGHT.getByteLength());
+            if (!Cools.isEmpty(weight) && staNo != 0) {
+                StaProtocol staProtocol = station.get(staNo);
+                if (staProtocol == null) {
+                    log.warn("绉伴噸绔欑偣涓嶅瓨鍦� [id:{}] [staNo:{}]", slave.getId(), staNo);
+                    continue;
+                }
+                staProtocol.setWeight(BigDecimal.valueOf(weight).setScale(4, RoundingMode.HALF_UP).doubleValue());
             }
         }
     }
@@ -185,14 +252,13 @@
      * 璇诲彇澶栧舰妫�娴嬮敊璇�
      */
     private void readDimensionErrors() {
-        List<Integer> staNosError = slave.getStaNosError();
-        if (staNosError == null || staNosError.isEmpty()) {
+        if (slave.getStaNosError() == null || !slave.getStaNosError()) {
             return;
         }
 
         OperateResultExOne<byte[]> result = siemensS7Net.Read(
                 DeviceField.DIMENSION_WORD.buildAddress(),
-                (short) (staNosError.size() * DeviceField.DIMENSION_WORD.getByteLength()));
+                (short) (DeviceField.DIMENSION_WORD.getArrLength() * DeviceField.DIMENSION_WORD.getByteLength()));
 
         if (!result.IsSuccess) {
             log.warn("璇诲彇澶栧舰妫�娴嬮敊璇け璐� [id:{}]", slave.getId());
@@ -200,21 +266,35 @@
         }
 
         byte[] content = result.Content;
-        for (int i = 0; i < staNosError.size(); i++) {
-            Integer siteId = staNosError.get(i);
-            StaProtocol staProtocol = station.get(siteId);
+        for (int i = 0; i < DeviceField.DIMENSION_WORD.getArrLength(); i++) {
+            int[] seg = DeviceField.DIMENSION_WORD.getSeg();
+            int staNo = siemensS7Net.getByteTransform().TransInt16(
+                    content, seg[0]);
+            if (staNo != 0) {
+                StaProtocol staProtocol = station.get(staNo);
+                if (staProtocol == null) {
+                    log.warn("寮傚父绔欑偣涓嶅瓨鍦� [id:{}] [staNo:{}]", slave.getId(), staNo);
+                    continue;
+                }
+                boolean[] status = siemensS7Net.getByteTransform().TransBool(
+                        content, seg[1] + i * DeviceField.DIMENSION_WORD.getByteLength(),
+                        1);
+//鏈娇鐢紝棰勭暀
+//                boolean[] status1 = siemensS7Net.getByteTransform().TransBool(
+//                        content, seg[1]+i * DeviceField.DIMENSION_WORD.getByteLength()+1,
+//                        1);
 
-            boolean[] status = siemensS7Net.getByteTransform().TransBool(
-                    content, i * DeviceField.DIMENSION_WORD.getByteLength(),
-                    DeviceField.DIMENSION_WORD.getByteLength());
+                staProtocol.setLeftErr(status[0]);
+                staProtocol.setRightErr(status[1]);
+                staProtocol.setFrontErr(status[2]);
+                staProtocol.setBackErr(status[3]);
+                staProtocol.setHighErr(status[4]);
+                staProtocol.setLoadErr(status[5]);
+                staProtocol.setWeightErr(status[6]);
+                staProtocol.setBarcodeErr(status[7]);
+            }
 
-            staProtocol.setFrontErr(status[0]);
-            staProtocol.setBackErr(status[1]);
-            staProtocol.setHighErr(status[2]);
-            staProtocol.setLeftErr(status[3]);
-            staProtocol.setRightErr(status[4]);
-            staProtocol.setWeightErr(status[5]);
-            staProtocol.setBarcodeErr(status[6]);
+
         }
     }
 
@@ -236,6 +316,7 @@
             Integer siteId = staNos.get(i);
             StaProtocol staProtocol = station.get(siteId);
             if (staProtocol == null) {
+                log.warn("绔欑偣涓嶅瓨鍦� [id:{}] [staNo:{}]", slave.getId(), siteId);
                 continue;
             }
 
@@ -254,6 +335,64 @@
     }
 
     /**
+     * 璇诲彇瀹夊叏浜や簰淇″彿
+     */
+    private void readSaleSingle(List<DevpSlave.Sta> putSta, List<DevpSlave.Sta> takeSta) {
+        int staNoSize = putSta.size();
+        OperateResultExOne<byte[]> result = siemensS7Net.Read(
+                SafeSignalField.SAFE_SIGNAL_FROM_CONVEYOR.buildAddress(),
+                (short) (staNoSize * SafeSignalField.SAFE_SIGNAL_FROM_CONVEYOR.getByteLength()));
+
+
+        if (!result.IsSuccess) {
+            log.warn("璇诲彇PLC瀹夊叏浜や簰淇℃伅寮傚父 [id:{}]", slave.getId());
+            return;
+        }
+
+        byte[] content = result.Content;
+        for (int i = 0; i < staNoSize; i++) {
+            Integer put = putSta.get(i).getStaNo();
+            Integer take = takeSta.get(i).getStaNo();
+            StaProtocol staProtocolPut = station.get(put);
+            StaProtocol staProtocolTake = station.get(take);
+            if (staProtocolPut == null || staProtocolTake == null) {
+                log.warn("绔欑偣涓嶅瓨鍦� [id:{}] [staNo:{},{}]", slave.getId(), put, take);
+                continue;
+            }
+
+            boolean[] status = siemensS7Net.getByteTransform().TransBool(
+                    content, i * SafeSignalField.SAFE_SIGNAL_FROM_CONVEYOR.getByteLength(), 1);
+
+            boolean[] status2 = siemensS7Net.getByteTransform().TransBool(
+                    content, i * SafeSignalField.SAFE_SIGNAL_FROM_CONVEYOR.getByteLength() + 2, 1);
+
+            staProtocolPut.setHeartbeat(status[0]);
+            staProtocolPut.setAllowTake(status[1]);
+            staProtocolPut.setAllowPut(status[2]);
+            staProtocolPut.setInComplete(status[3]);
+            staProtocolPut.setOutComplete(status[4]);
+
+            staProtocolTake.setHeartbeat(status2[0]);
+            staProtocolTake.setAllowTake(status2[1]);
+            staProtocolTake.setAllowPut(status2[2]);
+            staProtocolTake.setInComplete(status2[3]);
+            staProtocolTake.setOutComplete(status2[4]);
+            //
+            if (status[3]) {
+                staProtocolPut.setSafeSignal( new SafeSignal(i, (short) 0, true));
+                redis.push(RedisConveyorConstant.CONVEYOR_SAFE_FLAG,staProtocolPut);
+                News.info("瀹夊叏浜や簰淇″彿澶嶄綅 [id:{}] [staNo:{}]", slave.getId(), staProtocolPut.getSiteId());
+            }
+            if (status2[4]) {
+                staProtocolTake.setSafeSignal(new SafeSignal(i, (short) 0, false));
+                redis.push(RedisConveyorConstant.CONVEYOR_SAFE_FLAG, staProtocolTake);
+                News.info("瀹夊叏浜や簰淇″彿澶嶄綅 [id:{}] [staNo:{}]", slave.getId(), staProtocolPut.getSiteId());
+            }
+
+        }
+    }
+
+    /**
      * 鎸夐渶鏇存柊鏁版嵁搴擄紙闄嶄綆鏇存柊棰戠巼锛�
      */
     private void updateDatabaseIfNeeded() {

--
Gitblit v1.9.1