From 011d17bc8df981ba31e895873ab0e4a17da8288f Mon Sep 17 00:00:00 2001
From: lss <2538410689@qq.com>
Date: 星期四, 26 六月 2025 17:46:13 +0800
Subject: [PATCH] 优化

---
 HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs |   67 +++++++++++++++++----------------
 1 files changed, 35 insertions(+), 32 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 97eaf35..fb64156 100644
--- a/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs
+++ b/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs
@@ -23,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))
@@ -79,7 +79,7 @@
             catch (Exception ex)
             {
                 // Console.WriteLine($"plc{plc.IP}杩炴帴澶辫触锛宔rr={ex.Message}");
-                LogHelper.Error($"Link Error plc{plc.IP}杩炴帴澶辫触锛宔rr={ex.Message}",ex);
+                LogHelper.Error($"Link Error plc{plc.IP}杩炴帴澶辫触锛宔rr={ex.Message}", ex);
                 //Init();
             }
 
@@ -87,7 +87,7 @@
         //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();
+        private static object _lockdpj = new object();
 
         /// <summary>
         /// 鍚堣偉浣抽�璇诲彇鍙犵洏鏈�@@ -95,9 +95,9 @@
         /// <param name="startByte">鍋忕Щ閲忓湴鍧�vb2001</param>
         ///  /// <param name="varType">鍊肩被鍨�/param>
         /// <returns></returns>
-        public static int ReadDpj(string ip,int startByte, S7.Net.VarType varType)
+        public static int ReadDpj(string ip, int startByte, S7.Net.VarType varType)
         {
-           // string ip = "10.68.9.15"; 
+            // string ip = "10.68.9.15"; 
             short port = 102;
             lock (_lockdpj)
             {
@@ -131,7 +131,7 @@
                     throw;
                 }
             }
-          
+
 
 
         }
@@ -141,10 +141,10 @@
         /// <param name="startByte">鍋忕Щ閲忓湴鍧�/param>
         /// <param name="value">鍐欏叆鍊�byte绫诲瀷鍙兘0 1</param>
         /// <returns></returns>
-        public static bool WriteDpj(string ip,int startByte, byte value)
+        public static bool WriteDpj(string ip, int startByte, byte value)
         {
             bool result = false;
-           // string ip = "10.68.9.15"; 
+            // string ip = "10.68.9.15"; 
             short port = 102;
             lock (_lockdpj)
             {
@@ -154,7 +154,7 @@
 
                     LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port}");
                     S7Helper.Link(plc);
-                   // plc.Write($"VB{startByte}", value);
+                    // plc.Write($"VB{startByte}", value);
                     Thread.Sleep(300);
                     plc.Write(
                        dataType: DataType.DataBlock,
@@ -172,18 +172,21 @@
                     }
                     else
                     {
+                       // Task.Run(() => WriteDpj(ip, startByte, value));
                         LogHelper.Info($"鍙犵洏鏈哄啓鍏ュけ璐ワ紝鍥炶鍊硷細{fdvalue}");
                     }
 
-                        return result;
+                    return result;
                 }
                 catch (Exception ex)
                 {
-                    LogHelper.Info($"WriteDpj Error{ex}");
-                    throw;
+                   // 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

--
Gitblit v1.9.1