Browse Source

plc连接放在配置加载后面

刘彬 2 weeks ago
parent
commit
909fc501f8
2 changed files with 12 additions and 12 deletions
  1. 9 9
      Core/Management.cs
  2. 3 3
      ViewModels/WorkRunViewModel.cs

+ 9 - 9
Core/Management.cs

@@ -97,8 +97,7 @@ namespace DefaultEdit.Core
         private Thread  WorkThread;
         public Management()
         {
-            s7PlcCommunicate = new S7PlcCommunicate(CurentApplicationSettings.PlcIP);
-            s7PlcCommunicate.OpenPlc();
+
             for ( int i = 0; i < 6; i++ )
             {
                 CurrStationRfid.Add("");
@@ -112,7 +111,13 @@ namespace DefaultEdit.Core
            
         }
 
+        public void ConnectPlc()
+        {
+
+            s7PlcCommunicate = new S7PlcCommunicate(CurentApplicationSettings.PlcIP);
+            s7PlcCommunicate.OpenPlc();
 
+        }
         private void Management_RuningToWorkEvent(string key, bool value)
         {
             switch ( key )
@@ -279,7 +284,7 @@ namespace DefaultEdit.Core
             {
                 try
                 {
-                    if ( s7PlcCommunicate.IsConnected )
+                    if (s7PlcCommunicate!=null&& s7PlcCommunicate.IsConnected )
                     {
                         byte []buff= s7PlcCommunicate.ReadByte(25, 780, 74);
                         int actuallength=buff[53];
@@ -300,14 +305,9 @@ namespace DefaultEdit.Core
                         CurrConfig.Power1_2 = s7PlcCommunicate.GetShortAt(buff, 22);
                         CurrConfig.Power1_3 = s7PlcCommunicate.GetShortAt(buff, 24);
                         CurrConfig.Power1_4 = s7PlcCommunicate.GetShortAt(buff, 26);
-                        CurrConfig.Power2_1 = s7PlcCommunicate.GetShortAt(buff, 28);
-                        CurrConfig.Power2_2 = s7PlcCommunicate.GetShortAt(buff, 30);
-                        CurrConfig.Power2_3 = s7PlcCommunicate.GetShortAt(buff, 32);
-                        CurrConfig.Power2_4 = s7PlcCommunicate.GetShortAt(buff, 34);
                         CurrConfig.Pressure1 = s7PlcCommunicate.GetRealAt(buff, 36);
-                        CurrConfig.Pressure2 = s7PlcCommunicate.GetRealAt(buff, 40);
                         CurrConfig.PTime1 = s7PlcCommunicate.GetTimeSpanAt(buff, 44);
-                        CurrConfig.PTime2 = s7PlcCommunicate.GetTimeSpanAt(buff, 48);
+                 
                     }
                 }
                 catch

+ 3 - 3
ViewModels/WorkRunViewModel.cs

@@ -60,9 +60,9 @@ namespace DefaultEdit.ViewModels
             LogHelper.Info("===========================程序启动==========================");
             Management.CurentApplicationSettings = FileHelper.ReadApplicationConfiguration();
             LogHelper.Info("加载配置文件完成");
-          
-            //创建一个定时器,每秒更新一次时间
-            var timer = new System.Timers.Timer(1000);
+            Management. ConnectPlc();
+              //创建一个定时器,每秒更新一次时间
+              var timer = new System.Timers.Timer(1000);
             timer.Elapsed += (sender, e) =>
             {
                 Management.DateTimeNow = DateTime.Now;