From 00e3e465859114706fa2f802a9b4d3dbbee17a36 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期五, 13 十一月 2020 16:54:51 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/DevpThread.java |  105 +++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 70 insertions(+), 35 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/DevpThread.java b/src/main/java/com/zy/core/thread/DevpThread.java
index 3b57b24..90d5837 100644
--- a/src/main/java/com/zy/core/thread/DevpThread.java
+++ b/src/main/java/com/zy/core/thread/DevpThread.java
@@ -2,7 +2,8 @@
 
 import HslCommunication.Core.Types.OperateResult;
 import HslCommunication.Core.Types.OperateResultExOne;
-import HslCommunication.Profinet.Melsec.MelsecMcNet;
+import HslCommunication.Profinet.Siemens.SiemensPLCS;
+import HslCommunication.Profinet.Siemens.SiemensS7Net;
 import com.alibaba.fastjson.JSON;
 import com.core.common.DateUtils;
 import com.core.common.SpringUtils;
@@ -34,7 +35,7 @@
 public class DevpThread implements Runnable, ThreadHandler {
 
     private DevpSlave slave;
-    private MelsecMcNet melsecMcNet;
+    private SiemensS7Net siemensS7Net;
     private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>();
     private short heartBeatVal = 1;
 
@@ -75,7 +76,7 @@
                 }
 
                 // 蹇冭烦
-//                heartbeat();
+                heartbeat();
                 Thread.sleep(400);
             } catch (Exception e) {
                 e.printStackTrace();
@@ -87,25 +88,27 @@
     @Override
     public boolean connect() {
         boolean result = false;
-        melsecMcNet = new MelsecMcNet(slave.getIp(), slave.getPort());
-        OperateResult connect = melsecMcNet.ConnectServer();
+        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp());
+        siemensS7Net.setRack(slave.getRack().byteValue());
+        siemensS7Net.setSlot(slave.getSlot().byteValue());
+        OperateResult connect = siemensS7Net.ConnectServer();
         if(connect.IsSuccess){
             result = true;
-            OutputQueue.DEVP.offer(MessageFormat.format( "銆恵0}銆戣緭閫佺嚎plc杩炴帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+            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()));
             log.info("杈撻�佺嚎plc杩炴帴鎴愬姛 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
         } else {
-            OutputQueue.DEVP.offer(MessageFormat.format( "銆恵0}銆戣緭閫佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+            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()));
             log.error("杈撻�佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
         }
-        melsecMcNet.ConnectClose();
+        siemensS7Net.ConnectClose();
         return result;
     }
 
     /**
      * 璇诲彇鐘舵�� ====> 鏁村潡plc
      */
-    private void read(){
-        OperateResultExOne<byte[]> result = melsecMcNet.Read("D101", (short) 30);
+    private void read() throws InterruptedException {
+        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB100.0", (short) 40);
         if (result.IsSuccess) {
             for (int i = 1; i <= 8; i++) {
                 StaProtocol staProtocol = station.get(i);
@@ -114,22 +117,39 @@
                     staProtocol.setSiteId(i);
                     station.put(i, staProtocol);
                 }
-                staProtocol.setWorkNo(melsecMcNet.getByteTransform().TransInt16(result.Content, (i-1)*2));     // 宸ヤ綔鍙�
-                staProtocol.setStaNo(melsecMcNet.getByteTransform().TransInt16(result.Content, (i-1)*2+40));   // 鐩爣绔�
+                staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt16(result.Content, (i-1)*2));     // 宸ヤ綔鍙�
+                staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, (i-1)*2+20));   // 鐩爣绔�
             }
         }
-        OperateResultExOne<boolean[]> result1 = melsecMcNet.ReadBool("M800", (short) 64);
+        Thread.sleep(100);
+        OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB100.40", (short) 8);
         if (result1.IsSuccess) {
             for (int i = 1; i <= 8; i++) {
+                boolean[] status = siemensS7Net.getByteTransform().TransBool(result1.Content, i-1, 1);
                 StaProtocol staProtocol = station.get(i);
-                staProtocol.setAutoing(result1.Content[(i-1)*8]);  // 鑷姩
-                staProtocol.setLoading(result1.Content[(i-1)*8+1]);  // 鏈夌墿
-                staProtocol.setInEnable(result1.Content[(i-1)*8+2]); // 鍙叆
-                staProtocol.setOutEnable(result1.Content[(i-1)*8+3]);// 鍙嚭
-                staProtocol.setEmptyMk(result1.Content[(i-1)*8+4]);  // 绌烘澘淇″彿
+                staProtocol.setAutoing(status[0]);  // 鑷姩
+                staProtocol.setLoading(status[1]);  // 鏈夌墿
+                staProtocol.setInEnable(status[2]); // 鍙叆
+                staProtocol.setOutEnable(status[3]);// 鍙嚭
+                staProtocol.setEmptyMk(status[4]);  // 绌烘澘淇″彿
+
+                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
+                    staProtocol.setPakMk(true);
+                }
             }
         }
+        // 鍙拌溅浣嶇疆
+        OperateResultExOne<Short> result2 = siemensS7Net.ReadInt16("DB100.48");
+        if (result2.IsSuccess) {
+            StaProtocol staProtocol = station.get(8);
+            staProtocol.setPosition(result2.Content);
+        } else {
+            OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆�8鍙峰彴杞﹁幏鍙栧疄鏃朵綅缃け璐ワ紒锛侊紒", DateUtils.convert(new Date())));
+            log.error("8鍙峰彴杞﹁幏鍙栧疄鏃朵綅缃け璐ワ紒锛侊紒");
+        }
+
         if (result.IsSuccess && result1.IsSuccess) {
+
             OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId()));
 
             // 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴�
@@ -144,25 +164,26 @@
                     throw new Exception("鏇存柊鏁版嵁搴撴暟鎹け璐�");
                 }
             } catch (Exception e) {
-                OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戞洿鏂版暟鎹簱鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
-                log.error("鏇存柊鏁版嵁搴撴暟鎹け璐� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+                OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戞洿鏂版暟鎹簱鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
+                log.error("鏇存柊鏁版嵁搴撴暟鎹け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
             }
 
         } else {
-            OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戣鍙栬緭閫佺嚎plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
-            log.error("璇诲彇鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}] ", slave.getId(), slave.getIp(), slave.getPort());
+            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()));
+            log.error("璇诲彇鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
         }
     }
 
     /**
      * 鍐欏叆 ID+鐩爣绔� =====> 鍗曠珯鐐瑰啓鍏�
      */
-    private void write(StaProtocol staProtocol){
+    private void write(StaProtocol staProtocol) throws InterruptedException {
         if (null == staProtocol) {
             return;
         }
-        OperateResult write = melsecMcNet.Write("D10" + staProtocol.getSiteId(), staProtocol.getWorkNo());    // 宸ヤ綔鍙�
-        OperateResult write1 = melsecMcNet.Write("D12" + staProtocol.getSiteId(), staProtocol.getStaNo());    // 鐩爣绔�
+        OperateResult write = siemensS7Net.Write("DB100." + (staProtocol.getSiteId() - 1) * 2, staProtocol.getWorkNo());    // 宸ヤ綔鍙�
+        Thread.sleep(300);
+        OperateResult write1 = siemensS7Net.Write("DB100." + ((staProtocol.getSiteId() - 1) * 2 + 20), staProtocol.getStaNo());    // 鐩爣绔�
 //        boolean[] status = new boolean[8];
 //        status[0] = staProtocol.isAutoing();
 //        status[1] = staProtocol.isLoading();
@@ -171,10 +192,15 @@
 //        status[4] = staProtocol.isEmptyMk();
 //        OperateResult write2 = siemensS7Net.Write("DB100." + ((staProtocol.getSiteId() - 1) + 40), status);     //  鐘舵��
         if (!write.IsSuccess || !write1.IsSuccess) {
+            staProtocol = station.get(staProtocol.getSiteId());
+            if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) {
+                staProtocol.setPakMk(true);
+            }
             OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol)));
             log.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol));
         } else {
-            OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol)));
+            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));
         }
     }
 
@@ -185,7 +211,7 @@
         if (null == staProtocol) {
             return;
         }
-        OperateResult write = melsecMcNet.Write("D10" + staProtocol.getSiteId(), staProtocol.getWorkNo());    // 宸ヤ綔鍙�
+        OperateResult write = siemensS7Net.Write("DB100." + (staProtocol.getSiteId() - 1) * 2, staProtocol.getWorkNo());    // 宸ヤ綔鍙�
         if (!write.IsSuccess ) {
             OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol)));
             log.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol));
@@ -201,7 +227,7 @@
         if (null == staProtocol) {
             return;
         }
-        OperateResult write = melsecMcNet.Write("D12" + staProtocol.getSiteId(), staProtocol.getStaNo());    // 鐩爣绔�
+        OperateResult write = siemensS7Net.Write("DB100." + ((staProtocol.getSiteId() - 1) * 2 + 20), staProtocol.getStaNo());    // 鐩爣绔�
         if (!write.IsSuccess ) {
             OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol)));
             log.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol));
@@ -219,21 +245,30 @@
         } else {
             heartBeatVal = 1;
         }
-        OperateResult write = melsecMcNet.Write("DB100.50", heartBeatVal);
+        OperateResult write = siemensS7Net.Write("DB100.50", heartBeatVal);
         if (!write.IsSuccess) {
             log.error("杈撻�佺嚎plc缂栧彿={} 蹇冭烦澶辫触", slave.getId());
         }
     }
 
-    @Override
-    public void close() {
-        melsecMcNet.ConnectClose();
+    /**
+     * 璁剧疆鍏ュ簱鏍囪
+     */
+    public void setPakMk(Integer siteId, boolean pakMk) {
+        StaProtocol staProtocol = station.get(siteId);
+        if (null != staProtocol) {
+            staProtocol.setPakMk(pakMk);
+        }
     }
 
-    public static void main(String[] args) {
+    @Override
+    public void close() {
+        siemensS7Net.ConnectClose();
+    }
+
+    public static void main(String[] args) throws Exception {
         DevpSlave slave = new DevpSlave();
-        slave.setIp("192.168.3.65");
-        slave.setPort(6000);
+        slave.setIp("192.168.2.125");
         DevpThread devpThread = new DevpThread(slave);
         devpThread.connect();
         devpThread.read();

--
Gitblit v1.9.1