From f1b654fc854bd0a88ef7ee9815f8cfaf33e30007 Mon Sep 17 00:00:00 2001
From: TQS <56479841@qq.com>
Date: 星期四, 20 四月 2023 18:13:52 +0800
Subject: [PATCH] #

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

diff --git a/src/main/java/com/zy/core/thread/SiemensDevpThread.java b/src/main/java/com/zy/core/thread/SiemensDevpThread.java
index cba849c..5978508 100644
--- a/src/main/java/com/zy/core/thread/SiemensDevpThread.java
+++ b/src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -88,7 +88,8 @@
                         break;
                     // 鍐欐暟鎹� ID+鐩爣绔�
                     case 2:
-                        write((StaProtocol)task.getData(), 0);
+//                        write((StaProtocol)task.getData(), 0);
+                        write((StaProtocol)task.getData());
                         Thread.sleep(300);
                         read();
                         break;
@@ -120,7 +121,7 @@
             OutputQueue.DEVP.offer(MessageFormat.format( "銆恵0}銆戣緭閫佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{1}] [ip:{2}] [port:{3}]  [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
             News.error("杈撻�佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
         }
-        siemensS7Net.ConnectClose();
+//        siemensS7Net.ConnectClose();
         return result;
     }
 
@@ -180,7 +181,7 @@
         }
 
         //璇绘潯鐮�
-        Thread.sleep(100);
+        Thread.sleep(200);
         OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB100.100",(short)(barcodeSize*8));
         if (result2.IsSuccess) {
             for (int i = 0; i < barcodeSize; i++) {
@@ -194,7 +195,7 @@
         }
 
         // 鍏呯數淇″彿浣�
-        Thread.sleep(100);
+        Thread.sleep(200);
         OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB102.50", (short)1);
         if (result3.IsSuccess) {
             boolean[] status = siemensS7Net.getByteTransform().TransBool(result3.Content, 0, 1);
@@ -232,8 +233,122 @@
     /**
      * 鍐欏叆 ID+鐩爣绔� =====> 鍗曠珯鐐瑰啓鍏�
      */
+    private void write(StaProtocol staProtocol) throws InterruptedException {
+        if (null == staProtocol) {
+            return;
+        }
+        int index = staNos.indexOf(staProtocol.getSiteId());
+
+        OperateResult writeId,writeDest;
+//        //浠诲姟ID涓嬪彂娆℃暟
+        int idCount = 0;
+        boolean idFlag = false;
+        while(idCount < 5){
+            writeId = siemensS7Net.Write("DB100." + index*2, staProtocol.getWorkNo());    // 宸ヤ綔鍙�
+            if(writeId.IsSuccess){
+                Thread.sleep(200);
+                OperateResultExOne<byte[]> readId = siemensS7Net.Read("DB100." + index*2, (short)2);
+                if(readId.IsSuccess){
+                    short workNo = siemensS7Net.getByteTransform().TransInt16(readId.Content, 0);
+                    if(staProtocol.getWorkNo().equals(workNo)){
+                        //宸ヤ綔鍙峰啓鍏ユ垚鍔�
+                        idFlag = true;
+                        break;
+                    } else {//杩斿洖缁撴灉鏄垚鍔熶簡锛屼絾鏄湡瀹炲�间笉鐩稿悓
+                        idCount++;
+                        OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎宸ヤ綔鍙峰悗杩斿洖鎴愬姛锛屼絾鏄鍙栧伐浣滃彿鍊间笉涓�鑷淬�傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2},鍐欏叆娆℃暟={3}",
+                                slave.getId(), JSON.toJSON(staProtocol),idCount));
+                        log.error("鍐欏叆杈撻�佺嚎宸ヤ綔鍙峰悗杩斿洖鎴愬姛锛屼絾鏄鍙栧伐浣滃彿鍊间笉涓�鑷淬�傝緭閫佺嚎plc缂栧彿={}锛寋}锛屽啓鍏ユ鏁�={}", slave.getId(), JSON.toJSON(staProtocol), idCount);
+                    }
+                } else {
+                    idCount++;
+                    OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎宸ヤ綔鍙峰悗璇诲彇宸ヤ綔鍙峰け璐ャ�傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2},鍐欏叆娆℃暟={3}",
+                            slave.getId(), JSON.toJSON(staProtocol), idCount));
+                    log.error("鍐欏叆杈撻�佺嚎宸ヤ綔鍙峰悗璇诲彇宸ヤ綔鍙峰け璐}銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={},鍐欏叆娆℃暟={}", readId.Message, slave.getId(), JSON.toJSON(staProtocol), idCount);
+                }
+            } else {
+                idCount++;
+                OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎宸ヤ綔鍙峰け璐ャ�傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2},鍐欏叆娆℃暟={3}",
+                        slave.getId(), JSON.toJSON(staProtocol),idCount));
+                log.error("鍐欏叆杈撻�佺嚎宸ヤ綔鍙峰け璐}銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={},鍐欏叆娆℃暟={}", writeId.Message, slave.getId(), JSON.toJSON(staProtocol), idCount);
+            }
+            Thread.sleep(200);
+        }
+
+        //鍐橧D灏濊瘯浜�5娆¤繕鏄け璐ヤ簡
+        if(!idFlag){
+            staProtocol = station.get(staProtocol.getSiteId());
+            if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) {
+                staProtocol.setPakMk(true);
+            }
+            OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎宸ヤ綔鍙峰皾璇�5娆″け璐ャ�傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol)));
+            log.error("鍐欏叆杈撻�佺嚎宸ヤ綔鍙峰皾璇�5娆″け璐ャ�傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol));
+
+            //閲嶆柊娣诲姞鏁版嵁鍒颁换鍔¢槦鍒�
+            boolean result = MessageQueue.offer(SlaveType.Devp, slave.getId(), new Task(2, staProtocol));
+//            read();//璇诲彇1娆¤澶囩姸鎬�
+            return;
+        }
+
+        //浠诲姟鐩爣绔欎笅鍙戞鏁�
+        int destCount = 0;
+        boolean destFlag = false;
+        while(destCount < 5){
+            writeDest = siemensS7Net.Write("DB101." + index*2, staProtocol.getStaNo());    // 鐩爣绔�
+            if(writeDest.IsSuccess){
+                Thread.sleep(200);
+                OperateResultExOne<byte[]> readDest = siemensS7Net.Read("DB101." + index*2, (short)2);
+                if(readDest.IsSuccess){
+                    short staNo = siemensS7Net.getByteTransform().TransInt16(readDest.Content, 0);
+                    if(staProtocol.getStaNo().equals(staNo)){
+                        //鐩爣绔欏啓鍏ユ垚鍔�
+                        destFlag = true;
+                        break;
+                    } else {//杩斿洖缁撴灉鏄垚鍔熶簡锛屼絾鏄湡瀹炲�间笉鐩稿悓
+                        destCount++;
+                        OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎鐩爣绔欏悗杩斿洖鎴愬姛锛屼絾鏄鍙栫洰鏍囩珯鍊间笉涓�鑷淬�傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2},鍐欏叆娆℃暟={3}",
+                                slave.getId(), JSON.toJSON(staProtocol),destCount));
+                        log.error("鍐欏叆杈撻�佺嚎鐩爣绔欏悗杩斿洖鎴愬姛锛屼絾鏄鍙栫洰鏍囩珯鍊间笉涓�鑷淬�傝緭閫佺嚎plc缂栧彿={}锛寋},鍐欏叆娆℃暟={}", slave.getId(), JSON.toJSON(staProtocol), destCount);
+                    }
+                } else {
+                    destCount++;
+                    OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎鐩爣绔欏悗璇诲彇鐩爣绔欏け璐ャ�傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2},鍐欏叆娆℃暟={3}",
+                            slave.getId(), JSON.toJSON(staProtocol), destCount));
+                    log.error("鍐欏叆杈撻�佺嚎鐩爣绔欏悗璇诲彇鐩爣绔欏け璐}銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}锛屽啓鍏ユ鏁�={}", readDest.Message, slave.getId(), JSON.toJSON(staProtocol), destCount);
+                }
+            } else {
+                destCount++;
+                OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎鐩爣绔欏け璐ャ�傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2},鍐欏叆娆℃暟={3}",
+                        slave.getId(), JSON.toJSON(staProtocol),destCount));
+                log.error("鍐欏叆杈撻�佺嚎鐩爣绔欏け璐}銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={},鍐欏叆娆℃暟={}", writeDest.Message, slave.getId(), JSON.toJSON(staProtocol), destCount);
+            }
+            Thread.sleep(200);
+        }
+
+        //鍐欑洰鏍囩珯灏濊瘯浜�5娆¤繕鏄け璐ヤ簡
+        if(!destFlag){
+            staProtocol = station.get(staProtocol.getSiteId());
+            if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) {
+                staProtocol.setPakMk(true);
+            }
+            OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎鐩爣绔欏皾璇�5娆″け璐ャ�傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol)));
+            log.error("鍐欏叆杈撻�佺嚎鐩爣绔欏皾璇�5娆″け璐ャ�傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol));
+
+            //閲嶆柊娣诲姞鏁版嵁鍒颁换鍔¢槦鍒�
+            boolean result = MessageQueue.offer(SlaveType.Devp, slave.getId(), new Task(2, staProtocol));
+//            read();//璇诲彇1娆¤澶囩姸鎬�
+            return;
+        }
+
+        OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆� 杈撻�佺嚎鍛戒护涓嬪彂鎴愬姛 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol)));
+        log.info("杈撻�佺嚎鍛戒护涓嬪彂 [id:{}] >>>>> 鍛戒护涓嬪彂鎴愬姛锛� {}",  slave.getId(), JSON.toJSON(staProtocol));
+    }
+
+    /**
+     * 鍐欏叆 ID+鐩爣绔� =====> 鍗曠珯鐐瑰啓鍏�
+     */
     private void write(StaProtocol staProtocol, int times) throws InterruptedException {
-        if (null == staProtocol || times > 2) {
+        if (null == staProtocol || times > 3) {
             return;
         }
         int index = staNos.indexOf(staProtocol.getSiteId());
@@ -242,9 +357,9 @@
         OperateResult write1 = siemensS7Net.Write("DB101." + index*2, staProtocol.getStaNo());    // 鐩爣绔�
 
         if (!write.IsSuccess || !write1.IsSuccess) {
-            MessageQueue.offer(SlaveType.Devp, slave.getId(), new Task(2, staProtocol));
+//            MessageQueue.offer(SlaveType.Devp, slave.getId(), new Task(2, staProtocol));
             OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol)));
-            News.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触锛岄噸鏂版坊鍔犱换鍔″埌闃熷垪銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol));
+            News.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触==>>{}--{}锛岄噸鏂版坊鍔犱换鍔″埌闃熷垪銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", write.Message, write1.Message, slave.getId(), JSON.toJSON(staProtocol));
             times++;
             write(staProtocol, times);
 //            staProtocol = station.get(staProtocol.getSiteId());
@@ -327,16 +442,7 @@
         slave.setRack(0);
         slave.setSlot(0);
         SiemensDevpThread devpThread = new SiemensDevpThread(slave);
-        SiemensS7Net siemensS7Net = devpThread.getSiemensS7Net();
-        siemensS7Net.setRack(slave.getRack().byteValue());
-        siemensS7Net.setSlot(slave.getSlot().byteValue());
-        OperateResult connect = siemensS7Net.ConnectServer();
-        if(connect.IsSuccess){
-            News.info("杈撻�佺嚎plc杩炴帴鎴愬姛 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
-        } else {
-            News.error("杈撻�佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
-        }
-        siemensS7Net.ConnectClose();
+        devpThread.connect();
         // 鍐�
 //        StaProtocol staProtocol = devpThread.getStation().get(1);
 //        staProtocol.setWorkNo((short) 232);

--
Gitblit v1.9.1