From 1319cb7072623dff12369a889af766db2d166def Mon Sep 17 00:00:00 2001
From: lss <2538410689@qq.com>
Date: 星期一, 28 七月 2025 17:58:47 +0800
Subject: [PATCH] 叠盘机优化

---
 HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs |  118 ++++++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 76 insertions(+), 42 deletions(-)

diff --git a/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs b/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs
index 09d78ee..15d72f4 100644
--- a/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs
+++ b/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs
@@ -8,6 +8,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 using System.Web.Services.Description;
 using System.Web.UI.WebControls.WebParts;
@@ -22,27 +23,27 @@
     {
         private static bool debug = true;
         private static S7.Net.Plc plc = null;
-     
-        static S7Helper()
-        {
-            Init();
-        }
-        private static Dictionary<string, Plc> plcDic = new Dictionary<string, Plc>();
-        private static void Init()
-        {
-            //閰嶇疆鏂囦欢璇诲彇鎵�湁鐨刾lc杩涜鍒濆鍖�-            try
-            {
-                var plc1 = new Plc(CpuType.S71500, "", 0, 1);
-                plcDic.Add("plc1", plc1);
-                Link(plc1);
-            }
-            catch (Exception ex)
-            {
 
-                Console.WriteLine("S7Helper Init err=" + ex.Message);
-            }
-        }
+        //static S7Helper()
+        //{
+        //    Init();
+        //}
+        private static Dictionary<string, Plc> plcDic = new Dictionary<string, Plc>();
+        //private static void Init()
+        //{
+        //    //閰嶇疆鏂囦欢璇诲彇鎵�湁鐨刾lc杩涜鍒濆鍖�+        //    try
+        //    {
+        //        var plc1 = new Plc(CpuType.S71500, "", 0, 1);
+        //        plcDic.Add("plc1", plc1);
+        //        Link(plc1);
+        //    }
+        //    catch (Exception ex)
+        //    {
+
+        //        Console.WriteLine("S7Helper Init err=" + ex.Message);
+        //    }
+        //}
         private static Plc GetPlc(string plc)
         {
             if (plcDic.ContainsKey(plc))
@@ -78,7 +79,7 @@
             catch (Exception ex)
             {
                 // Console.WriteLine($"plc{plc.IP}杩炴帴澶辫触锛宔rr={ex.Message}");
-                LogHelper.Info($"Link Error plc{plc.IP}杩炴帴澶辫触锛宔rr={ex.Message}");
+                LogHelper.Error($"Link Error plc{plc.IP}杩炴帴澶辫触锛宔rr={ex.Message}", ex);
                 //Init();
             }
 
@@ -86,44 +87,63 @@
         //https://www.ad.siemens.com.cn/productportal/Prods/S7-1200_PLC_EASY_PLUS/SmartSMS/060.html
         //https://www.ad.siemens.com.cn/productportal/Prods/S7-1200_PLC_EASY_PLUS/07-Program/02-basic/01-Data_Type/09-String.html
 
+        private static object _lockdpj = new object();
+
         /// <summary>
         /// 鍚堣偉浣抽�璇诲彇鍙犵洏鏈�         /// </summary>
         /// <param name="startByte">鍋忕Щ閲忓湴鍧�vb2001</param>
         ///  /// <param name="varType">鍊肩被鍨�/param>
         /// <returns></returns>
-        public static int ReadDpj(int startByte, S7.Net.VarType varType)
+        public static int ReadDpj(string ip, int startByte, S7.Net.VarType varType)
         {
-            string ip = "10.68.9.15"; short port = 102;
+            // string ip = "10.68.9.15"; 
+            short port = 102;
+
             try
             {
-                var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 0);
+
+                var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 1);
+                if (plc.IsConnected == true)
+                {
+                    LogHelper.Info($"璇ヨ澶囨鍦ㄥ鐞嗗叾浠栦氦浜抂{ip}]");
+                    throw new Exception("PLC鍗忚灞傝繛鎺ュけ璐�);
+                }
                 LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port}");
                 S7Helper.Link(plc);
                 int result = 0;
+                // 楠岃瘉杩炴帴鐘舵�
+                if (plc.IsConnected != true)
+                {
+                    LogHelper.Info($"鐗╃悊杩炴帴鎴愬姛浣嗗崗璁眰鏈氨缁猍{ip}]");
+                    throw new Exception("PLC鍗忚灞傝繛鎺ュけ璐�);
+                }
+
                 if (varType == VarType.Int)
                 {
                     var value = (short)plc.Read(DataType.DataBlock, 1, startByte, VarType.Int, 1);
                     LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},璇诲彇绫诲瀷{varType},璇诲彇鍊納value}");
                     result = int.Parse(value.ToString());
                 }
-                else
+                else//鍚堣偉浣抽�闄や簡鏁伴噺锛屽叾浠栭兘鏄痓yte绫诲瀷
                 {
 
                     byte value = (byte)plc.Read(DataType.DataBlock, 1, startByte, VarType.Byte, 1);
                     LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},璇诲彇绫诲瀷{varType},璇诲彇鍊納value}");
                     result = value;
                 }
-
+                plc.Close();
                 LogHelper.Info($"璇诲彇鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},鍦板潃锛歿startByte},璇诲彇鍊艰浆鎹�{result}");
+
                 return result;
             }
             catch (Exception ex)
             {
-                LogHelper.Info($"ReadDpj Error{ex}");
+                plc.Close();
+                LogHelper.Info($"鍙犵洏鏈�{ip}閾炬帴澶辫触");
+                LogHelper.Error($"ReadDpj ip:{ip},startByte:{startByte} Error:{ex}", ex);
                 throw;
             }
-
 
         }
         /// <summary>
@@ -132,43 +152,57 @@
         /// <param name="startByte">鍋忕Щ閲忓湴鍧�/param>
         /// <param name="value">鍐欏叆鍊�byte绫诲瀷鍙兘0 1</param>
         /// <returns></returns>
-        public static bool WriteDpj(int startByte, int value)
+        public static bool WriteDpj(string ip, int startByte, byte value)
         {
             bool result = false;
-            string ip = "10.68.9.15"; short port = 102;
+            // string ip = "10.68.9.15"; 
+            short port = 102;
+
             try
             {
-                var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 0);
+                var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 1);
 
                 LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port}");
                 S7Helper.Link(plc);
+                if (plc.IsConnected != true)
+                {
+                    LogHelper.Info($"鐗╃悊杩炴帴鎴愬姛浣嗗崗璁眰鏈氨缁猍{ip}]");
+                    throw new Exception("PLC鍗忚灞傝繛鎺ュけ璐�);
+                }
+                // plc.Write($"VB{startByte}", value);
+                Thread.Sleep(300);
                 plc.Write(
                    dataType: DataType.DataBlock,
                    db: 1,
                    startByteAdr: startByte,
                    value: value
                );
+
                 LogHelper.Info($"鍐欏叆鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},鍦板潃锛歿startByte},鍐欏叆鍊�{value}");
-                byte[] readBack = plc.ReadBytes(DataType.DataBlock, 1, startByte, 1);
-                if (readBack[0] == value)
+                Thread.Sleep(300);
+                byte fdvalue = (byte)plc.Read(DataType.DataBlock, 1, startByte, VarType.Byte, 1);
+                if (fdvalue == value)
                 {
                     result = true;
-                    LogHelper.Info($"鍙犵洏鏈哄啓鍏ユ垚鍔燂紝鍥炶鍊硷細{readBack[0]}");
+                    LogHelper.Info($"鍙犵洏鏈哄啓鍏ユ垚鍔燂紝鍥炶鍊硷細{fdvalue}");
                 }
-
+                else
+                {
+                    // Task.Run(() => WriteDpj(ip, startByte, value));
+                    LogHelper.Info($"鍙犵洏鏈哄啓鍏ュけ璐ワ紝鍥炶鍊硷細{fdvalue}");
+                }
+                plc.Close();
                 return result;
             }
             catch (Exception ex)
             {
-                LogHelper.Info($"WriteDpj Error{ex}");
-                throw;
+                plc.Close();
+                // Task.Run(() => WriteDpj(ip, startByte, value));
+                LogHelper.Info($"鍙犵洏鏈�{ip}閾炬帴澶辫触");
+                LogHelper.Error($"WriteDpj ip:{ip},startByte:{startByte} Error:{ex}", ex);
+                return false;
             }
-
-
             // S7-200 Smart鐨刅鍖轰篃鏄犲皠涓篋B1
-
-
-
         }
         internal static short[] ReadInt(string device, int db, int byteAddr, int count)
         {

--
Gitblit v1.9.1